简体   繁体   中英

Wordpress $wpdb->get_results returns an empty array but phpMyAdmin returns multiple rows. Why?

My WordPress plugin code looks like this:

$table = $wpdb->prefix."cf_form_entry_values";
$where = "entry_id = ".$entryid;
$select = "SELECT id FROM {$table} WHERE {$where}";
$query = $wpdb->prepare( $select );
$result = $wpdb->get_results( $query );

It produces the following mysql query: SELECT id FROM wpqs_cf_form_entry_values WHERE entry_id = 49

WordPress returns an empty array. No SQL errors reported (I have show_errors() set), var_dump is empty. But if I cut and paste that query into phpMyAdmin it returns the expected rows. I don't understand why WordPress is giving me empty results. I've also tried SELECT * FROM wpqs_cf_form_entry_values WHERE entry_id = 49

I've noted that if I remove the WHERE clause, WordPress is happy to return the entire table to me, but why can't I filter it with a WHERE clause?

MORE INFORMATION AFTER FURTHER TESTING... It's curious what works and what doesn't.

These two queries don't work:

SELECT * FROM wpqs_cf_form_entry_values WHERE entry_id = 55 AND slug = 'photo'
SELECT * FROM wpqs_cf_form_entry_values WHERE entry_id = 55

But this one does:

SELECT * FROM wpqs_cf_form_entry_values WHERE slug = 'photo'

I've tried putting the entry_id value in quotes and without. Does that extra info help anyone?

Could you please try to add this code?

$wpdb->show_errors( true )

Maybe it will then show some information on why it dosen't work. Did you try to write without PHP variables, like a complete string?

Also try to put table in qoutes

SELECT * FROM `wpqs_cf_form_entry_values`

I have also noticed entry_id is missing from SELECT You should have it like this:

SELECT id,entry_id FROM `wpqs_cf_form_entry_values`

Also try to put on top of the file

global $wpdb

$where = "entry_id = ".$entryid; I suppose $entryid should be wrapped in ''

The select statement is not being correctly set up for the wpdb->prepare() method.

$wpdb->prepare() needs 2 arguments. 1) the select statement 2) the values which are variables in the statement.

  1. The select statement should be created with "sprintf formatting" in the following manner:

Where value is a digit:

"select * from table where id = %d";

Where value is a string:

"select * from table where name = %s";

Where value is a "float":

"select * from table where id = %f";

Where value is a mix (any number or mixture is possible):

"select * from table where id = %d OR name = %s OR price = %f";

%d , %s , %f are the only 3 values which should go in a select statement which is going to be sent to wpdb->prepare()

In your case, ASSUMING entry_id is an integer, the statement should be created as follows:

$table = $wpdb->prefix."cf_form_entry_values";
$where = "entry_id = %d";
$select = "SELECT id FROM $table WHERE $where";

The second parameter is the value or values to replace the "sprintf formats". These must be in the order they appear in the select statement. In your case, the only value is $entryid . I pass these values in an array, but they can be passed in individually. In your case, I would pass the select statement and the $entryid in as follows:

$prepared_statement = $wpdb->prepare( $select,[$entryid] );
$results = $wpdb->get_results($prepared_statement);

Note, I have changed your $result variable to $results as this $wpdb->get_results() is for returning multiple rows. If you're expecting a single row, it's best to use $wpdb->get_row() with your prepared statement.

MORE INFORMATION:https://developer.wordpress.org/reference/classes/wpdb/prepare/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM