从数组中回显数值

3

我正在WordPress数据库上运行查询,以计算wp_fruit表中值为“apple”的出现次数,如下所示...

$count = $wpdb->get_results("SELECT COUNT(*) as count FROM wp_fruit WHERE value='apple'" );

which returns....

array(1) { [0]=> object(stdClass)#446 (1) { ["count"]=> string(3) "238" } } Array ( [0] => stdClass Object ( [count] => 238 ) ) array(1) { [0]=> object(stdClass)#445 (1) { ["count"]=> string(3) "238" } } Array ( [0] => stdClass Object ( [count] => 238 ) ) array(1) { [0]=> object(stdClass)#446 (1) { ["count"]=> string(3) "238" } } Array ( [0] => stdClass Object ( [count] => 238 ) )

我需要输出的值是238,应该如何打印它?
1个回答

3

$count 是一个行对象数组,在这种情况下只有一个:

echo $count[0]->count;

如果你使用$row而不是$count可能会更有意义。
echo $row[0]->count;

因此,您正在访问第一行(第0行)的计数列(对象属性)。


谢谢,现在明白了。 - fightstarr20

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接