admin管理员组文章数量:1129441
I'm trying to do a count for pagination and its returning NULL why the query returns results correctly. In this case $total returns NULL which is not correct why? (I didn't include the $args as they work)
$total_query = "SELECT COUNT(*) FROM (${args}) AS total_count";
$total = $wpdb->get_var( $total_query );
$items_per_page = 5;
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$offset = ( $page * $items_per_page ) - $items_per_page;
$results = $wpdb->get_results( $args . " ORDER BY user_registered DESC LIMIT ${offset}, ${items_per_page}" );
I'm trying to do a count for pagination and its returning NULL why the query returns results correctly. In this case $total returns NULL which is not correct why? (I didn't include the $args as they work)
$total_query = "SELECT COUNT(*) FROM (${args}) AS total_count";
$total = $wpdb->get_var( $total_query );
$items_per_page = 5;
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$offset = ( $page * $items_per_page ) - $items_per_page;
$results = $wpdb->get_results( $args . " ORDER BY user_registered DESC LIMIT ${offset}, ${items_per_page}" );
Share
Improve this question
edited Dec 23, 2016 at 5:51
Nelson101
asked Dec 23, 2016 at 3:10
Nelson101Nelson101
451 silver badge9 bronze badges
0
2 Answers
Reset to default 1In your code it is actually counting the query you have inserted in $testargs
. In your case you entered one. So it is giving you back one. If you need to get the count on WordPress users table then write the query like below-
global $wpdb;
$total_query = "SELECT COUNT(*) FROM {$wpdb->users} AS total_count";
$total = $wpdb->get_var( $total_query );
i think you code snippet should look something like below code snippet in your code i am not getting why you are adding the same query again in $total_query
by appending $testargs
so try the below code snippet that should work for you.
$total_query = "SELECT COUNT(*) FROM {$wpdb->users}";
$total = $wpdb->get_var( $total_query );
echo $total;
本文标签: mysqlwpdbgtgetvar not returning count
版权声明:本文标题:mysql - $wpdb->get_var not returning count 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736744598a1950702.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论