SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2002
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I print the number of rows?

    I want php to print the number of rows in my database. I've read several threads on it, and none of them agree on the best way to do it. I have tried several of their suggestions, but I haven't been able to get any of them to work with my version of php. Last year I had a function that worked, but I seem to have forgotten part of it.

    Here is part of what I had.

    PHP Code:
    echo 'There are $num_rows in the database'
    There must be something missing, because that no longer works. And I am told the best method for counting rows is the Select Count method. But I can't figure out how to get it to work.

    Can someone help me write a complete function that would work just as the example above did? The tablename is dvd. I'd like to keep it to as few lines as possible.

  2. #2
    SitePoint Member
    Join Date
    Jan 2005
    Location
    San Francisco
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It sounds like you are looking to get a count of all the rows in an individual table. The SQL for that is:

    SELECT COUNT(*) FROM your_table_name;

    See: http://www.webdevelopersnotes.com/tu..._counting.php3

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    try
    PHP Code:
    $query mysql_query("select * from dvd") or die(mysql_error());
    $num_rows mysql_num_rows($query);
    echo 
    $num_rows
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    SitePoint Addict trogdor1024's Avatar
    Join Date
    Oct 2004
    Location
    New Jersey
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Might want to use ...
    Code:
    SELECT field1 FROM dvd
    (rather than SELECT *) Since you are only interested in the count, why load up the whole table rather than as little as possible. I *believe* this would gain you efficiency depending on how large your table is and how tasked the server is for CPU cycles.

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by trogdor1024
    Might want to use ...
    Code:
    SELECT field1 FROM dvd
    (rather than SELECT *) Since you are only interested in the count, why load up the whole table rather than as little as possible. I *believe* this would gain you efficiency depending on how large your table is and how tasked the server is for CPU cycles.
    This is very true, I only used * as an example!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    SitePoint Evangelist
    Join Date
    Jan 2002
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks spikeZ. Your method worked beautifully.

  7. #7
    SitePoint Zealot johno's Avatar
    Join Date
    Sep 2003
    Location
    Bratislava, Slovakia
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SPIKEZ: A little more explanation. If you are using "SELECT * FROM table" or "SELECT field1 FROM table" the overhead is just amazing, because database server has to go through all rows to count them. If you are using MyISAM tables the row count is stored in internally so SELECT COUNT(*) FROM table is very very fast.
    Annotations support for PHP5
    TC/OPT™ Group Leader


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
  •