Jump to content

using SUM for the first time


Shadowing

Recommended Posts

Im having a problem putting my SQL statement into a query

I tested this SQL statement and it works perfectly but im getting blank result when its in this query. Im guesing im not selecting what I want out of the query correctly. hopefully someone can help me with this.

 

 SELECT SUM(money) FROM `systems` WHERE id = 3112 

 

This query is suppose to add all the money for all the rows that equal the id

 

 $sql = "SELECT SUM(money) FROM systems WHERE id='".($_SESSION['user_id'])."'"; 
	                $query = mysql_query($sql) or die(mysql_error());

                                                 $total = $query['money'];

                echo $total; 

Link to comment
https://forums.phpfreaks.com/topic/254160-using-sum-for-the-first-time/
Share on other sites

I figured it out!!!

 

why did i have to use a loop with php but not when typing it in to SQL?

 

		$querys = "SELECT SUM(money) FROM systems WHERE id='".($_SESSION['user_id'])."'"; 
		$results = mysql_query($querys) or die(mysql_error());
	while($row = mysql_fetch_array($results)){

		$total = $row['SUM(money)'];

			}		

echo $total;

As a note and I'm not sure if this correct but $q = SELECT SUM(money) AS total FROM `systems` WHERE id = 3112

 

$results = mysql_query($q) or die(mysql_error());

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

  $total = $row['total'];

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.