最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

mysql - $wpdb->get_var returns 0

programmeradmin1浏览0评论

I use following code to get a number :

$where = 'WHERE type = 4 AND active = 1 AND user_id = ' . $user_id ;
$user_followed = $wpdb->get_var("SELECT COUNT( * ) AS total FROM {$wpdb->tablex} {$where}");

But it returns 0 (zero). What is the problem?

I use following code to get a number :

$where = 'WHERE type = 4 AND active = 1 AND user_id = ' . $user_id ;
$user_followed = $wpdb->get_var("SELECT COUNT( * ) AS total FROM {$wpdb->tablex} {$where}");

But it returns 0 (zero). What is the problem?

Share Improve this question edited Jun 19, 2019 at 20:38 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Jun 19, 2019 at 19:50 YearmazYearmaz 396 bronze badges 1
  • It's impossible for anyone here to tell you why a query to your own custom data isn't working. As far as anyone cal tell from this question, you're getting 0 because that's the correct response. If it's not then you need to put something in the question that helps. – Jacob Peattie Commented Jun 22, 2019 at 8:23
Add a comment  | 

1 Answer 1

Reset to default 0

Your count for that query may be 0. Make sure $wpdb->tablex is correct.

Also, definitely use $wpdb->prepare

$user_followed = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) AS total FROM {$wpdb->tablex} WHERE type = %d AND active = %d AND user_id = %d", 4, 1, $user_id ) );

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论