简体   繁体   中英

Wordpress: using $wpdb->get_results instead of mysqli_query causes empty var

I'm trying to translate a query built with mysqli_query in a wordpress query. The problem is that using $wpdb->get_results I'm getting no results at all. I've even tried with $wpdb->query and $wpdb->get_var with any results too.

$MYSQLi contains the database informations as you can imagine Of course all the variables are fine! They have the correct values inside.

The old query

                $check_last_conversation = mysqli_query($MYSQLi,"select * from vp_pms_messages inner join vp_pms_group_users on vp_pms_messages.id = vp_pms_group_users.message_id and vp_pms_messages.group_id = vp_pms_group_users.group_id where vp_pms_group_users.from_username = '".mysqli_real_escape_string($MYSQLi,$session_username)."' and vp_pms_group_users.from_del = '".mysqli_real_escape_string($MYSQLi,'0')."' or vp_pms_group_users.to_username = '".mysqli_real_escape_string($MYSQLi,$session_username)."' and vp_pms_group_users.to_del = '".mysqli_real_escape_string($MYSQLi,'0')."' group by vp_pms_messages.group_id ".$order_data_by_this." desc limit 8");

The Wordpress one

                $check_last_conversation = $wpdb->get_results("select * from ".$wpdb->prefix."vp_pms_messages inner join ".$wpdb->prefix."vp_pms_group_users on ".$wpdb->prefix."vp_pms_messages.id = ".$wpdb->prefix."vp_pms_group_users.message_id and ".$wpdb->prefix."vp_pms_messages.group_id = ".$wpdb->prefix."vp_pms_group_users.group_id where ".$wpdb->prefix."vp_pms_group_users.from_username = '".$session_uid."' and ".$wpdb->prefix."vp_pms_group_users.from_del = '0' or ".$wpdb->prefix."vp_pms_group_users.to_username = '".$session_uid."' and ".$wpdb->prefix."vp_pms_group_users.to_del = '0' group by ".$wpdb->prefix."vp_pms_messages.group_id ".$order_data_by_this." desc limit 8");

Could it be some encoding issue? Cn you give me some directions? Thanks.

I assume your wp table prefix is vp_ . If that's so, by doing $wpdb->prefix."vp_pms_messages you will get vp_vp_pms_messages which is non-existent table. Same goes for other tables. You can also check your php error logs as well as use plugin called Query Monitor to see if there are any error and other debugging

Have you set global $wpdb in your function?

If it's not that, then check the value $wpdb->last_error after the call to see what the issue may be.

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