Panchoa
Panchoa

Reputation: 147

Wordpress - $wpdb->get_results not getting any result

I made a small function in a custom page to get the content from a table and to display it to the user. It was working correctly before but since I changed the DNS it isn't working anymore.

Before, my code looked like this :

global $wpdb;
$table = "0_luxcom_token";
$query = $wpdb->prepare("SELECT token_name, token_dateexp FROM $table");
$resultat = $wpdb->get_results($query, ARRAY_A);

PHP was returning an error because prepare() didn't have an argument but it was still working.
Now, PHP is returning me a fatal error so it doesn't work anymore. I changed my code to this :

global $wpdb;
$table = "0_luxcom_token";
$query = $wpdb->prepare("SELECT token_name, token_dateexp FROM %s", $table);
$resultat = $wpdb->get_results($query, ARRAY_A);

I don't get any error from prepare() but now, get_results->($query, ARRAY_A); doesn't return anything.

I tried var_dump($resultat) and it returned me

array(0){ }

Then I tried var_dump($query) and it returned me

string(54) "SELECT token_name, token_dateexp FROM '0_luxcom_token'"

I noticed when I put my table in the argument it's now between simple quote, so I wonder if it's because of this it's not working anymore but I'm not sure.

Upvotes: 0

Views: 2469

Answers (1)

u_mulder
u_mulder

Reputation: 54831

If you have no variables to prepare for query, use simple query method:

global $wpdb;
$table = "0_luxcom_token";
$query = $wpdb->query("SELECT token_name, token_dateexp FROM $table");
$resultat = $wpdb->get_results($query, ARRAY_A);

Or just get_results:

global $wpdb;
$table = "0_luxcom_token";
$resultat = $wpdb->get_results("SELECT token_name, token_dateexp FROM $table", ARRAY_A);

Upvotes: 3

Related Questions