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

Leave A Reply

Your email address will not be published.

  1. Abhishek says


  2. Anuj Kumar says

    I am glad this helped you.

  3. pardeep says

    Great! way of coding and it very useful for me .

    1. Anuj Kumar says

      I am glad this helped you.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy