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
1 Answer
Reset to default 0Your 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 ) );