SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot Jppr's Avatar
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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>

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe the correct syntax for the mysql count is

    $result=mysql_query("select * from article where auid=5");
    $num=mysql_num_rows($result);


  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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"

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Zealot Jppr's Avatar
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why does count(*) needs an alias like "as blabla" ??

    </jppr>

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    SitePoint Zealot Jppr's Avatar
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  9. #9
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •