QuePID Posted May 18, 2011 Share Posted May 18, 2011 Hi, I have a database with roughly 73k rows (records). I am using a query string such as: SELECT DISTINCT Column FROM databasename I am then using mysql_num_rows($results) to count the number of distinct rows. This method works, but it is slow. Is there a faster way of doing this perhaps internal to the mysql without having to send the entire results to the PHP when I only need a count? Link to comment https://forums.phpfreaks.com/topic/236777-counting-select-distincts/ Share on other sites More sharing options...
fenway Posted May 18, 2011 Share Posted May 18, 2011 select count( distinct column ) from databasename Link to comment https://forums.phpfreaks.com/topic/236777-counting-select-distincts/#findComment-1217157 Share on other sites More sharing options...
QuePID Posted May 19, 2011 Author Share Posted May 19, 2011 select count( distinct column ) from databasename When I echo the results of your query string I get "Resource id #6", and when I use mysql_num_rows($results) I get a value of 1 which isn't correct (should be 224). What is the type of data returned in the results of the query? Link to comment https://forums.phpfreaks.com/topic/236777-counting-select-distincts/#findComment-1217356 Share on other sites More sharing options...
Pikachu2000 Posted May 19, 2011 Share Posted May 19, 2011 This is now a php issue . . . Moving thread to PHP Coding Help. Link to comment https://forums.phpfreaks.com/topic/236777-counting-select-distincts/#findComment-1217370 Share on other sites More sharing options...
Fadion Posted May 19, 2011 Share Posted May 19, 2011 Using fenway's query with PHP code: <?php $results = mysql_query("SELECT COUNT(DISTINCT column) AS myCount FROM table"); $values = mysql_fetch_assoc($results); echo $values['myCount']; ?> Link to comment https://forums.phpfreaks.com/topic/236777-counting-select-distincts/#findComment-1217377 Share on other sites More sharing options...
QuePID Posted May 19, 2011 Author Share Posted May 19, 2011 Using fenway's query with PHP code: <?php $results = mysql_query("SELECT COUNT(DISTINCT column) AS myCount FROM table"); $values = mysql_fetch_assoc($results); echo $values['myCount']; ?> Worked like a charm, and was quite fast. How would I do similar for a count of all records within the table? Link to comment https://forums.phpfreaks.com/topic/236777-counting-select-distincts/#findComment-1217475 Share on other sites More sharing options...
anupamsaha Posted May 19, 2011 Share Posted May 19, 2011 Worked like a charm, and was quite fast. How would I do similar for a count of all records within the table? <?php $results = mysql_query("SELECT COUNT(*) AS totalCount FROM table"); $values = mysql_fetch_assoc($results); echo $values['totalCount']; ?> Link to comment https://forums.phpfreaks.com/topic/236777-counting-select-distincts/#findComment-1217479 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.