Hello, If I want to print the total number of users that register in my website (at the top of the website), do I need to calculate the number in every page that user goes? mysql_query("SELECT id FROM `users` "); echo mysql_num_rows($query); is there any better way to keep this number and print it instand of calculte it every time (every page)? Roi.
Yes, you should store this number in a file on your web server, and just update it every hour or more. Like in pseudo code: if (file is older than 1 hour) then: mysql query and write the result into the file output the result in html else: read the file output the result in html end if I believe reading a file is less ressource hungry than running the mysql query, but maybe things have changed, I don't know.
or you could run a query like this $sql = 'SELECT COUNT(*) AS `total` FROM `users`'; $query = mysql_query($sql); $result = mysql_fetch_assoc($query); echo 'Total number of users: '.$result['total']; PHP: let the database do the counting. it is a lot quicker and less data that is returned that doing it your way. Also storing it in a temp file is a rubbish idea. You still end up having to read a file etc and I a database is a lot quicker.
For small databases it's not much of an issue, me personally I would create a table for totals and every time a user is added/deleted just updated the amount stored in the column.
Why not just create a function for it and then simply call the function out onto the page? I also agree - it's easier to do a simple query to get the rows then using a temp file. Now there is something else to consider as well. The number of 'active' versus inactive users on your site - if you are in fact keeping that data. So if you truly wanted 'active' users you would have to use something like: $sql = "SELECT * FROM users WHERE active = '1'"; $res = mysql_query($sql); $num = mysql_num_rows($res); That would give you the active users for the site that you could put into a function and call anywhere.
This is the approach usually used in most forum softwares. A "total's" table only needs to be updated when something is added/removed, a fairly infrequent operation... and then you get a small query on a small table with a small results set whenever you want to show that number, a far, far, far more frequent occurrence. Blindly going after the table to sum the totals every time you want to show it is just going to be slow and drag server performance down... HOWEVER, it is possible for the 'totals' table to end up out of sync, so toss in a admin function to 'recalculate all totals' just in case.
creating temp file or new small table force me to manage them - update the, all the time. calcule the total users force the DB to run all over the table and sum the total rows (happened every time that user click on new page). I thought that there is a better solution for that...:/
I think it will be better if you use If you want a "realtime update", you have to run that query again and again per each page reload. If not, you should set it to a session then set a delay to auto refresh it.
Notice about COUNT: Better user COUNT(*) but not COUNT(val). If you use COUNT(val) and this field have some NULL values this values will be ignored as i remember. COUNT(*) it's ok, and this query will be cached as i remember.