Why do some mySQL queries return a Resource ID#??
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 posted this at 19:46 — 25th April 2007.
She has: 11,421 posts
Joined: Jun 1999
Nevermind, I think I figured it out. Wasn't using it right (my brain is in no condition to be doing programming today!)
demonhale posted this at 01:23 — 26th April 2007.
He has: 3,278 posts
Joined: May 2005
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...
benf posted this at 22:32 — 27th April 2007.
They have: 426 posts
Joined: Feb 2005
What was the problem because im getting the same problem?
kb posted this at 23:55 — 27th April 2007.
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 posted this at 01:40 — 28th April 2007.
He has: 1,502 posts
Joined: Sep 2006
That looks right.
kb posted this at 03:06 — 28th April 2007.
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.
pr0gr4mm3r posted this at 03:37 — 28th April 2007.
He has: 1,502 posts
Joined: Sep 2006
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 posted this at 14:33 — 28th April 2007.
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.