PHP PDO Select and Display Data using Fetch Method - Web Development and Web Design Codes

Latest

Friday, April 28, 2017

PHP PDO Select and Display Data using Fetch Method

Displaying Data from Database using PDO Fetch Method

PHP PDO Select and Display Data using Fetch Method
PHP PDO Select and Display Data using Fetch Method

Hi Guys in This Lesson we will Learn how to Display Data from MySQL Database using PHP Data Objects(PDO) Fetch Method.
Very simple and Easy PDO Codes for Begginers to Display Data from Database.

Overviews and Setup:

Database Structure and Overviews:
Database Name: tutorial
Table name: test
Create a Table Using Following SQL Commands
CREATE TABLE IF NOT EXISTS `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(150) NOT NULL,
  `email` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

Insert some rows manully or Following SQL Commands to Insert some rows in Table
INSERT INTO `test` (`id`, `name`, `email`) VALUES
(12, 'ANJANBD', 'Anjankumardhali6@gmail.com'),
(11, 'Krishna', 'krishna@mail.com'),
(3, 'Rupam Mondal', 'RupomMondal@gmail.com'),
(4, 'ANJAN', 'KumarANjan@gmail.com'),
(5, 'Biswas Shiuli', 'ShiuliBiswas@gmail.com'),
(7, 'Hrithik', 'Hrithik@gmail.com'),
(8, 'Modhu Bonik', 'Modhu360@gmail.com'),
(9, 'Rupam Mondal Joy', 'RupomMondal@gmail.com'),
(13, 'Rupa', 'Rupa@hotmail.com'),
(14, 'ANJAN', 'Kumar@gmail.com'),
(15, 'ANJANBD', 'Anjankumardhali6@gmail.com'),
(16, 'Krishna', 'krishna@mail.com'),
(17, 'Rupam Mondal', 'RupomMondal@gmail.com'),
(18, 'ANJAN', 'KumarANjan@gmail.com'),
(19, 'Biswas Shiuli', 'ShiuliBiswas@gmail.com'),
(20, 'Hrithik', 'Hrithik@gmail.com'),
(21, 'Modhu Bonik', 'Modhu360@gmail.com'),
(22, 'Rupam Mondal Joy', 'RupomMondal@gmail.com'),
(23, 'Rupa', 'Rupa@hotmail.com'),
(24, 'ANJAN', 'Kumar@gmail.com'),
(25, 'ANJANBD', 'Anjankumardhali6@gmail.com'),
(26, 'ANJANBD60', 'Anjankumardhali6@gmail.comss'),
(27, 'Priyoshi', 'Priyoshi@gmail.com'),
(28, 'Rupom Chakma', 'Rupam@gmail.com'),
(29, 'Monisha Roy', 'Monisharoy@gmail.com');

Files Structure:
Create 2 Files
1.display.php [Display.php will display all rows from table test]
2.style.css [Applying some CSS codes to Design our display.php page]

codes for display.php

in display.php php files we will fetch all rows from test and display as a table.
<?php
$host = "localhost"; //Host Name
$username = "root"; //MySQL Username.if you don't know leave 'root' by Defaults
$password = ""; //MySQL Password if Password Protected .else Leave it Blank
$db_name = "tutorial"; //Database Name

try {
 //Creating Connection between Server and Database
    $conn = new PDO("mysql:host=$host;dbname=$db_name", $username, $password);
    // SET PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   
    }
catch(PDOException $e)
    {
  //Displaying Error Message with Detail.
    echo "Sorry failed to Connect: " . $e->getMessage();
    }
?> 
   <link rel="stylesheet" href="style.css" type="text/css"/>     
  <table>
     <tr>
         <th>ID</th>
      <th>Name</th>
      <th>Email</th>
   </tr>
   <?php
   $query = "SELECT * FROM test";
        $stmt = $conn->prepare( $query );
        $stmt->execute();
        while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
        ?>
   
   <tr>
       <td><?php echo $row['id'];?></td>
    <td><?php echo $row['name'];?></td>
    <td><?php echo $row['email'];?></td>
             </tr>
   <?php
      }
   ?> 
  </table>
        

codes for style.css

now we will apply some basic css codes for design our display page.
table th,td{
 border: 1px solid blue;
 text-align:left;
}
table{
 border-collapse:collapse;
 width:50%;
}
th,td{
 padding:8px;
}
th{
 padding: 12px;
 font-weight:bold;
 font-size:20px;
        color:white;
 background:#6666ff;
}
tr:nth-child(even){background: #ccffcc}
tr:nth-child(odd){background:#f2d9e6}

If you Like this Article Please Share with your Friends. Keep Visiting for Next Articles.Thank You.

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.