Blog » PHP » how to get fetch results from database in php mysqli using object oriented.

how to get fetch results from database in php mysqli using object oriented.

In This script will cover how to get fetch data from database in PHP MYSQLI using object oriented programming. this script showing whole fetch data from database and we can get one by one results using other view details file. I have added two PHP file index.php file and view.php file. you can see This example and download complete source code.

creating database connection config.php file


<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "demo_product";
$con = new mysqli($servername, $username, $password, $database);
if($con->connect_error){
die("connection failed<br/>");
}
?>


index.php


we can display all fetch results from database using index.php file.


<!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>Products</title>
</head>
<body>
<style>
.products{
	background:#eee;
	height:auto;
	padding:10px;
}
.products img{
	float:left;
	width:70px;
	height:70px;
	padding-right: 5px;
}
p, h5{
  display: block;
  margin: 0px;
	padding: 0px;
}
a{
	font-family:Arial, Helvetica, sans-serif;
	color:#06F;
	text-decoration:none;
}
p, h5{
	font-family:Arial, Helvetica, sans-serif;
	font-size:14px;
}
</style>

<?php
include("config.php");
if ($stmt = $con->prepare("SELECT * FROM product order by id desc")){
$stmt->execute();
}
$result = $stmt->get_result();
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>

<div class="products">

<img class="image" src="images/<?php echo $row['img'];?>" />

<h5><?php echo $row['title'];?></h5>

<p><?php echo $row['description'];?></p>

<p><?php echo $row['date_time']; ?></p>

<a href="view.php?pid=<?php echo $row['id'];?>">View Details</a> 
 
</div>

<?php 
}}else{
echo"not found";
}
?>

</body>
</html>


view.php file we can get one by one record using this file.


<!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>View Details</title>
</head>
<style>
.products{
	background:#eee;
	height:70px;
}
.content{
	padding:14px;
}
.products img{
	float:left;
	width:70px;
	height:70px;
	padding-right: 5px;
}
p, h5{
  display: block;
  margin: 0px;
	padding: 0px;
}
p, h5{
	font-family:Arial, Helvetica, sans-serif;
	font-size:16px;
}
</style>
<body>

<?php
include("config.php");
if(isset($_GET['pid'])){
$id=$_GET['pid'];
$stmt = $con->prepare("SELECT title,description,img FROM product WHERE id=? ");
$stmt->bind_param('s', $id);
$stmt->execute();
$stmt->bind_result($p_title, $p_description, $img);
if($stmt->fetch()){
?>

<div class="products">

<img class="image" src="images/<?php echo $img;?>" />

<div class="content">

<h5><?php echo $p_title; ?></h5>

<p><?php echo $p_description; ?></p>

</div>

<?php }}?>

</div>

</body>
</html>
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.