Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take Count (*) as variable from SQL to php?

Tags:

sql

php

mysql

I have made request to SQL:

SELECT meta_value,  COUNT(*)  from wp_postmeta; 

and have in respond an array:

array (size=102)
  0 => 
    object(stdClass)[24]
      public 'meta_value' => string '37' (length=2)
      public 'COUNT(*)' => string '147' (length=3)
  1 => 
    object(stdClass)[23]
      public 'meta_value' => string '32' (length=2)
      public 'COUNT(*)' => string '143' (length=3)

I take "meta_value" without any troubles with php code:

$result->meta_value;

But how take values of public 'COUNT(*)' => string '143' (length=3)? I have tried different syntaxis and some errors only.

I need values: 147,143...


1 Answers

Use AS to create an alias

SELECT meta_value,  COUNT(*) As count  from wp_postmeta; 

then use count

like image 69
Marco Acierno Avatar answered Jun 15 '26 01:06

Marco Acierno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!