在PHP中获取MySQL列的总和

问题描述:

我在表中添加一列并返回总和。我有一个循环,但它不工作。在PHP中获取MySQL列的总和

while ($row = mysql_fetch_assoc($result)){ 
    $sum += $row['Value']; 
} 

echo $sum; 
+0

您应该将其替换为执行总和的SQL查询,或者您应该在循环前初始化'$ sum = 0'。我建议使用sum()字段进行SQL查询。 – fbstj 2011-04-27 18:08:15

+1

这里没有什么错,除了$ sum之前可以有一些价值循环,你最喜欢的是什么? – piotrm 2011-04-27 18:08:32

+1

你必须声明$ sum变量高于WHILE循环到'0' – SagarPPanchal 2013-06-06 05:00:20

您可以完全处理它在MySQL查询:

SELECT SUM(column_name) FROM table_name; 

在PHP代码,试试这个:

$result = mysql_query('SELECT SUM(value) AS value_sum FROM codes'); 
$row = mysql_fetch_assoc($result); 
$sum = $row['value_sum']; 

使用PDO(mysql_query折旧)

$stmt = $handler->prepare("SELECT SUM(value) AS value_sum FROM codes"); 
$stmt->execute(); 

$row = $handler->fetchAll(PDO::FETCH_OBJ); 
$sum = $row->value_sum; 
+0

像这样'$ sum = mysql_query(“SELECT SUM(Value)FROM Codes”);'用这个我得到'资源id#10',但不是所有值的总和。 – jack 2011-04-27 18:47:02

+4

尝试'$ result = mysql_query('SELECT SUM(value)AS value_sum FROM codes'); $ row = mysql_fetch_assoc($ result); $ sum = $ row ['value_sum'];'。 – Flinsch 2011-04-27 19:03:12

+0

什么是value和value_sum? – 2016-07-28 06:56:24

$row['Value']可能是一个字符串。尝试使用 intval($row['Value'])

此外,请确保在循环之前设置了$sum = 0

或者,更好的是,将SUM(Value) AS Val_Sum添加到您的SQL查询中。

$sql = "SELECT SUM(Value) FROM Codes"; 

$result = mysql_query($query); 

while($row = mysql_fetch_array($result)){ 

    sum = $row['SUM(price)']; 

} 

echo sum; 

$query = "SELECT * FROM tableName"; 
$query_run = mysql_query($query); 

$qty= 0; 
while ($num = mysql_fetch_assoc ($query_run)) { 
    $qty += $num['ColumnName']; 
} 
echo $qty; 

我已经取代你的代码,它工作得很好

$sum=0; 
while ($row = mysql_fetch_assoc($result)){ 
    $value = $row['Value']; 

    $sum += $value; 
} 

echo $sum; 

$result=mysql_query("SELECT SUM(column) AS total_value FROM table name WHERE column='value'"); 
$result=mysql_result($result,0,0); 

试试这个:

$sql = mysql_query("SELECT SUM(Value) as total FROM Codes"); 
$row = mysql_fetch_array($sql); 
$sum = $row['total']; 

获得一笔特定的行值使用PHP MYSQL

"SELECT SUM(filed_name) from table_name" 
+1

你的意思是使用MySQL? – TGrif 2017-09-06 14:50:17

+0

我们从MYSQL得到结果时得到的结果,然后我们为什么试图让一些循环获得结果.. – senthilkumar 2017-09-08 09:53:32

MySQL 5.6(LAMP)。 column_value是您想要添加的列。 table_name是表格。

方法#1

$qry = "SELECT column_value AS count 
     FROM table_name "; 

$res = $db->query($qry); 

$total = 0; 
while ($rec = $db->fetchAssoc($res)) { 
    $total += $rec['count']; 
} 
echo "Total: " . $total . "\n"; 

方法#2

$qry = "SELECT SUM(column_value) AS count 
     FROM table_name "; 

$res = $db->query($qry); 

$total = 0; 
$rec = $db->fetchAssoc($res); 
$total = $rec['count']; 

echo "Total: " . $total . "\n"; 

方法#3 -SQLi

$qry = "SELECT SUM(column_value) AS count 
     FROM table_name "; 

$res = $conn->query($sql); 

$total = 0; 
$rec = row = $res->fetch_assoc(); 
$total = $rec['count']; 

echo "Total: " . $total . "\n"; 

方法#4:折旧(不使用)

$res = mysql_query('SELECT SUM(column_value) AS count FROM table_name'); 
$row = mysql_fetch_assoc($res); 
$sum = $row['count'];