Shadowing Posted January 1, 2012 Share Posted January 1, 2012 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; Quote Link to comment https://forums.phpfreaks.com/topic/254160-using-sum-for-the-first-time/ Share on other sites More sharing options...
Shadowing Posted January 1, 2012 Author Share Posted January 1, 2012 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; Quote Link to comment https://forums.phpfreaks.com/topic/254160-using-sum-for-the-first-time/#findComment-1303026 Share on other sites More sharing options...
floridaflatlander Posted January 1, 2012 Share Posted January 1, 2012 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']; } Quote Link to comment https://forums.phpfreaks.com/topic/254160-using-sum-for-the-first-time/#findComment-1303029 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.