<?php
$query=mysql_query("SELECT * FROM vph");
while($array=mysql_fetch_array($query)){
echo $a= $array['vph_weight'];// $a is like this 1,2,3,2,5,,,,,..
}
?>
I want to add $a
values. Please help to add those value. I am new in PHP.
<?php
$query=mysql_query("SELECT * FROM vph");
while($array=mysql_fetch_array($query)){
echo $a= $array['vph_weight'];// $a is like this 1,2,3,2,5,,,,,..
}
?>
I want to add $a
values. Please help to add those value. I am new in PHP.
You could simply rewrite your query as below instead of doing a separate array_sum()
operation overhead !
SELECT SUM(vph_weight) AS vphsum FROM vph
$query=mysql_query("SELECT SUM(vph_weight) AS vphsum FROM vph");
while($array=mysql_fetch_array($query)){
echo $array['vphsum'];
}
This (mysql_*
) extension is deprecated as of PHP 5.5.0
, and will be removed in the future. Instead, Prepared Statements of MySQLi
or PDO_MySQL
extension should be used to ward off SQL Injection attacks !