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 ”