Php

HTML Table Listing Using PHP and PostgreSQL Database

This PHP tutorial help to create an HTML listing using the PostgreSQL database. It’s very simple and easy to create HTML listing using PHP, as like other databases used, except PostgreSQL database connection string and Postgres method to fetch data.

PHP provides PostgreSQL libs to communicate PHP with Postgres database.

We will follow the following steps to integrate PostgreSQL with php.

  • Enable Postgres module from php.ini file
  • Create connection string using postgres database.
  • Get data and display into an HTML template.

How to Enable postgres data in php.ini

we will enable postgres extension from php.ini file, we will un-comment the below the lines.

uncommented, extension=php_pdo_pgsql.dll,
uncommented, extension=php_pgsql.dll,
uncommented, extension_dir = "c:/wamp/bin/php/php5.3.5/ext/",

Note: You can read second part : jQuery Ajax Add, Edit and Delete Using PHP and PostgreSQL

How to Connect Postgres Database with PHP

We will use pg_connect method to create a database connection with PostgreSQL. I will pass dbhost, db name, port and password.

We have created a ‘test’ database and created employees using the below script.

CREATE TABLE employee (
    id        char(5),
    employee_name       varchar(100),
    employee_salary         integer,
 employee_age        integer,
    CONSTRAINT code_title PRIMARY KEY(id)
);

we will create connection.php file and add the below code,

<?php
Class dbObj{
 /* Database connection start */        var $servername, $username, $password, $dbname, $port, $conn;

 __construct()
 {
   $this->servername = "localhost";
   $this->username = "test";
   $this->password = "test123";
   $this->dbname = "test";
   $this->port = "5432";
 }
 function getConnstring() {
  $con = pg_connect("host=".$this->servername." port=".$this->port." dbname=".$this->dbname." user=".$this->username." password=".$this->password."") or die("Connection failed: ".pg_last_error());

  /* check connection */  if (pg_last_error()) {
   printf("Connect failed: %s\n", pg_last_error());
   exit();
  } else {
   $this->conn = $con;
  }
  return $this->conn;
 }
}
?>

We will create response.php file and add the below code to access data records from PostgreSQL table.

Related Post
include("connection.php");
class Employee {
 protected $conn;
 protected $data = array();
 function __construct() {

  $db = new dbObj();
  $connString =  $db->getConnstring();
  $this->conn = $connString;
 }
 
 public function getEmployees() {
  $sql = "SELECT * FROM employee";
  $queryRecords = pg_query($this->conn, $sql) or die("error to fetch employees data");
  $data = pg_fetch_all($queryRecords);
  return $data;
 }
}

Included connection.php file at the top of file, This file used to access the Postgres database connection object. We have defined the Employee class and assigned the connection object into a class variable.

Finally, created getEmployees() method to access employee data from the postgres database and return data. We will use this method later on into index.php file to get results set.

We will create index.php and included required css and js libs files.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

We will import response.php file to access data from postgres using the method.

<?php
    include("response.php");
    $newObj = new Employee();
    $emps = $newObj->getEmployees();
 ?>

Now, we have all employee’s data in $emps php variable. We will iterate on $emps array and bind data with html table column using foreach() method.

<table id="employee_grid" class="table" width="100%" cellspacing="0">
 <thead>
  <tr>
   <th>Name</th>
   <th>Salary</th>
   <th>Age</th>
   <th>Action</th>
  </tr>
 </thead>
 <tbody>
  <?php foreach($emps as $key =>$emp)
   <tr>
    <td><?php echo $emp['employee_name'] ?></td>
    <td><?php echo $emp['employee_salary'] ?></td>
    <td><?php echo $emp['employee_age'] ?></td>
    <td>
     <div class="btn-group" data-toggle="buttons"><a href="#" target="_blank" class="btn btn-warning btn-xs" rel="noopener">Edit</a><a href="#" target="_blank" class="btn btn-danger btn-xs" rel="noopener">Delete</a><a href="#" target="_blank" class="btn btn-primary btn-xs" rel="noopener">View</a></div></td>

   </tr>

 <?php endforeach;?>

You can download the source code and see Demo from the below link.

Conclusion:

We have enabled postgres database driver into php using php.ini file, Also created a database connection object using pg_connect() method, fetched data from postgres database using PHP and display into HTML table.

View Comments

Recent Posts

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

2 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

2 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

3 months ago

Categories