PHPPHP OOPs Concepts

How to Fetch data from database Using OOPS In PHP

This post about how to fetch data from database using oops concept in PHP. In previous post i explained how to insert data using OOPs concepts. Now from the same table we will fetch the data.

Create a apge function.php inside this page define a class DB_con.
In DB_con class define a constructer for dbconnection and write a funtion for fetchdata.

define('DB_PASS' ,'');
define('DB_NAME', 'demos');
class DB_con
 function __construct()
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
 public function fetchdata()
 $result=mysqli_query($this->dbh,"select * from insertdata");
 return $result;

2. create a page where you can create a object for class and call the fetchdata function.

$fetchdata=new DB_con();
 <table width="100%"  border="0" >
    <th width="9%" height="42" scope="col" >S no.</th>
    <th width="13%" scope="col">Name</th>
    <th width="11%" scope="col">Email</th>
    <th width="11%" scope="col">Contact no</th>
    <th width="11%" scope="col">Gender</th>
    <th width="13%" scope="col">Education</th>
    <th width="13%" scope="col">Address</th>
    <th width="19%" scope="col">PostingDate</th>
      <td height="29"><?php echo $cnt;?></td>
    <td><?php echo $row['name'];?></td>
    <td><?php echo $row['email'];?></td>
    <td><?php echo $row['contactno'];?></td>
    <td><?php echo $row['gender'];?></td>
    <td><?php echo $row['education'];?></td>
    <td><?php echo $row['addrss'];?></td>
    <td><?php echo $row['posting_date'];?></td>
  <?php $cnt=$cnt+1;} ?>

Download full Source Code (Fetch Data Using OOPS Concept)
Size: 140 KB
Version: V 1.2

Recommended Tutorials for you


Anuj kumar

This is Anuj Kumar. I’m a professional web developer with 5+ years of experience. I write blogs in my free time. I love to learn new technologies and share with others. I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials. .


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Check Also
Back to top button