Skip to main content
Participating Frequently
November 3, 2008
Answered

Resource id error

  • November 3, 2008
  • 2 replies
  • 381 views
Hi i am wanting to get the average of a record set, I have the following query

<?php
$sum = mysql_query("SELECT AVG(Rating) FROM Project_1_Rate");
?>

and then the following display

<?php echo $sum ?>

When i test it i get the following display

Resource id #8

I have never seen this before, the code seems right but if anyone has suggestions then they will be much appreciated.

This topic has been closed for replies.
Correct answer Newsgroup_User
meesonator wrote:
> I have never seen this before, the code seems right but if anyone has
> suggestions then they will be much appreciated.

The first part of the code is right, but $sum is not the result of the
query; it's the result resource returned by mysql_query(). You need to
extract the data you require from it.

$sum = mysql_query("SELECT AVG(Rating) FROM Project_1_Rate");
$row = mysql_fetch_row($sum);
echo $row[0];

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/

2 replies

Participating Frequently
November 3, 2008
Works Great.
Cheers David!
Newsgroup_UserCorrect answer
Inspiring
November 3, 2008
meesonator wrote:
> I have never seen this before, the code seems right but if anyone has
> suggestions then they will be much appreciated.

The first part of the code is right, but $sum is not the result of the
query; it's the result resource returned by mysql_query(). You need to
extract the data you require from it.

$sum = mysql_query("SELECT AVG(Rating) FROM Project_1_Rate");
$row = mysql_fetch_row($sum);
echo $row[0];

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/