Jump to content

Problem displaying results of query in browser: involves mysql_fetch_array


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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