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.
php.ini
filewe 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
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.
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.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
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
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
I can not download sourcecode
enabled download link
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.
Hello, it is possible to implement the delete function ?
Have ttired to generate a delete.php, but was not successfull.
thanks.
you can read second part : https://www.phpflow.com/php/jquery-ajax-add-edit-and-delete-using-php-and-postgresql-part-ii/
Not able to download source code
I have fixed url