SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Counting MySQL Records

    Hello everybody,

    I have a website that has a lot of articles for webmasters, and I want to be able to print out that a given category has x number of records. How would I do this?

    Here's an example: I have a Category_ID = 3. How could I have PHP search the database for any article that has Category_ID = 3 and then display the number of records that it found? I don't want the records to display, just the number of records.

    Any help would be greatly appreciated.
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  2. #2
    :) delemtri's Avatar
    Join Date
    Jun 2001
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $result mysql_query("select Category_ID from table where Category_ID='3'"); 
    $blah mysql_num_rows($result); 
    echo 
    "There are $blah rows."

  3. #3
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you can use
    $x=mysql_affected_rows();

  4. #4
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oliver and slighltywhacked,

    Thank you very much!
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  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)
    A couple of things.

    mysql_affected_rows() will not work, it is used for UPDATE, INSERT and DELETE statements only.

    You can get the count for all categories using a query with GROUP by and COUNT()

    PHP Code:
    $result mysql_query("select count(*), Category_ID from table GROUP BY Category_ID"); 
    That would return each Category_ID and how many records have that Category_ID.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Freddy,

    Thanks a lot! I was trying to figure out how to do that just as you replied!
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  7. #7
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    How would I turn this into a script though?
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  8. #8
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In a script, it'll be something like:

    PHP Code:
    $sql mysql_query("SELECT count(*) as num, Category_ID from $table WHERE Category_ID='3'");

    $row mysql_fetch_array($sql);
    extract($row);

    echo 
    "Category $Category_ID has $num articles"

  9. #9
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Robo,

    Thank you very much!
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!


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
  •