我正在使用php 7.2 ...
代码工作正常
<?php
$add=mysqli_query($conn,'SELECT ac_id, SUM(amount) from stac where ac_id="117" ');
while($row1=mysqli_fetch_array($add)) { $mark=$row1['SUM(amount)'];
echo $mark ; }?>
但下面的代码不起作用。请帮帮我。
<?php
$ac_id=$_POST['ac_id'];
$add=mysqli_query($conn,'SELECT ac_id, SUM(amount) from stac where ac_id=$ac_id ');
while($row1=mysqli_fetch_array($add)) { $mark=$row1['SUM(amount)'];
echo $mark ; }?>
答案 0 :(得分:0)
看起来你的id字段不是整数所以你应该将变量传递给单引号(')并且还用双引号(“)传递你的查询,这样变量就能正确解析。
用此
替换您的查询$add=mysqli_query($conn,"SELECT ac_id, SUM(amount) from stac where ac_id='$ac_id' ");
答案 1 :(得分:0)
使用聚合时,您是否忘记添加组? 像这样:
SELECT ac_id, SUM(amount) from stac where ac_id=$ac_id group by ac_id
答案 2 :(得分:-1)
尝试将您的查询更改为
$add=mysqli_query($conn,"SELECT ac_id, SUM(amount) from stac where ac_id='$ac_id' ");