PHPPOSTGRESQL

How To Insert Data Into Database Using PHP And PostgreSQL

If you want to insert data into postgresql using PHP then you are at right place.In this post we will learn how to connect postgresql with php and insert data into database.

Step 1 :  Create Table.
CREATE TABLE EMP
      (ID       SERIAL PRIMARY KEY,
      NAME      TEXT    NOT NULL,
      MOBNO      INT     NOT NULL,
      EMAIL   TEXT     NOT NULL
      )




Step 2 :Create Database Connection
.
<?php 
 $host = "localhost";
$port = "5432";
$dbname = "demo";
$user = "postgres";
$password = "*****";

$connection_string = "host={$host} port={$port} dbname={$dbname} user={$user} password={$password} ;
$conn = pg_connect($connection_string);
if($conn){
    echo "Connected to ". pg_host($dbconn); 
}else{
    echo "Error in connecting to database.";
}
?>

Step 3:Create Insert Code Connection
.

<?php 
$sql = "insert into emp (NAME, MOBNO, Email)  values('postgre', 68687, 'test@gmail.com')";
$result = pg_query($conn, $sql);
if(!$result){
  echo pg_last_error($dbconn);
} else {
  echo "Inserted successfully";
}
?>

Related Articles

Back to top button