0% found this document useful (0 votes)
4 views

Database Connection

This document shows how to connect to a PostgreSQL database from PHP, create a table, insert data, and retrieve data. It connects to the database, creates a table with student data, inserts 3 records, and then retrieves and displays the records.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Database Connection

This document shows how to connect to a PostgreSQL database from PHP, create a table, insert data, and retrieve data. It connects to the database, creates a table with student data, inserts 3 records, and then retrieves and displays the records.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

File: /home/smj/Downloads/database_connection.

txt Page 1 of 2

smj@ubuntu:~$ sudo -i -u postgres


[sudo] password for smj:
postgres@ubuntu:~$ psql
psql (9.3.24)
Type "help" for help.

postgres=# ALTER USER postgres WITH ENCRYPTED PASSWORD 'password';


ALTER ROLE

postgres=# CREATE DATABASE Ankita;

postgres=# \l
postgres=# \c Ankita;
You are now connected to database "Ankita" as user "postgres".
Ankita=# CREATE TABLE student(roll_no int primary key, Name varchar(20));
CREATE TABLE
Ankita=# select * from student;
roll_no | name
---------+------
(0 rows)

Ankita=# insert into student values(1, 'Ankita');


INSERT 0 1
Ankita=# insert into student values(2, 'Deepti');
INSERT 0 1
Ankita=# insert into student values(3, 'Geeta');
INSERT 0 1
Ankita=# select * from student;
roll_no | name
---------+--------
1 | Ankita
2 | Deepti
3 | Geeta
(3 rows)

Add php file.php

<?php
$host="localhost";
$user="postgres";
$pass="password";
$db="Ankita";

$con=pg_connect("host=$host dbname=$db user=$user password=$pass");


if(!$con)
{
die("Could not connect to database server <br>");
}
else
{
echo "Connection succes <br>";
}

$query= "SELECT * FROM student";


$result= pg_query($con, $query) or die("Error in query: $query.".pg_last_error($con));

$rows=pg_num_rows($result);
echo "There are currently $rows records in database.<br>";
pg_close($con);
File: /home/smj/Downloads/database_connection.txt Page 2 of 2

?>

O/P :
Connection succes
There are currently 3 records in database.

Fetch data from database

<?php
$host="localhost";
$user="postgres";
$pass="password";
$db="Ankita";

$con=pg_connect("host=$host dbname=$db user=$user password=$pass");


if(!$con)
{
die("Could not connect to database server <br>");
}
else
{
echo "Connection succes <br>";
}

$query= "SELECT * FROM student";


$result= pg_query($con, $query) or die("Error in query: $query.".pg_last_error($con));

$rows=pg_num_rows($result);
echo "There are currently $rows records in database.<br>";

if($rows>0)
{
for($i=0;$i<$rows;$i++)
{
$row=pg_fetch_row($result,$i);
echo $row[0] ."<br>". $row[1] ."<br>". $row[2];

}
}
else
{
echo"No data Available";
}
pg_close($con);
?>

You might also like