Entry
How can I get the number of rows in a database table?
What's the fastest way to return the number of rows in a table?
Jun 29th, 2000 07:32
James Jensen, Nathan Wallace, Adam Whitehead
To get the number of rows in a table you should use the COUNT(*) feature
in SQL.
Here is an example for MySQL:
$res = mysql_db_query($db,"select count(*) from TABLE_NAME");
$row_count = mysql_result($res,0,"count(*)");
**********
For MSSQL, the COUNT(*) aggregate works fine only with small tables.
When you get tens of thousands of records, issuing a COUNT(*) query
takes a long time because it has to fetch all the records. So, you
should use sp_spaceused stored procedure instead:
$res = mssql_query($db,"sp_spaceused TABLE_NAME");
$data = mssql_fetch_object($res);
echo("Count for TABLE_NAME is " . $data->rows );
BLAM! The data comes back very quick.