Hi,
when I use the following code to count number of articles, I get "Resource id #2" as output. How come???
----
$num = mysql_query("SELECT COUNT(*) FROM article WHERE auid=5");
----
I don't get it. What's wrong?
Thanks
</jppr>
| SitePoint Sponsor |

Hi,
when I use the following code to count number of articles, I get "Resource id #2" as output. How come???
----
$num = mysql_query("SELECT COUNT(*) FROM article WHERE auid=5");
----
I don't get it. What's wrong?
Thanks
</jppr>



I believe the correct syntax for the mysql count is
$result=mysql_query("select * from article where auid=5");
$num=mysql_num_rows($result);





Actually the correct syntax would be
$num = mysql_query("SELECT COUNT(*) as totalnum FROM article");
$totalnum = mysql_result($num, 0);
Please don't PM me with questions.
Use the forums, that is what they are here for.



Okay, that is probably a better way of doing it, but the way I wrote seems to work okay.
I have never seen it written like that before.. but you know what people say, "you learn something new everday"![]()





It's just a matter of using less resource intensive functions whenever possible
Please don't PM me with questions.
Use the forums, that is what they are here for.

Why does count(*) needs an alias like "as blabla" ??
</jppr>





because its a function and the result of that function needs to be assigned to something how else would you go about getting the results
Please don't PM me with questions.
Use the forums, that is what they are here for.

I see.
I thought that when I did something like
$var = SELECT COUNT(*) blabla
that var would automatically get the result of count.
Thanks freddy





Or when getting the result, you could use:
$row = mysql_fetch_array($result)
then $counter = $row["COUNT(*)"]; // because COUNT(*) will be the field
- Son Nguyen
AdSpeed.com - Ad Serving and Ad Management Made Easy
Bookmarks