CodeIgniter Laravel PHP PostgreSQL PHP Example Javascript jQuery MORE

Fetch record or Data using PostgreSQL and PHP


In this example we use 2 file for Fetch record or Data using PostgreSQL and PHP.

  1. database.php
  2. view.php

database.php

<?php
  $host        = "host = 127.0.0.1";
   $port        = "port = 5432";
   $dbname      = "dbname = testdb";
   $credentials = "user = postgres password=pass123";
   $conn = pg_connect( "$host $port $dbname $credentials"  );
?>

view.php

<?php
include_once 'database.php';
$result = pg_query($db,"SELECT * FROM employee");
?>
<!DOCTYPE html>
<html>
 <head>
 <title> Retrive data</title>
 </head>
<body>
<table>
	<tr>
		<td>First Name</td>
		<td>Last Name</td>
		<td>City</td>
		<td>Email id</td>
	</tr>
<?php
$i=0;
while($row=pg_fetch_assoc($result)) {
?>
	<tr>
		<td><?php echo $row["first_name"]; ?></td>
		<td><?php echo $row["last_name"]; ?></td>
		<td><?php echo $row["city_name"]; ?></td>
		<td><?php echo $row["email"]; ?></td>
	</tr>
<?php
$i++;
}
?>
</table>
</body>
</html>