我正在尝试从mysql返回一个计数。我的代码在
之下$number_of_options_sql = tep_db_query("SELECT COUNT( * ) FROM
(select sum(options_id) as total from products_attributes
where products_id='".$products_id."' group by options_id) as total");
$number_of_options_result = tep_db_fetch_array($number_of_options_sql);
当我在Phpmyadmin中运行此查询时,它会在表标题中显示COUNT(*)
的结果。我得到了正确的结果,查询对我有效,我无法在屏幕上打印出来。
我尝试以下列方式返回值,并且不在屏幕上打印任何内容:
echo $number_of_options_result[COUNT( * )];
echo $number_of_options_result[total];
答案 0 :(得分:2)
在AS field_name
COUNT(*)
$number_of_options_sql = tep_db_query("SELECT COUNT(*) AS field_name FROM (select sum(options_id) as total from products_attributes where products_id='".$products_id."' group by options_id) as total");
要打印:
echo $number_of_options_result['field_name'];
(将“field_name”替换为您选择的任何相关名称)
答案 1 :(得分:0)
只需将您的查询编辑为
即可SELECT COUNT(*) as count FROM ....
然后它将像'count'一样存储,你可以打印$ number_of_options_result [count];方式。
答案 2 :(得分:0)
您的查询是为表/查询而不是列分配别名。使用下面这个:
$number_of_options_sql = tep_db_query("SELECT COUNT(*) as total FROM (select sum(options_id) as total from products_attributes where products_id='".$products_id."' group by options_id) a");
$number_of_options_result = tep_db_fetch_array($number_of_options_sql);
看起来你想知道product_id的不同选项id的数量,我会按如下方式重写查询:
$number_of_options_sql = tep_db_query("SELECT COUNT(DISTINCT options_id) as total FROM products_attributes WHERE products_id='".$products_id."'");
答案 3 :(得分:0)
输入变量“total”
$number_of_options_sql = tep_db_query("SELECT COUNT(*) as total
然后它起作用
echo $number_of_options_result['total'];