我想显示销售人员完成的销售/购买数量.我有一个搜索框,当我输入他们的名字时,它应该带回一个数字.因此,要计算总购买量,我正在使用以下代码:
<?PHP
MysqL_connect ("****", "****","****") or die (MysqL_error());
MysqL_select_db ("*******");
$term = $_POST['term'];
$sql = MysqL_query("select count(*) from car_orders where side='buy' and sales_id like '%$term%'");
while ($row = MysqL_fetch_array($sql)){
echo 'owner_id: '.$row['sales_id'];
echo '<br/> side '.$row['side'];
echo '<br/><br/>';
}
?>
解决方法:
您需要添加ALIAS
select count(*) AS totalCOUNT from car_orders where ....
现在您可以使用它来获取价值
$row['totalCOUNT']
但在您的查询中看到,sales_id和side都没有被投影.以下查询仅是一个假设
select `sales_id`,`side`, count(*) AS totalCOUNT
from car_orders
where side='buy' and
sales_id like '%$term%'
GROUP BY `sales_id`,`side`
现在您可以获取所有值,
$row['sales_id']
$row['side']
$row['totalCOUNT']