This question already has an answer here:
- mysql count into PHP variable 2 answers
I have a custom table within my wordpress database. I would like to COUNT the rows in the table that have the same value.
So for example:
IP ADDRESS
0.0.0.0.0
0.0.0.0.1
0.0.0.0.0
The output would then be:
there are 2 x 0.0.0.0.0
there are 1 x 0.0.0.0.1
I can achieve the above fine with a normal sql query, but i am trying to do this via the $wpdb class
Here is my query:
$table = $wpdb->prefix . 'wplt';
$posts = $wpdb->get_results("SELECT ip_address, COUNT(ip_address) FROM $table GROUP BY ip_address");
foreach ( $posts as $post ) {
echo $post->ip_address;
echo $post->COUNT(ip_address);
}
With the above i get the following error:
Fatal error: Call to undefined method stdClass::COUNT()
</div>