我想在html中显示“Vendido -Pagado”(我的表中的2列)的结果但是下面的代码不起作用,任何线索为什么?
<?php
//connect to db
include '../connect.php';
$saldo = "SELECT Vendido-Pagado AS Saldo FROM Cooks WHERE Mail = '$email'";
$resultsaldo = mysqli_query($conn, $saldo);
if (mysqli_num_rows($resultsaldo) > 0) {
// output data of each row
while($row3 = mysqli_fetch_assoc($resultsaldo) {
echo '<a class="w-nav-link navlink right" href="../misaldo.php">Saldo: '
.$row3['Saldo']. "</a>";
}} else { echo "There was an error fetching the total";}
mysqli_close($conn);
?>
答案 0 :(得分:2)
对于MYSQL查询中的SUM,您可以使用MYSQL SUM aggregate function
$saldo = "SELECT (SUM(Vendido) + SUM(Pagado)) AS Saldo FROM Cooks WHERE Mail = '$email'";
OR
我使用了$row3['col1']
和$row3['col2']
来展示如何总结两列,不要忘记更改列的名称
col1
,col2
。
while($row3 = mysqli_fetch_assoc($resultsaldo) {
echo "Sum of two columns : ".($row3['col1'] + $row3['col2']);
}
注意:如果您使用mysqli
,最好使用mysqli prepared statements.
答案 1 :(得分:0)
您可以直接使用sql查询。
更改此
SELECT Vendido-Pagado AS Saldo FROM Cooks WHERE Mail = '$email'
到
SELECT CONCAT( Vendido, '-', Pagado ) AS Saldo FROM Cooks WHERE Mail = '$email'