PDA

View Full Version : Why mysql_result()?


zetetic
06-14-2005, 02:55 PM
Last night I was working on making it possible to display the total smilie count on forumhome, and I came up with this code:

$totalsmilies = $DB_site->query("SELECT COUNT(*) FROM smilie");

But when I put $totalsmilies on FORUMHOME I got: "Resource id #62" instead of the number of smilies.

Eventually, I figured out that I had to change it to this...

$totalsmilies = $DB_site->query("SELECT COUNT(*) FROM smilie");
$totalsmilies = mysql_result($totalsmilies,0);

...for it to work. Why did I have to do that and is there a better way?

Marco van Herwaarden
06-14-2005, 02:58 PM
Change to the following:
$totalsmilies = $DB_site->query_first("SELECT COUNT(*) AS count FROM smilie");

Then use $totalsmilies[count] in your template.


And the answer to the "why" question is simple. $DB_site->query will only prepair a cursor to a query statement, it will not retrieve any records. You will either have to build a loop to retrieve all records after that, or use query_first.

zetetic
06-14-2005, 03:00 PM
Ah, I see. Thanks Marco. :)