I built an interactive website using a mysql database and php, and I need a quick and easy way of calculating the total number of users I have registered.
Since this number is approaching 2000, I'm looking for a command that will return the number of records in a specified table.
$sqlc = mysql_query("select * from users");
$carr = mysql_num_rows($sqlc);
echo "there are $carr users total";
Thanks tim...
However, won't this eat up a lot of memory/cpu cycles? Let's say I have 10,000 records in the user table, wouldn't the space necessary to store an array with 10,000 records be a lot? If so, is this the only way? I was thinking that perhaps the mysql database stores some sort of fixed latent variable that always records the total number of records within each table, and that there is a function that accesses this value, without having to query and return the entire table's contents... ?
I used that script on a user database counting over 2k users - never had a slow load issue with it.
its late here and i'm running on lil sleep as is, so my other solution (whether its the best or not) would be to add a auto_increment column in your SQL table.
edit: the mysql_num_rows does not return an array, it returned form is a numeric value of the amount of rows of the mysql_query (using the SELECT command only.) I really suggest you try that.
Last edited by tim on Wed Apr 07, 2004 10:20 pm, edited 1 time in total.
Actually, I'll stick with your original solution... the reason why I'm counting members this way as opposed to implementing an auto increment function each time someone registers, is that there are too many opportunities for the number to get off track (deleted members, bugs and what not)
... so doing it this way saves me the energy of implementing a membership count mechanism.
Thanks for the advice, I have implemented your original solution and it seems to be working without any change in speed.
If you just want to know how many records you have COUNT() is probably, as kfiralfia said, the better solution. If you use a normal SELECT and then mysql_num_rows() using
COUNT() counts how many records there are - I think you might be thinking of MAX() which would give you the highest ID value, so COUNT() will be fine no matter how many records are added or deleted
twigletmac wrote:COUNT() counts how many records there are - I think you might be thinking of MAX() which would give you the highest ID value, so COUNT() will be fine no matter how many records are added or deleted
Mac
ahh, I was thinking MAX... oh well, late nights arent good for my brain. excellent job twig.