问题
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?
The variables I am putting in look fine. so it should be querying correctly.
回答1:
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.....
回答2:
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;
来源:https://stackoverflow.com/questions/13235947/wordpress-wpdb-get-results-and-num-rows