Jump to content

Problem displaying results of query in browser: involves mysql_fetch_array


stevieontario

Recommended Posts

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&#09;$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

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";

Archived

This topic is now archived and is closed to further replies.

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