6

As title implies, I cannot seem to get $wpdb->get_results to give results. Running the "raw" query is successful, but this seems to be failing for some strange unknown reason.

    global $wpdb;


    $sql = "SELECT *
            FROM something aki, 
                 somethingelse akb, 
                 somethingelseelse ac 
            WHERE aki.keyID = akb.keyID 
              AND akb.someID = ac.someID 
              AND aki.type LIKE '%thing%'";

    $corps = $wpdb->get_results($sql, OBJECT);

    var_dump($corps);
  • Have you tried runnning this query directly in the phpmyadmin? – TBI Infotech Sep 22 '14 at 5:15
  • 1
    Yes, phpmyadmin and mysql shell directly. It works in both. Yet no results (empty array) in $wpdb->get_results – 1Up Sep 22 '14 at 5:40
  • 1
    Not able to figure out the exact issue. But you can debug it by using it without second parameter OBJECT and using simple query and then adding conditions. You will get to know the issue – TBI Infotech Sep 22 '14 at 5:49
  • 1
    Arrrr... I just used $wpdb->show_errors( true ) before the query and I got the error back. It seems that WP started encoding the ' character inside queries suddenly and out of the blue... Might be the reason of your problems too. Might want to feed '%thing%' as a parameter to get_reults. – NoOne Feb 27 '16 at 19:44
  • 1
    Eventually, my problem turned out to be that I should use $wpdb->posts instead of wp_posts inside the query. I guess that somehow, for no apparent reason, the DB tables on our server got renamed? WTF?!?!?! – NoOne Feb 27 '16 at 20:03
2

Use $wpdb->show_errors( true ) before the query and see what error comes back.

My problem (because I've experienced the same thing) was that I should use $wpdb->posts instead of wp_posts inside the query. The prefix of the tables can change from WP installation to installation or even in the same installation depending on the time (e.g. an admin can change the prefix whenever he likes). So, one should write the query like that:

$query = "select <stuff here> from $wpdb->posts where <stuff here>";
| improve this answer | |
0

May be a bit late, but you are missing the "prepare" call necessary in wordpress:

$Table_Name    = $wpdb->prefix.'Your Table Name';
$sql_query     = $wpdb->prepare("SELECT * FROM $Table_Name WHERE aki.keyID=%d AND akb.someID=%d AND aki.type like %s", akb.keyID, ac.someID, ''%thing%');
$result        = $wpdb->query( $sql_query ); 

Multiple results can be found in the array.

| improve this answer | |
  • He mentioned that he had the table name wrong above. The $wpdb->prepare() call isn;t required for a query that is entirely static and not user-originated so isn't likely to be helpful here. In any case, your prepare() line above contains code that is not PHP. – Brian C Apr 11 at 4:31
  • For numeric searchs no...but for mySQL string searches, require a different set of parameter passing that the prepare call does for you. Otherwise you have to insert the '%s%' around the string. If you don't the mysql search fails – Debbie Kurth Apr 12 at 21:49
  • Correct, but you misread. “Static” means unchanging, ie a query based entirely on fixed text, within variables inserted. The query shown was fixed test (aka “static”) so using prepare adds nothing. However it’s good practice to use prepare() when interpolating values into a query (aka dynamic or non-static). Cheers. – Brian C Apr 13 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.