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![]()
| SitePoint Sponsor |
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!
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!


after your query use
PHP Code:$row_count = mysql_num_rows($query);
#where $query is your query!
echo $row_count;
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!
would the query be the table name i want to show?
I am a learner, be gentle!


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 Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!
duh! am such a tool.
Thanks for your help spikez!
I am a learner, be gentle!


Rather than select back all of the data if you all you want is a total you can do it like this:
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.PHP Code:$result = mysql_query("SELECT COUNT(*) AS total FROM tablename");
$total = mysql_result($result, 0, 'total');
Cheers,
Rich
Thanks Rich, will try this method too.
Marc![]()
I am a learner, be gentle!


Very true Rich but if you are already doing the query, eg
Using mysql_num_rows gives you that total instead of doing another query....PHP Code:$query = mysql_query("select id, name from table_names where id='3' AND name='Dave'");
Just another way of doing it!
S
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!


Absolutely - there was just no indication he was already doing a querynum_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).
Bookmarks