Whats the best way to get total # of records in a mysql table with php?

Mar 12, 2012   //   by phpfreelancer.biz   //   Blog, MySQL Tutorial  //  No Comments

Whats the most efficient way of selecting total number of records from a large table?

MyISAM tables already store the row count, so “SELECT COUNT(*) FROM table” on a MyISAM table simply reads that value. It doesn’t scan the table or the index(es). So, it’s just as fast or faster than reading the value from a different table.

$result = mysql_query( "select count(id) as num_rows from table" );
$row = mysql_fetch_object( $result );
$total = $row->num_rows;

Leave a comment

Share This Post

RSS Wordpress News