• GromBeestje

    (@grombeestje)


    Hello,

    I am having a problem with database access using the WPDB class.
    When I print the query to the output and run it in phpmyadmin, the query gives the expected result. However, WPDB does not provide the results. Also, there is no error message. The error logs show an database error, but without error message.

    $wpdb->show_errors();
    $testQuery = $wpdb->prepare("SELECT * FROM $table_name
                                 WHERE service_id = %d
                                 AND   token = %s" , $service_id, $token);
    $testResult = $wpdb->get_results($testQuery,ARRAY_A);
    
    echo "<pre>testQuery\n$testQuery\n";
    echo "testResult: "; print_r($testResult);
    echo "db error:" . $wpdb->last_error;
    echo "</pre>";

    Can anyone give their thoughts about this issue?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Did you remember to declare global $wpdb;? Your code otherwise looks fine.

    Thread Starter GromBeestje

    (@grombeestje)

    I have global $wpdb; as the first line of the function. Other queries are working fine in that function, so that doesn’t appear to be the problem.

    Aashish

    (@aashishsonawane)

    Hi,

    I have a small suggestion here, just try & include the prepare statement within

    $wpdb->query();

    I believe this would execute your query. Following is the modified query statement for reference:

    $wpdb->show_errors();
    $testQuery =
    $wpdb->query(
    	$wpdb->prepare("SELECT * FROM $table_name
                                 WHERE service_id = %d
                                 AND   token = %s" , $service_id, $token
    	)
    );
    $testResult = $wpdb->get_results($testQuery,ARRAY_A);
    
    echo "<pre>testQuery\n$testQuery\n";
    echo "testResult: "; print_r($testResult);
    echo "db error:" . $wpdb->last_error;
    echo "</pre>";

    Try this solution!


    Regards!

    Moderator bcworkz

    (@bcworkz)

    Verify that the variables used in your query actually contain the values you expect. These are the only differences between what you might run in phpMyAdmin and the query here, so they warrant close examination.

    If that checks out, I would start to suspect some sort of plugin or theme conflict. I adapted your code to run on my DB and it works fine that way. Of course I may have obscured an error in my adaptation, but since your query works in phpMyAdmin, between the two checks we’ve determined that all of the basic code is valid, except the variables themselves. If they check out, a conflict is the only other possibility.

    RossMitchell

    (@rossmitchell)

    It would really help me if you posted the output from your code. Just what is the string that the prepare statement creates ? Are the id and token really what we expect, does the token have internal spaces, in which case it will need quoting.
    And BTW what happens if you had:
    $token = "something; drop table;"
    I know what prepare is supposed to do, but I don’t expect it to be psychic in reading my intentions. In other words, would this be safer:

    $testQuery = $wpdb->prepare("SELECT * FROM $table_name
                                 WHERE service_id = %d
                                 AND   token = '%s'" , $service_id, $token);

    And can we trust the value in $table_name ? The structure of the application will answer this.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Query doesn't work in WPDB but works in phpmyadmin’ is closed to new replies.