jaymc Posted April 24, 2007 Share Posted April 24, 2007 I want to count how many rows in a database match a members username Whats the best way At current I basically have $q = "SELECT `id` FROM `table` WHERE `username` = 'fred'"; $num = mysql_num_rows($q); Is that the fastest and less intensive way to count? Quote Link to comment https://forums.phpfreaks.com/topic/48524-fastest-way/ Share on other sites More sharing options...
chombone Posted April 24, 2007 Share Posted April 24, 2007 try $querystring = "SELECT COUNT(*) FROM table WHERE username = 'fred'"; $total_records = mysql_result(mysql_query($querystring,$connection),0); Quote Link to comment https://forums.phpfreaks.com/topic/48524-fastest-way/#findComment-237431 Share on other sites More sharing options...
jaymc Posted April 24, 2007 Author Share Posted April 24, 2007 Is that know to be the best/fastest way to achieve a count of rows? Quote Link to comment https://forums.phpfreaks.com/topic/48524-fastest-way/#findComment-237448 Share on other sites More sharing options...
jaymc Posted April 24, 2007 Author Share Posted April 24, 2007 What is $connection by the way? Quote Link to comment https://forums.phpfreaks.com/topic/48524-fastest-way/#findComment-237455 Share on other sites More sharing options...
chombone Posted April 24, 2007 Share Posted April 24, 2007 $connection is the variable I use as a handle for the database connection e.g. $connection = mysql_connect("localhost","username","password") or die ("Could not connect: " . mysql_error()); mysql_select_db("databasename",$connection) or die ("Could not select db: " . mysql_error()); I don't know if COUNT is quicker, but the easy way to find out is create a large table and do some timings. It's certainly more efficient in Oracle. Quote Link to comment https://forums.phpfreaks.com/topic/48524-fastest-way/#findComment-237465 Share on other sites More sharing options...
jaymc Posted April 25, 2007 Author Share Posted April 25, 2007 What does the * represent? Is it always * Quote Link to comment https://forums.phpfreaks.com/topic/48524-fastest-way/#findComment-238286 Share on other sites More sharing options...
leela Posted April 25, 2007 Share Posted April 25, 2007 Hello sir, how to post questions thru my id???????? thanks Quote Link to comment https://forums.phpfreaks.com/topic/48524-fastest-way/#findComment-238388 Share on other sites More sharing options...
Guest Posted April 25, 2007 Share Posted April 25, 2007 Instead of $querystring = "SELECT COUNT(*) FROM table WHERE username = 'fred'"; You may prefer to use $querystring = "SELECT COUNT(id) FROM table WHERE username = 'fred'"; Assuming id is a primary key. If you want to go farther, add an index on username. This would be optimal, I believe. The difference isn't super profound right now (~0.0007ms on 300 records, ~0.0021ms on 3,000 records) but will be more appearant with a lot of records. Quote Link to comment https://forums.phpfreaks.com/topic/48524-fastest-way/#findComment-238418 Share on other sites More sharing options...
fenway Posted April 26, 2007 Share Posted April 26, 2007 Definely the index on username! Quote Link to comment https://forums.phpfreaks.com/topic/48524-fastest-way/#findComment-238653 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.