How To Data Fetch From Database Using PHP PDO, MySQL

Written by admin
// Establish database connection.
$dbconnection = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
catch (PDOException $e)
exit("Error: " . $e->getMessage());
include  'dbconnection.php';
  <link href="" rel="stylesheet" id="bootstrap-css">
  <div class="container">
    <div class="row centered-form">
      <div class="col-xs-8 ">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title">Data Fetch From Database Using PHP PDO</h3> </div>
          <div class="panel-body">
            <table class="table table-bordered">
                      $sql ="SELECT name,email,contact_no,gender,qualification,address from student_details";
                      $query= $dbconnection -> prepare($sql);
                      $query-> execute();
                      $results = $query -> fetchAll(PDO::FETCH_OBJ);
                      if($query -> rowCount() > 0)
                      foreach($results as $result)
                    <td><?php echo($cnt);?></td>
                    <td><?php echo htmlentities($result->name);?></td>
                    <td><?php echo htmlentities($result->email);?></td>
                    <td><?php echo htmlentities($result->contact_no);?></td>
                    <td><?php echo htmlentities($result->gender);?></td>
                    <td><?php echo htmlentities($result->qualification);?></td>
                    <td><?php echo htmlentities($result->address);?></td>
                  <?php  $cnt=$cnt+1; } } ?>
  <style type="text/css">
  body {
    background-color: #fff;
  .centered-form {
    margin-top: 60px;
  .centered-form .panel {
    background: rgba(255, 255, 255, 0.8);
    box-shadow: rgba(0, 0, 0, 0.3) 20px 20px 20px;
How To Data Fetch From Database Using PHP PDO, MySQL With Source Code
Size: 3kb
Version: 2.1

About the author


Hi there! I'm Shiv Gupta. I specialized in building Websites. I write blogs in my free time. I really like to find out and share my knowledge with others.I founded ThePhpConcept in March 2020. I started this blog in order that I can interact with some like-minded people and also help people learning PHP, Mysql, jQuery, Html, and PHP Projects and related technologies.

Leave a Comment

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