Pergunta

I am running a query on a wordpress database to count the number of occurences of the value 'apple' in the wp_fruit table like so...

$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 ) )

The value I am after is 238, how do I echo this out?

Foi útil?

Solução

$count is an array of row objects, in this case only one:

echo $count[0]->count;

It might make more sense if you use $row instead of $count.

echo $row[0]->count;

So you are accessing the count column (object property) of the first row (row 0).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top