PHPPOSTGRESQL

Crud Application Using PHP And PostgreSqls

Step 1 : Create Database

CREATE DATABASE demo

Step 2 : Create Table:

CREATE TABLE  test (id serial NOT NULL,name character varying,email character varying,mobno character varying ) ;

Step 3 : Create Database Connection:

$host = "localhost";
    $port = "5432";
    $dbname = "demo";
    $user = "postgres";
    $password = "password"; 
    $connection_string = "host={$host} port={$port} dbname={$dbname} user={$user} password={$password} ";
    $dbconn = pg_connect($connection_string);     

Step 4 : Insert data.

<?php 
$sql = "insert into test(name, email, mobno)  values('test',test@gmail.com', '165675')";
$result = pg_query($dbconn, $sql);
if($result){
  ehco "Record saved";
}  
?>

Step 5 : Select data from database.

<?php 
$result = pg_query($dbconn, "SELECT * FROM test");
if (!$result) {
    echo "Something went wrong.";
    exit;
}
$arr = pg_fetch_all($result);
echo "<pre>";
print_r($arr);
echo "</pre>";
?>

Step 6 : Update data.

<?php 
$sql = "update test set name ='check' , email ='123@gmail.com' where id = 1;
$result = pg_query($dbconn, $sql);
if($result){
  echo "Updated successfully.";
} else {
  echo  pg_last_error($dbconn);;
}
?>

Step 7 : Delete data.

<?php 
$sql = "delete from test where id= 1";
 $result = pg_query($dbconn, $sql);
if($result){
  echo "Deleted successfully\n";
  
} else {
  echo pg_last_error($dbconn);
}
?>

Related Articles

Back to top button