2

I am fetching data from a database like this.

select categories.categorie_name, count(*)
from names
join categories on names.categories_id=categories.categories_id
group by categories.categorie_name
order by 2

i got as a result 2 columns

 categorie_name  |  count(*) 

how ever when I try to acces the count(*) column from my view I can not print what is in the column.

<?php foreach ($categories as $categorie)
        echo $categorie->categorie_name." ".$categorie->count(*)."<br>";
?>

can you please advice how to solve this?

Rubenex
  • 469
  • 2
  • 8
  • 23

2 Answers2

2

Try this

select categories.categorie_name, count(*) as cnt
from names
join categories on names.categories_id=categories.categories_id
group by categories.categorie_name
order by 2

then use cnt like $categorie->cnt;

Adarsh M Pallickal
  • 813
  • 3
  • 16
  • 37
1

Use braces {} for count(*) then you can access that from object properly.

   <?php 
    foreach ($categories as $categorie){
            echo $categorie->categorie_name." ".$categorie->{'count(*)'}."<br>";
    }
    ?>

In other way just add add ref to count(*) as 'cat_count'

select categories.categorie_name, count(*) as cat_count
from names
join categories on names.categories_id=categories.categories_id
group by categories.categorie_name
order by 2

Now access with $categorie->cat_count

mrsrinivas
  • 34,112
  • 13
  • 125
  • 125