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.
simple-table-listing-postgres-php

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.

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.

7 thoughts on “HTML Table Listing Using PHP and PostgreSQL Database

  1. Thanks. Working like a charme.
    It is possible to “activate” the “delete button” ? Means, after pressing the “Delete” button should delete the employee.
    I am not sure what to do.

  2. Hello, it is possible to implement the delete function ?
    Have ttired to generate a delete.php, but was not successfull.
    thanks.

Leave a Reply

Your email address will not be published.