gpt4 book ai didi

php - 使用 COUNT( * ) 返回 Mysql 结果

转载 作者:行者123 更新时间:2023-11-29 09:14:19 26 4
gpt4 key购买 nike

我正在尝试从 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];

最佳答案

COUNT(*) 后使用 AS field_name

$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”替换为您选择的任何相关名称)

关于php - 使用 COUNT( * ) 返回 Mysql 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4606110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com