In the previous tutorial, we learned how to insert data in PostgreSQL using PHP. In this tutorial, we will learn how to fetch/read the inserted data in PostgreSQL using PHP.
File structure for this tutorial
dbcon.php:Â This is used for PostgreSQL database connection with PHP.
read.php:Â This is used for HTML Table and we will also put the PHP code here for data fetch/read.
We will use the same PostgreSQL database and table used in the last tutorial.
tblemployee structure
Code for PostgreSQL connection with PHP (dbcon.php)
<?php // Database configuration $host = 'localhost'; $db = 'testdb'; // Here you can use your datbase name $user = 'postgres'; $pass = 'Test@123'; // Here you can your PostgreSQL DB user password $port = '5432'; // Default port for PostgreSQL // Create connection string $conn_string = "host=$host port=$port dbname=$db user=$user password=$pass"; // Establish a connection to the PostgreSQL database $conn = pg_connect($conn_string); if (!$conn) { echo "Error: Unable to open databasen"; exit; }
Code for fetching data from PostgreSQL using PHP
<table class="table table-striped"> <thead> <tr> <th>#</th> <th>Name</th> <th>Email Id</th> <th>Mobile No</th> <th>Department</th> <th>Creation Date</th> </tr> </thead> <tbody> <?php $query= pg_query($conn,"select * from tblemployee"); $cnt=1; while($row=pg_fetch_array($query)){ ?> <tr> <td><?php echo $cnt;?></td> <td><?php echo $row['empname'];?></td> <td><?php echo $row['empemailid'];?></td> <td><?php echo $row['empmobileno'];?></td> <td><?php echo $row['empdepartment'];?></td> <td><?php echo $row['creationdate'];?></td> </tr> <?php $cnt++;} ?> </tbody> </table>
Here is the full code that we have written during this tutorial:
<?php include_once('dbcon.php'); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title>Fetch Data from PostgreSQL using PHP</title> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script> <style> body { color: #666; background: #f5f5f5; font-family: 'Roboto', sans-serif; } .table-responsive { margin: 30px 0; } table.table tr th, table.table tr td { border-color: #e9e9e9; } table.table-striped tbody tr:nth-of-type(odd) { background-color: #fcfcfc; } .table-wrapper { min-width: 1000px; background: #fff; padding: 20px; box-shadow: 0 1px 1px rgba(0,0,0,.05); } .pagination { margin: 10px 0 5px; } .pagination li a { border: none; min-width: 30px; min-height: 30px; color: #999; margin: 0 2px; line-height: 30px; border-radius: 4px !important; text-align: center; padding: 0; } .pagination li a:hover { color: #666; } .pagination li.active a, .pagination li.active a.page-link { background: #59bdb3; } .pagination li.active a:hover { background: #45aba0; } .pagination li:first-child a, .pagination li:last-child a { padding: 0 10px; } .pagination li.disabled a { color: #ccc; } .pagination li i { font-size: 17px; position: relative; top: 1px; margin: 0 2px; } </style> </head> <body> <div class="container-xl"> <div class="table-responsive"> <div class="table-wrapper"> <table class="table table-striped"> <thead> <tr> <th>#</th> <th>Name</th> <th>Email Id</th> <th>Mobile No</th> <th>Department</th> <th>Creation Date</th> </tr> </thead> <tbody> <?php $query= pg_query($conn,"select * from tblemployee"); $cnt=1; while($row=pg_fetch_array($query)){ ?> <tr> <td><?php echo $cnt;?></td> <td><?php echo $row['empname'];?></td> <td><?php echo $row['empemailid'];?></td> <td><?php echo $row['empmobileno'];?></td> <td><?php echo $row['empdepartment'];?></td> <td><?php echo $row['creationdate'];?></td> </tr> <?php $cnt++;} ?> </tbody> </table> </div> </div> </div> </body> </html>
PHP-PostgreSQL Data Fetch/Read Script (Download Source Code)
Size: 310 KB
Version: V 1.0
The post How to fetch data from PostgreSQL using PHP appeared first on PHPGurukul.
Source: Read MoreÂ