我的SQL代碼`如何在php中顯示sql的sum()的sql查詢結果?
$sql1 = "select sum(marks) from `result_master` where roll_no='$roll_no' and round='$round' GROUP BY `event_name`" ;
$result1 = mysqli_query($conn,$sql);
`
我想用回聲呼應SQL查詢的結果PHP頁面。 我該怎麼做? 感謝
我的SQL代碼`如何在php中顯示sql的sum()的sql查詢結果?
$sql1 = "select sum(marks) from `result_master` where roll_no='$roll_no' and round='$round' GROUP BY `event_name`" ;
$result1 = mysqli_query($conn,$sql);
`
我想用回聲呼應SQL查詢的結果PHP頁面。 我該怎麼做? 感謝
事情是這樣的:
$sql1 = "select sum(marks) as total_marks from `result_master` where roll_no='$roll_no' and round='$round' GROUP BY `event_name`" ;
$result1 = mysqli_query($conn,$sql1);
while($row = mysqli_fetch_assoc($result1){
echo $row['total_marks'];
}
由於@sean的意見建議你有$sql1 = ...,
但$result1-> mysqli_query($conn,$sql);
做查詢。
將其更改爲$result1 -> mysqli_query($conn,$sql1);
代碼應該是這樣的:
$sql1 = "select sum(marks) AS Total_Marks from `result_master` where roll_no='$roll_no' and round='$round' GROUP BY `event_name`" ;
$result1 = mysqli_query($conn,$sql1);
while($row = mysql_fetch_array($result1)) {
echo "Total Marks = ".$row['Total_Marks ']."\n";
}
$sql1 = "select event_name, sum(marks) sum from `result_master` where roll_no='$roll_no' and round='$round' GROUP BY `event_name`";
$result1 = mysqli_query($oDb, $sql1);
$aData = $result1->fetch_all(MYSQLI_ASSOC);
foreach ($aData as $aRow){
echo "Event: {$aRow['event_name']} -> count: {$aRow['sum']} \n";
}
在查詢中使用別名 - 爲sum_marks'選擇總和(標記)...'。然後,在你的'$ result1 = mysqli_query($ conn,$ sql);'你用['mysqli_fetch_assoc()'](http://php.net/manual/en/mysqli-result.fetch-assoc) PHP),即。 'while($ row = mysqli_fetch_assoc($ result1){echo $ row ['sum_marks']。「
」;}' – Sean
只注意到你有'$ sql1 = ...',但在'$ sql'上做查詢 - >'mysqli_query($ conn,$ sql);'。 – Sean
感謝您的回答,它的工作 –