mwl707 Posted May 7, 2009 Share Posted May 7, 2009 Hi Can anyone help please . I am trying to write a function. The code is passed a variable which is used in a mysql query. (Code below) In the second to last line I always get 0 as a result when I know the result should be 3. If i use the following code I get the correct result $screen_size = "i100s" ; $result = mysql_query("SELECT SUM($screen_size) FROM event WHERE jobstatus = 'confirmed' ") ; while($row = mysql_fetch_array($result)){ $res = $row['SUM($screen_size)'] ; echo "Total ". $row['jobstatus']. " = ". $res ; echo "<br />"; In the second to last line I always get 0 as a result when I know the result should be 3. If i use the following code I get the correct result $screen_size = "i100s" ; $result = mysql_query("SELECT SUM($screen_size) FROM event WHERE jobstatus = 'confirmed' ") ; while($row = mysql_fetch_array($result)){ $res = $row['SUM(i100s)'] ; echo "Total ". $row['jobstatus']. " = ". $res ; echo "<br />"; Can anyone offer some advice ? Thanks Mick Link to comment https://forums.phpfreaks.com/topic/157281-solved-aggregate-functions/ Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 What is i100s? Is that a column name? Link to comment https://forums.phpfreaks.com/topic/157281-solved-aggregate-functions/#findComment-828957 Share on other sites More sharing options...
DarkWater Posted May 7, 2009 Share Posted May 7, 2009 Doesn't matter. Probably is. The problem is that you're using single quotes (' ') around your array key ($row['SUM($screen_size)']) so variables don't interpolate. Use double quotes. Link to comment https://forums.phpfreaks.com/topic/157281-solved-aggregate-functions/#findComment-829027 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 Doesn't matter. Probably is. The problem is that you're using single quotes (' ') around your array key ($row['SUM($screen_size)']) so variables don't interpolate. Use double quotes. That's not it. You can't run SUM outside of a query. o.O Well, unless it's a function. Link to comment https://forums.phpfreaks.com/topic/157281-solved-aggregate-functions/#findComment-829030 Share on other sites More sharing options...
DarkWater Posted May 7, 2009 Share Posted May 7, 2009 If it's in quotation marks, it wouldn't matter. <?php echo "substr('test', 0, 10);"; He could (and probably should) be using an alias in the query, but in reality, it doesn't matter. Just more typing if he doesn't. xD Link to comment https://forums.phpfreaks.com/topic/157281-solved-aggregate-functions/#findComment-829033 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 Well no, if $screen_size is not a table column, then returning 0 makes sense. Link to comment https://forums.phpfreaks.com/topic/157281-solved-aggregate-functions/#findComment-829035 Share on other sites More sharing options...
mwl707 Posted May 8, 2009 Author Share Posted May 8, 2009 Ken2k7 Sorry for being vague, Yes i100s is a column in my table called event Thanks for your help Link to comment https://forums.phpfreaks.com/topic/157281-solved-aggregate-functions/#findComment-829583 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Try this $result = mysql_query("SELECT SUM(`i100s`) AS `sum` FROM event WHERE jobstatus = 'confirmed'"); while($row = mysql_fetch_assoc($result)) { echo $result['sum']; } Link to comment https://forums.phpfreaks.com/topic/157281-solved-aggregate-functions/#findComment-829592 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.