1

This is my var_dump result in WordPress but when I want to echo COUNT(*) this showed error reports. Parse error: syntax error, unexpected '*' in

But package is showed well. How can I echo each COUNT(*)?

My sql is:

$aaaaa = $wpdb->get_results( "SELECT package, COUNT(*) FROM $table_name GROUP BY package");

var_dump Result:

 array (size=5)
      0 => 
        object(stdClass)[592]
          public 'package' => string 'package 1' (length=9)
          public 'COUNT(*)' => string '1' (length=1)
      1 => 
        object(stdClass)[593]
          public 'package' => string 'package 3' (length=9)
          public 'COUNT(*)' => string '3' (length=1)
      2 => 
        object(stdClass)[594]
          public 'package' => string 'Package 4' (length=9)
          public 'COUNT(*)' => string '2' (length=1)

Echo:

    echo  $aaaaa[2]->package;
    echo  $aaaaa[2]->COUNT(*);
Nirav Madariya
  • 1,470
  • 2
  • 24
  • 37

2 Answers2

1

You need to give your count column a simple text label:

SELECT package, COUNT(*) as count FROM $table_name GROUP BY package

morrisdj
  • 84
  • 3
1

The name of the property is 'COUNT(*)', but it doesn't fit the standart rules of variables` naming. Nevertherless you still can use such names for the object's properties, but you can't access it by normal way. You can access it such way:

echo  $aaaaa[2]->{'COUNT(*)'};

But it would be better, if you give the label for count column field, as @morrisdj have proposed in the answer above.

Kulikov Sergey
  • 265
  • 1
  • 9