我从这样的数据库中获取数据。
select categories.categorie_name, count(*)
from names
join categories on names.categories_id=categories.categories_id
group by categories.categorie_name
order by 2
我得到的结果是2列
categorie_name | count(*)
当我尝试从我的视图中访问count(*)列时,我无法打印列中的内容。
<?php foreach ($categories as $categorie)
echo $categorie->categorie_name." ".$categorie->count(*)."<br>";
?>
请问您如何解决这个问题?
答案 0 :(得分:2)
试试这个
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
然后使用像$categorie->cnt;
答案 1 :(得分:1)
对{}
使用大括号count(*)
,然后您可以正确地从对象访问它。
<?php
foreach ($categories as $categorie){
echo $categorie->categorie_name." ".$categorie->{'count(*)'}."<br>";
}
?>
另外,只需将{ref count(*)
添加为&#39; cat_count&#39;
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
现在使用$categorie->cat_count