How to fetch data from PostgreSQL using PHP
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?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 database\n"; exit; } |
Code for fetching data from PostgreSQL using PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
<?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> |