Rickstar
Rickstar

Reputation: 6189

trying to show a total from mysql in php

I am trying to get the qunt with are int and add them all up and show a total

$qunt = 0;
$result = mysql_query("SELECT * 
                         FROM properties_items 
                        WHERE user_propid='$view' 
                     ORDER BY id DESC") or die (mysql_error()); 

while ($row = mysql_fetch_array($result)) { 
  $itemid = $row['itemid'];
  $qunt = $row['qunt'];

  $qunt++;
}

echo $qunt;

Upvotes: 0

Views: 117

Answers (2)

CSharper
CSharper

Reputation: 236

$qunt = 0; 
$result = mysql_query("SELECT *  
                         FROM properties_items  
                        WHERE user_propid='$view'  
                     ORDER BY id DESC") or die (mysql_error());  

while ($row = mysql_fetch_array($result)) {  
  $itemid = $row['itemid']; 
  $qunt += $row['qunt'];  
} 

echo $qunt;

Dare I say that you probably aren't using the itemid, in which case there's no point in looping through a result set in code. Instead, try something like this:

$qunt = mysql_query "SELECT SUM(qunt) FROM properties_items WHERE user_propid='$view'";

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

$qunt += $row['qunt'];

And get rid of the ++ line.

Upvotes: 0

Related Questions