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 Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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']; } Quote Link to comment 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.