PHP Code to get Total Rows?

Discussion in 'PHP' started by amelen, Oct 24, 2007.

  1. #1
    Quick question.. What's the easiest way to get the total number of rows in a table/database? I want to have a php script that just displays the total rows. Something that isn't too resource intensive or inefficient (since it will be executed every few minutes to get updates).

    Thanks!
     
    amelen, Oct 24, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    $query = mysql_query("SELECT COUNT(*) AS total FROM table_name") OR die(mysql_error());
    $result = mysql_fetch_assoc($query);
    
    echo $result['total'];
    
    PHP:
     
    nico_swd, Oct 24, 2007 IP
  3. webrepair

    webrepair Peon

    Messages:
    41
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    mysql_num_rows() is an alternative to this.

    Regards,
     
    webrepair, Oct 24, 2007 IP
  4. amelen

    amelen Active Member

    Messages:
    686
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    85
    #4
    Thanks guys! Trying it out now..
     
    amelen, Oct 24, 2007 IP
  5. amelen

    amelen Active Member

    Messages:
    686
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    85
    #5
    Works like a charm! Now we can put up a live member count on our site. Thanks!
     
    amelen, Oct 24, 2007 IP
  6. amelen

    amelen Active Member

    Messages:
    686
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    85
    #6
    How resource intensive do you guys think this is? Can I run something like this every minute (or every 30 seconds) without any major impact?
     
    amelen, Oct 24, 2007 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    How many rows are in the table?
     
    nico_swd, Oct 24, 2007 IP
  8. amelen

    amelen Active Member

    Messages:
    686
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    85
    #8
    There are 500,000 rows in the table with about 500-1,000 added every day.
     
    amelen, Oct 24, 2007 IP
  9. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #9
    It shouldn't be expensive regardless of the table size. MySQL already "knows" how many rows are in a table. As long as you're counting all the rows without any where clauses it doesn't require any computation.
     
    jnestor, Oct 24, 2007 IP
  10. amelen

    amelen Active Member

    Messages:
    686
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    85
    #10
    Thanks Jnestor!
     
    amelen, Oct 24, 2007 IP
  11. nasium

    nasium Active Member

    Messages:
    114
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    90
    #11
    mysql_num_rows() is the best way to do this, however the first example is a good way as well.
     
    nasium, Oct 24, 2007 IP
  12. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #12
    actually, for simply counting the number of rows, using mysql_num_rows() would be a waste, because to use that you'd first need to SELECT all rows (therefore loading the data in all those rows to memory).

    'SELECT COUNT(*)' is the cheapest in terms of resources usage and is the most logical query to use.
     
    phper, Oct 24, 2007 IP
  13. junandya

    junandya Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #13
    How about this one:

    $yourQuery = mysql_query ("select * from tabel name", $connection) or die ("the error is:" . mysql_error());
    $numAllRow = mysql_num_rows ($yourQuery);

    echo "$numAllRow";


    Regards...
     
    junandya, Oct 24, 2007 IP
  14. amelen

    amelen Active Member

    Messages:
    686
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    85
    #14
    Thanks! Select Count(*) seems the fastest. I now have it on all the www.t35.com pages (the member count at the top). Thanks guys!
     
    amelen, Oct 25, 2007 IP
  15. shazaam42

    shazaam42 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    I have a related question, but dealing with related tables. I have 1 table for users, and another for votes. When someone votes for a user, an entry is made in the votes table with the user's ID.

    Is there an easy and elegant way to query which users have the 10 most votes and how many each of the top 10 got?

    I'm sure I could do some ugly, nested recursive search (look at the users table, and for each ID in the users table, count the number of votes, then build an array that keeps track of votes, etc.) but it's stupid and ugly and overhead intensive.

    Is there some way to query the votes table and count the number of rows for each id and return the results on the top 10? I feel like it's there, but my brain can't do it. HELP!
     
    shazaam42, Oct 30, 2007 IP
  16. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #16
    SELECT user_id, COUNT(*) AS n
    FROM vote
    GROUP BY user_id
    ORDER BY n DESC
    LIMIT 10;
    Code (markup):
     
    phper, Oct 30, 2007 IP
  17. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Its preferable to use count(primary_key) instead of count(*)

    MyISAM handles this OK but it can be crunch time if youre using InnoDB.

    Just a tip :)
     
    tonybogs, Oct 31, 2007 IP
  18. shazaam42

    shazaam42 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    GROUP BY!! :D

    I knew there was a way! Thank you, it works like a charm, and it's so simple.

    Life saver...
    -Caleb
     
    shazaam42, Oct 31, 2007 IP