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

mysql, php

Solution

You were told correctly. mysql can do this count for you which is much more efficient.

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

Problem

Whats the most efficient way of selecting total number of records from a large table? Currently, Im simply doing ``` $result = mysql_query("SELECT id FROM table"); $total = mysql_num_rows($result) ``` I was told this was not very efficient or fast, if you have a lot of records in the table.

Original source