Monday, November 22, 2010

Count number of rows with php mysql

This is a small script which will help you find the number of rows from a mysql database for your query.



//code for connect.php

$host = "localhost"; // replace this with your host, default is localhost

$user = "username"; // replace with the mysql username

$pass = "password"; // replace with the mysql password

$db_name = "mydb";


mysql_connect($host,$user,$pass) or die("
Cannot connect to host.....".mysql_error());

mysql_select_db($db_name) or die("
Cannot connect to Database.....".mysql_error());

?>


// Connect to the database

include "connect.php";

// 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;

No comments:

Post a Comment