Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Resource id error

Explorer ,
Nov 03, 2008 Nov 03, 2008

Copy link to clipboard

Copied

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.

TOPICS
Server side applications

Views

345
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Nov 03, 2008 Nov 03, 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 Guid...

Votes

Translate
LEGEND ,
Nov 03, 2008 Nov 03, 2008

Copy link to clipboard

Copied

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/

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 03, 2008 Nov 03, 2008

Copy link to clipboard

Copied

LATEST
Works Great.
Cheers David!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines