2016-03-23 175 views
1

我的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頁面。 我該怎麼做? 感謝

+0

在查詢中使用別名 - 爲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

+1

只注意到你有'$ sql1 = ...',但在'$ sql'上做查詢 - >'mysqli_query($ conn,$ sql);'。 – Sean

+0

感謝您的回答,它的工作 –

回答

0

事情是這樣的:

$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']; 
     } 
0

由於@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"; 
} 
0
$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"; 
}