6

我正在使用以下代码:

                    $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 也会返回 1?有任何想法吗?

我放入的变量看起来不错。所以它应该正确查询。

4

2 回答 2

15
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;

现在它返回从上面查询中选择的行数,如果没有选择行,则返回 0.....

于 2015-02-18T06:40:28.360 回答
3

如果您只想要计数(也许是分页总数),这样做会更快:

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;
于 2019-02-02T00:05:41.793 回答