Skip to content
Advertisement

Referencing the count in object PHP

I’m trying to reference COUNT(*) from the object below. I grab the date using $the_date = $obj2->the_date as per the below example, but how do I reference count? Of course using COUNT(*) will throw an error.

object(stdClass)[358]
  public 'COUNT(*)' => string '36' (length=2)
  public 'the_date' => string '2019-08-29' (length=10)

Advertisement

Answer

You need to alias the concerned column in the resultset of the query, like:

SELECT the_date, COUNT(*) as cnt FROM ...

Then:

object(stdClass)[358]
  public 'cnt' => string '36' (length=2)
  public 'the_date' => string '2019-08-29' (length=10)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement