我试图从2个不同的表中选择2个值并得到此错误。当我只从1张桌子中选出它的时候。这是我的代码(在查看这个问题的答案后已经改变它了):
$apartmentNum = $_GET['apartmentNumber'];
$getBillsSumQuery = "SELECT a.totalBillsAmount , b.count(id) FROM apartments AS a , bills AS b WHERE a.number = "'.$apartmentNum.'" AND b.apartmentNumber = '.$apartmentNum.'";
$totalSum = mysqli_query($con,$getBillsSumQuery);
$row = mysqli_fetch_assoc($totalSum); //this is where the program crushes
$newSum = $row['totalBillsAmount'];
$billsCount = $row['count(id)'];
echo '{"billSum":' . $newSum . ' , "billCount":' . $billsCount .'}';
答案 0 :(得分:0)
您需要写count(b.id)
而不是b.count(id)
。使用以下查询。
$getBillsSumQuery = "SELECT a.totalBillsAmount , count(b.id) FROM apartments AS a , bills AS b
WHERE a.number = "'.$apartmentNum.'" AND b.apartmentNumber = '.$apartmentNum.'";