简体   繁体   English

num_rows不返回任何值

[英]num_rows returns no value

I'm using the following code to find the number of rows returned: 我正在使用以下代码来查找返回的行数:

global $wpdb;

$results = $wpdb->get_results("SELECT * FROM list WHERE queue = 1 ORDER BY id ASC LIMIT 0,1");

$rowcount = $results->num_rows;
echo $rowcount;

As you can see, the results are limited to 1 row, and when I run this query in SQL it returns 1 row just fine. 如您所见,结果限制为1行,当我在SQL中运行此查询时,它返回1行就可以了。 But in PHP the $rowcount doesn't return any value at all. 但是在PHP中, $rowcount根本不返回任何值。

Any ideas what might be wrong? 任何想法可能有什么问题吗? I get no error. 我没有错。

I think you need to use count($results) since the method $wpdb->get_results returns a array of objects/arrays. 我认为您需要使用count($results)因为方法$wpdb->get_results返回对象/数组的数组。

Another way to get the number of rows is to use $wpdb->num_rows . 获取行数的另一种方法是使用$wpdb->num_rows Apparently this works for $wpdb->get_results . 显然,这适用于$wpdb->get_results

From the docs ( http://codex.wordpress.org/Class_Reference/wpdb ), it says the following about get_results: 从文档( http://codex.wordpress.org/Class_Reference/wpdb )中,它对get_results表示以下内容:

Generic, multiple row results can be pulled from the database with get_results. 可以使用get_results从数据库中提取通用的多行结果。 The function returns the entire query result as an array. 该函数以数组形式返回整个查询结果。 Each element of this array corresponds to one row of the query result and, like get_row, can be an object, an associative array, or a numbered array. 该数组的每个元素对应于查询结果的一行,并且像get_row一样可以是一个对象,一个关联数组或一个编号数组。 If no matching rows are found, or if there is a database error, the return value will be an empty array. 如果找不到匹配的行,或者存在数据库错误,则返回值将为空数组。 If your $query string is empty, or you pass an invalid $output_type, NULL will be returned. 如果$ query字符串为空,或者传递了无效的$ output_type,则将返回NULL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM