Wordpress $wpdb->get_results 和 num_rows
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13235947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress $wpdb->get_results and num_rows
提问by Jimmyt1988
I'm using the following code:
我正在使用以下代码:
$wpdb->get_results("
SELECT * FROM " . $wpdb->prefix . "product_order
WHERE
rel = '" . $post["id"] . "' AND
`range` = '" . $range . "' AND
category = '" . $range . "'
");
echo $wpdb->num_rows;
num_rows returns 1 even though there is no rows in the database? Any ideas?
即使数据库中没有行,num_rows 也返回 1?有任何想法吗?
The variables I am putting in look fine. so it should be querying correctly.
我放入的变量看起来不错。所以它应该正确查询。
回答by Raman Singh
global $wpdb;
$wpdb->get_results("
SELECT * FROM " . $wpdb->prefix . "product_order
WHERE
rel = '" . $post["id"] . "' AND
`range` = '" . $range . "' AND
category = '" . $range . "'
");
echo $wpdb->num_rows;
Now it returns numbers of rows select from above query and 0 if no row selected.....
现在它返回从上面查询中选择的行数,如果没有选择行,则返回 0.....
回答by Scotty G
if you JUST want the count (maybe for pagination total), it is faster to do:
如果你只是想要计数(也许是分页总数),这样做会更快:
global $wpdb;
$rows = $wpdb->get_results("
SELECT COUNT(*) as num_rows FROM " . $wpdb->prefix . "product_order
WHERE
rel = '" . $post["id"] . "' AND
`range` = '" . $range . "' AND
category = '" . $range . "'
");
echo $rows[0]->num_rows;