我正在尝试从 2 个不同的表中选择 2 个值并收到此错误。 当我只从 1 个表中选择时,它起作用了。 这是我的代码(在查看此问题 n 行的答案时已经更改了它):
$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 .'}';
你需要写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.'";