2017-05-09 54 views
0

我一直在尋找五天中的更好的一部分(沒有笑話,不想打擾你們),但我似乎無法得到這個權利。我想使用PHP中的函數在屏幕上顯示一列(技術)的總量。別擔心,$ day部分可用於其他功能。我不認爲這是問題的一部分。先謝謝你。如何使用MySQL和PHP在列中獲得總和

function calc_tech($day){ 

    include("connect.php"); // include database connection 

    $res = $mysqli -> query(" SELECT * FROM sales WHERE day = $day ") or die($mysqli->error); // Fetch data from the table 

    while($val = $res -> fetch_array()){ 
     $tech_total = $val[tech] += $val[tech]; 
    } 

    echo $tech_total; // Echo the result 
    //echo "42"; 

    $res -> free(); // Free the query results 
    $mysqli -> close(); // Close mysqli object 

} 

我的數據庫表看起來像這樣:

enter image description here

+0

選擇總和(技術); – JYoThI

+0

你想在這裏輸出什麼? –

+0

那麼,什麼是不工作? –

回答

1

你需要做的SUM()查詢列:

功能calc_tech($日){

include("connect.php"); // include database connection 

$res = $mysqli -> query(" SELECT SUM(tech) as sum FROM sales WHERE day = $day ") or die($mysqli->error); // Fetch data from the table 

$val = $res -> fetch_array(); 
    $tech_total = $val['sum']; 


echo $tech_total; // Echo the result 

}

+1

非常感謝!它現在完美運行! – adesgagne

0

1)'$day'應該用單引號括起來。

FOR SUM of column name查詢應該是這樣的,從哪天='$當天的銷量

"select sum(tech) as Toatal from sales where day='$day';" 
+0

這確實有助於謝謝你! – adesgagne

+0

很高興幫助你@adesgagne – JYoThI