raziel2101
raziel2101

Reputation: 13

PHP - Getting values from while loop

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

Upvotes: 1

Views: 3444

Answers (7)

Memolition
Memolition

Reputation: 494

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'];
    }
}

Upvotes: 0

jszobody
jszobody

Reputation: 28911

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.

Upvotes: 4

M8R-1jmw5r
M8R-1jmw5r

Reputation: 4996

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.

Upvotes: 3

parnas
parnas

Reputation: 721

<?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;

?>

Upvotes: 0

DobotJr
DobotJr

Reputation: 4049

<?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;


  ?>

Upvotes: 0

Daryl Gill
Daryl Gill

Reputation: 5524

   $Number_Variable = 0;
  while ($row = mysql_fetch_array($sql)){

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

  }

Then after you have finished your while:

echo $Number_Variable;

Upvotes: 0

Class
Class

Reputation: 3160

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;

Upvotes: 0

Related Questions