Why do some mySQL queries return a Resource ID#??

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

I have this query:

$totalsubmissions = mysql_query("SELECT COUNT(*) FROM survey_usability");'

When I echo back the value of $totalsubmissions it just says "Resource id #5". I've seen that happen on other queries in the past -- why?? What am I doing wrong? I'm just starting to write this script to return some results from a survey.

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

Nevermind, I think I figured it out. Wasn't using it right Doh! (my brain is in no condition to be doing programming today!)

demonhale's picture

He has: 3,278 posts

Joined: May 2005

Megan;218377 wrote: Nevermind, I think I figured it out. Wasn't using it right Doh! (my brain is in no condition to be doing programming today!)

Mine too... This week I might be experiencing a programmers block... add to that, that I officially have an RSI on my right hand for too much mousing around... PT's do have cures for those actually, but I still have it though...

They have: 426 posts

Joined: Feb 2005

What was the problem because im getting the same problem?

He has: 1,380 posts

Joined: Feb 2002

If you have a query like that, you need to "structuralize" (I'm not sure what the official word is for it) the data.

For example:

$sql = mysql_query("SELECT name, email FROM wherever LIMIT 5");

while ($qry = mysql_fetch_array($sql)) {
     $actual_name = $qry['name'];  // actual name data for this row
     $actual_email = $qry['email'];   // actual email data for this row
}
'

Again, I'm not sure of the exact reasons, but I think it's similar in method to 'pointers' in C. It returns the location of what you're looking for, and then you have to pull the actual data out of that memory location.

(Anyone want to verify this?)

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

brady.k;218479 wrote: If you have a query like that, you need to "structuralize" (I'm not sure what the official word is for it) the data.

For example:

$sql = mysql_query("SELECT name, email FROM wherever LIMIT 5");

while ($qry = mysql_fetch_array($sql)) {
     $actual_name = $qry['name'];  // actual name data for this row
     $actual_email = $qry['email'];   // actual email data for this row
}
'

Again, I'm not sure of the exact reasons, but I think it's similar in method to 'pointers' in C. It returns the location of what you're looking for, and then you have to pull the actual data out of that memory location.

(Anyone want to verify this?)

That looks right.

He has: 1,380 posts

Joined: Feb 2002

Haha I meant verify my reasons as to why you have to do it that way. But ok. Wink

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

brady.k;218489 wrote: Haha I meant verify my reasons as to why you have to do it that way. But ok. Wink

Well, Megan was printing the $sql variable like it was a string. In this case, $sql was a Result Set. That could be similar to a variable that references a database connection or file stream. You can't print it (or if you do, weird output will result), you have to send it through methods (such as mysql_fetch_row, mysql_fetch_assoc, mysql_fetch_array, etc).

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

Thanks for explaining that! It makes much more sense now.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.