SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast marc_2ky's Avatar
    Join Date
    Apr 2004
    Location
    Glasgow, Scotland
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Displaying how many rows in a table

    Hi, i have been racking my brains on how to display how many rows are within a table.

    I have done something similar in the past but I can't remember how.

    If you know how to do this please let me know.

    Thanks
    Marc
    I am a learner, be gentle!

  2. #2
    SitePoint Enthusiast marc_2ky's Avatar
    Join Date
    Apr 2004
    Location
    Glasgow, Scotland
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry, forgot to mention, say there is 10 rows, do i need to use a print command to display this within a php page?

    Thanks again
    Marc
    I am a learner, be gentle!

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    after your query use
    PHP Code:
    $row_count mysql_num_rows($query);
    #where $query is your query!
    echo $row_count
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    SitePoint Enthusiast marc_2ky's Avatar
    Join Date
    Apr 2004
    Location
    Glasgow, Scotland
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    would the query be the table name i want to show?
    I am a learner, be gentle!

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    No this would appear after your query...
    PHP Code:
    $query mysql_query("select * from mytable");
    $row_count mysql_num_rows($query); 
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    SitePoint Enthusiast marc_2ky's Avatar
    Join Date
    Apr 2004
    Location
    Glasgow, Scotland
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    duh! am such a tool.

    Thanks for your help spikez!
    I am a learner, be gentle!

  7. #7
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Rather than select back all of the data if you all you want is a total you can do it like this:

    PHP Code:
    $result mysql_query("SELECT COUNT(*) AS total FROM tablename");
    $total mysql_result($result0'total'); 
    spikeZ method does of course work too! But if you have a lot of data in your table it'll take a while to select and return it all when you don't really need to.

    Cheers,

    Rich
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.

  8. #8
    SitePoint Enthusiast marc_2ky's Avatar
    Join Date
    Apr 2004
    Location
    Glasgow, Scotland
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Rich, will try this method too.

    Marc
    I am a learner, be gentle!

  9. #9
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Very true Rich but if you are already doing the query, eg
    PHP Code:
    $query mysql_query("select id, name from table_names where id='3' AND name='Dave'"); 
    Using mysql_num_rows gives you that total instead of doing another query....
    Just another way of doing it!

    S
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  10. #10
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Absolutely - there was just no indication he was already doing a query num_rows is fine but it doesn't return how many rows are in his table, only that which was actually selected, which can be quite a different thing! (I know you know this anyway, just making it clear for the OP).
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.


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
  •