stevieontario Posted January 8, 2010 Share Posted January 8, 2010 Afternoon PHPFreaks, I'm having a difficult time displaying the results of a mysql query. Here's the code: $sql = "SELECT fuel, sum( output ), avg(capabilityfactor) FROM performance\n CROSS JOIN\n (SELECT MAX(id) AS sourceid FROM sourceinfo) AS sq\n USING (sourceid)\n GROUP BY fuel\n ORDER BY sum( output ) DESC"; $result = mysql_query($sql, $cxn) or die ("fuggoff, cahsuh muhfuh"); while ( $row = mysql_fetch_assoc($result)) { extract($row); echo "<pre>$fuel: $output	$capabilityfactor</pre>"; } mysql_close($cxn); The browser displays only $fuel (five rows) in the correct order, but not $output or $capabilityfactor. When I do the query in phpMyAdmin, it runs just fine. This leads me to suspect I am using mysql_fetch_assoc() or extract() incorrectly, but I can't figure out what I'm doing wrong. Any suggestions would be much appreciated! Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/187763-problem-displaying-results-of-query-in-browser-involves-mysql_fetch_array/ Share on other sites More sharing options...
cags Posted January 8, 2010 Share Posted January 8, 2010 If you were to do a print_r on the $row array I think you might see your problem. Two of your fields are calculated fields that you haven't given an alias to, I'm guessing they are appearing in the array under keys 0 and 1 (at least not as output and capabilityfactor which is what your code assumes). Just a theory, I haven't tested. $sql = "SELECT fuel, sum( output ) AS output, avg(capabilityfactor) AS capabilityfactor FROM performance\n CROSS JOIN\n (SELECT MAX(id) AS sourceid FROM sourceinfo) AS sq\n USING (sourceid)\n GROUP BY fuel\n ORDER BY sum( output ) DESC"; Quote Link to comment https://forums.phpfreaks.com/topic/187763-problem-displaying-results-of-query-in-browser-involves-mysql_fetch_array/#findComment-991328 Share on other sites More sharing options...
stevieontario Posted January 8, 2010 Author Share Posted January 8, 2010 cags, thanks very much, that was exactly the problem. Everything is good now. I love this forum! thanks again Quote Link to comment https://forums.phpfreaks.com/topic/187763-problem-displaying-results-of-query-in-browser-involves-mysql_fetch_array/#findComment-991334 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.