SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot Jppr's Avatar
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    how come that when I use the count() function in php to cound the number of rows in an array, that it always counts one too many?

    What is best: mysql_num_rows() or count() ?

    (I will use it for mySql results)

    Thomas

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    count() returns the number of elements in an array but array indexes start at 0 by default so an array with a count of 10 would have indexes 0 - 9. I suspect that is why you are getting one more than you thought use mysql_num_rows() for the number of rows returned by a query. Hope that helps.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Zealot Jppr's Avatar
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm planning to do such stuff with mysql_num_rows() from now on.

    But what I uses count() for, was the following:
    I wanted to check how many articles a particular author submitted. So I did the next:

    # Count the number of articles of this author
    $na = mysql_query("SELECT aid FROM article WHERE auid=$id");$na = mysql_fetch_array($na);
    $na = count($na);

    But then $na always gave one too many, i.e. when an author didn't submitted anything yet, $na would be 1, one articles would result in $na == 2, etc.

    What's the cause of that? Does it have something to do with indexes?

    Thomas

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No its because you assigned $na to the query so if the query executed okay then $na would have the value of 1 even if no rows returned but if the query returned a row then you assigned $na to mysql_fetch(array) so that is why you got a different number you probably should have structured it like so:
    $na = mysql_query("SELECT aid FROM article WHERE auid=$id");
    if (mysql_num_rows($na) > 0) {
    $row = mysql_fetch_array($na);
    }
    else {
    print "No articles found";
    }
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Zealot Jppr's Avatar
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I see.

  6. #6
    SitePoint Member
    Join Date
    Dec 2000
    Location
    Poland
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why don't you use this ?

    $res=mysql_query("SELECT COUNT(*) as na FROM article WHERE auid=$id GROUP BY auid");
    $na=mysql_fetch_array($res);
    $na=$na["na"];

    This query can be optimized by MySQL (eg. where the auid column is indexed) and only one value (one row) is returned.

    Chris

  7. #7
    SitePoint Zealot Jppr's Avatar
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Won't it be easier if I just did the code I had, then minus one?

    Maybe I'll try asdn's code anyway.

    Thanks,
    T

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not really because you code is wrong and if you have more than a few articles it wouold really get screwed up, by just minusing one is a hack and should never be used, do it thr right way first
    This would be the proper way

    $result = mysql_query("SELECT aid FROM article WHERE auid=$id");
    $na = mysql_num_rows($result) {


    Also with your orig code by counting the $na you are merely counting the number of elements returned in the mysql_fetch_array() which would be the number of fields anyway not the number of records returned. Also with asdn you get no data returned don't you want to get the aid of the record returned without having to run another query?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    SitePoint Member
    Join Date
    Dec 2000
    Location
    Poland
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jppr,

    What the code you should choose depends on what you are trying to do.
    If you want to extract data about articles, which belong to a partcular author, and the number of records is used only for an auxilary purpose (eg. breaking listing into pages or display their total number above articles) you should go with freddy's code.
    If you want to display data related only to authors (eg. statics articles per authors) you should go with my code, but remove the where clause to get the number of articles for each author in one query = "SELECT count(*) as na from articles group by auid". You can use joins for getting the names and other details about authors in the same query.
    But if you want to extract only number of articles for particular author (for unknown to me purpose) I recomend my code as it is at least 2x faster on indexed field (1.5x on non indexed) and even better if the number of articles for the author (number of returned rows) grows. Of course on my Linux box (RH6+Apache+PHP3+MySQL, all are not the newest versions).

    Chris

  10. #10
    SitePoint Zealot Jppr's Avatar
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's just to display how many articles a particular author submitted, so I will use mysql_num_rows. That's the shortest and most logical method.

    Thanks everybody for the replies!
    T


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
  •