Jump to content

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

}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.