Blog » PHP » how to count number of all rows In Php and mysql database.

how to count number of all rows In Php and mysql database.

In This article will show you how to Count Number of all Database fetch rows results in PHP MYSQLI. This simple counting script In PHP MYSQL. You can see Complete source code.

complete source code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Counting products</title>
</head>
<body>
<style>
.count{
	background:#ccc;
	padding:7px;
}
.count p{
	font-family:Arial, Helvetica, sans-serif;
	color:#06F;
	font-size:16px;
}
</style>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "count";
$con = new mysqli($servername, $username, $password, $database);
if($con->connect_error){
die ('Failed to connect to MySQL: ' . $mysqli->connect_errno);
}
$results = $con->query("SELECT COUNT(*) AS num_item FROM product");		  					
$row = $results->fetch_array();
$all_items = $row['num_item'];
?>
<div class="count">
  <p> Number Of Total Records (<?php echo $all_items;?>) </p> 
 </div>
</body>
</html>

Database 'product' table

CREATE TABLE IF NOT EXISTS `product` (
`id` int(55) NOT NULL,
 `title` varchar(55) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
ARTICLE POSTED BY : Admin

Hi, I'am k.g uththara. I'm a professional web developer. I write blog posts in my free time. if you want to contact me, send a message on contact page, thanks.