1

Sorry for asking a simple question.

Im still new to php, so im still learning.

For example using the while($row = mysql_fetch_assoc($sql)), I queried and echoed a single column name "weight", after 4 results, each $row['weight'] has values of 30,15,20,35. How can I fetch these values and perform addition to get the total value of the weight.

<?php

  $sql = mysql_query("SELECT weight FROM table WHERE ID = '$ID'");

  while ($row = mysql_fetch_assoc($sql)){

  echo $row['weight'];

  ?>

Sample output:

30
15
20
35

I want to perform ADDITION.

30+15+20+35=100

raziel2101
  • 13
  • 3
  • 8
  • as mysql_query calls are being deprecated with mysqli, I would suggest you take a look at mysqli (as it will be removed at some point, you may end up with non working code in the future). That said, the easiest is to sum it straight in mysql select sum(weight) instead of select weight – Youn Elan Apr 30 '13 at 02:29

7 Answers7

4

To to this in PHP just add each weight to a $total variable inside your loop:

$sql = mysql_query("SELECT weight FROM table WHERE ID = '$ID'");
$total = 0;
while ($row = mysql_fetch_assoc($sql)){
    $total += $row['weight'];
}
echo $total;

However you may also want to consider doing it directly in sql:

$sql = mysql_query("SELECT SUM(weight) AS total FROM table WHERE ID = '$ID'");
$row = mysql_fetch_assoc($sql);
$total = $row['total'];

Also note that mysql functions are deprecated as of PHP 5.5 and will be removed removed in the future, you need to look at the mysqli_query or PDO::query instead.

jszobody
  • 28,495
  • 6
  • 61
  • 72
3

You let mysql calculate that for you:

SELECT SUM(weight) AS total FROM table WHERE ID = '$ID' GROUP BY ID;

Example:

  $sql = sprintf(
      'SELECT SUM(weight) AS total FROM table WHERE ID = '%d' GROUP BY ID', $ID
  );

  $result = mysql_query($sql);

  $total  = $result ? mysql_fetch_assoc($result)['total'] : 0;

But I dislike this example code because it uses the mysql_* functions and PDO is much better.

M8R-1jmw5r
  • 4,896
  • 2
  • 18
  • 26
0

I believe you are looking for something like:

$total = 0;#initialize
while ($row = mysql_fetch_array($sql)){
      $total += $row['weight'];#adds weight to the total
}
echo $total;
Class
  • 3,149
  • 3
  • 22
  • 31
0
   $Number_Variable = 0;
  while ($row = mysql_fetch_array($sql)){

  $Number_Variable += $row['weight'];

  }

Then after you have finished your while:

echo $Number_Variable;
Daryl Gill
  • 5,464
  • 9
  • 36
  • 69
0
<?php

  $sum = 0;
  $sql = mysql_query("SELECT weight FROM table WHERE ID = '$ID'");

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

   $sum = $sum + $row['weight']; 


  }

  echo $sum;


  ?>
DobotJr
  • 3,929
  • 9
  • 36
  • 49
0
<?php

  $sql = mysql_query("SELECT weight FROM table WHERE ID = '$ID'");

  $total = 0;
  while ($row = mysql_fetch_array($sql)){
    $total = $total + $row['weight'];
  }

    echo $total;

?>
parnas
  • 721
  • 5
  • 14
0

Now that you're learning start learning mysql/php with mysqli or pdo cause mysql_* functions are deprecated

$mysqli = new mysqli('server', 'database_user', 'database_pass', 'database_name');
if($mysqli->connect_errno) {
    echo "Error connecting to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

$my_query = $mysqli->query("SELECT SUM(weight) as total_weight FROM your_table");
if($my_query->num_rows){
    while($row = $my_query->fetch_assoc()){
       echo 'Total weight is: ' . $row['weight'];
    }
}
Community
  • 1
  • 1
Memolition
  • 492
  • 1
  • 5
  • 12