Count the number of rows

Count the number of rows in a MySQL database

//database can be found at http://www.getphp.net/sample-mysql-database/

<?php
//connect to server with username and password
$connection = mysql_connect (“localhost”,”username”, “password”) or die (“Cannot make the connection”);
//connect to database
$db = mysql_select_db (“test”,$connection) or die (“Cannot connect to database”);
//our SQL query
$sql_query = “SELECT * FROM test”;
//store the SQL query in the result variable
$result = mysql_query($sql_query);
$rows = mysql_num_rows($result);
//output total
echo $rows;

?>

LEAVE A REPLY

Please enter your comment!
Please enter your name here

*