Dreamhost

Looking for somewhere to host your site? Browse our PHP web hosts section!

Home > Code Snippets > Count and display the number of rows in a mysql database table

Count and display the number of rows in a mysql database table

Description

This code will count the number of rows (entries / records) in a MySQL database table and then display it using echo on the screen.

The code


<?php


/*

  Datebase connection code goes here.
  If you don't know how to do it, see:
  http://www.totallyphp.co.uk/code/connect_to_a_mysql_database.htm

  Change tablename to match the name of your own table

*/

// Connect to the database
db_connect();

// Query the database and get the count
$result mysql_query("SELECT * FROM tablename");
$num_rows mysql_num_rows($result);

// Display the results
echo $num_rows;

?>

Get the code

Download the file to your computer: Click here to get the file