SQL PHP HTML ASP JavaScript articles and free scripts to download
 

Finding total number of rows in a table


We can get the number of rows or records present in a table by using mysql_num_rows() function. This function is to be used along with mysql select query. We can add condition by using mysql where clause to the select query and get the conditional rows. This function is widely used in different php scripts and you can see the use of this function in PHP paging tutorial in our php tutorial section. Please note that this can't be used along with update, delete, insert commands, for this mysql_affected_rows is to be used. You can use count command also to read the number of records. Here is the sample code for mysql_num_rows()


$query = mysql_query("SELECT * FROM student");
$number=mysql_num_rows($query);
echo "Total records in Student table= ". $number;







Tim05-03-2009
The result offered in the main article requires transferring all of the data from the table across the wire to PHP. A much less intensive approach to getting the number of records in a table is:

$query = mysql_query("SELECT COUNT(*) FROM student");
list($number) = mysql_fetch_row($query);
echo "Total records in Student table=$number";
Marcus08-04-2009
Also, to be less intensive all you need to do is select a single column with the least amount of information such as the primary key column:

$query = mysql_query("SELECT key FROM student");
$number = mysql_num_row($query);
echo "Total records in Student table=".strval($number)
JSC06-05-2009
Looks like the ultimate efficient count would be
"SELECT COUNT(key) FROM student"
:)
MSQL11-05-2009
prefer COUNT(key) AS Total
arnab07-07-2009
very useful site.
konrad07-09-2009
To Marcus:

Little mistake - should be:
mysql_num_rows not mysql_num_row
webmaster17-04-2010
I used Tim's example. Very helpful. I took me forever to find a decent example.
Shreeram N Venkatesh04-08-2010
how could we see the table contents from a database using mysql in the dos prompt?
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked
Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
SQL Tutorial List
SQL Commands
SQL Sections
Date & Time
Join Table
String
Math
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.