0% found this document useful (0 votes)
30 views13 pages

Chapter 1

This document discusses connecting to and interacting with a PostgreSQL database from PHP. It covers connecting to the database, creating and manipulating tables through queries, and preparing and executing statements. Functions covered include pg_connect() to open a connection, pg_close() to close it, pg_prepare() to prepare queries for execution, and pg_execute() to run prepared statements with parameters. Examples are provided for creating a table, inserting records, selecting and updating data, and using prepared statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views13 pages

Chapter 1

This document discusses connecting to and interacting with a PostgreSQL database from PHP. It covers connecting to the database, creating and manipulating tables through queries, and preparing and executing statements. Functions covered include pg_connect() to open a connection, pg_close() to close it, pg_prepare() to prepare queries for execution, and pg_execute() to run prepared statements with parameters. Examples are provided for creating a table, inserting records, selecting and updating data, and using prepared statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

WEB DEVELOPMENT - II

COURSE CODE: CSC3604


Paper – IV
No. of credits: 02
Course Outcomes

 Describe concept of Object-Oriented Programming, file, and XML


 Represent AJAX model to develop interactive web pages
 Implement database connectivity and AJAX with PHP to generate dynamic and
interactive web pages
 Distinguish PHP as a server-side programming language
 Determine how websites are built using various concepts and technologies.
 Develop the modern web pages using the HTML, CSS features with different
layouts and PHP as per need of applications
Topics to be covered

 Database Connectivity with PostgreSQL


 Introduction to OOPs
 Working with file and Directories
 Web Techniques
 XML
 AJAX
Chapter -1 : Database Connectivity with
PostgreSQL

 Connection with PostgreSQL Database

 Performing basic database operation (DML) (Insert, Delete, Update, Select)

 Setting query parameter

 Executing query

 Mini Project
Connection with PostgreSQL Database
Following PHP codes demonstrates how to set a connection with PostgreSQL Database (In Lab):
<?php
$host="host=http://192.168.16.1";
$port="port=5432";
$dbname="dbname=root";
$credentials="user=root password=";
$conn=pg_connect("$host $port $dbname $credentials") or die("could not connect");
?>
Creating a table from PHP

<?php
// $host="host=localhost";
$sql=<<<EOF
// $port="port=5432";
create table company(id int , name
// $dbname="dbname=postgres"; varchar(20));
EOF;
//$credentials= "user=postgres password=123"; $ret=pg_query($db, $sql);
//$db=pg_connect("$host $port $dbname $credentials"); if(!$ret)
echo pg_last_error($db);
$db=pg_connect("host=http://192.168.16.1 port=5432 else
dbname=ty100 user=ty100 password=123"); echo "Table created";
pg_close($db);
if(!$db)
?>
echo "unable to connect db\n";
else
echo "DB ok!!!";
Inserting records into table from PHP
$ret=pg_query($db, $sql);
<?php if(!$ret)
echo pg_last_error($db);
$db=pg_connect("host=http://192.168.16.1 port=5432
else
dbname=ty100 user=ty100 password=123"); echo "3 records added";
if(!$db)
echo "unable to connect db\n";
else
echo "DB ok!!!";
//insert the records into table
$sql=<<<EOF
insert into company values(1,'aaa'),(2,'bbb'),(3,'ccc');
EOF;
//display the records //read the records from resultset and display each record one by
$sql=<<<EOF one
select * from company; echo "\n displaying updated values";
EOF;
$ret=pg_query($db,$sql);
while($row=pg_fetch_row($ret))
if(!$ret) {
{ echo "ID= " . $row[0] . "\n";
echo pg_last_error($db); echo "Name= ".$row[1] . "\n\n";
exit; }
} echo "operation successful";
//read the records from resultset and display each record one by
one
while($row=pg_fetch_row($ret))
{
echo "ID= " . $row[0] . "\n";
echo "Name= ".$row[1] . "\n\n";
}
echo "operation successful";

//update operation
$sql=<<<EOF
update company set name='SSSS' where id=2;
EOF;
$ret=pg_query($db,$sql);
if(!$ret)
{
echo pg_last_error($db);
exit;
}
Pg_connect()
 Opens a postgreSQL connection
 The syntax is:
pg_connect(string $connection_string, int $flags=0)

 Pg_connect() opens a connection to postgreSQL database specified by connection_string


 The connection_string can be empty to use all default parameters, or it can contain one or
more parameter settings separated by whitespace
 The recognized parameter keywords are: host, port, dbname, user and password etc.
Pg_close()

 Closes a postgreSQL connection


 The syntax is:
Pg_close();
pg_prepare()
 Submits a request to create a prepared statement with the given parameters, and waits for completion.
 The syntax is:
pg_prepare([resource $connection], String $stmtname, string $query)
pg_prepare() creates a prepared statement for later execution with pg_execute() or pg_send_execute(). This feature
allows commands that will be repeatedly to be parsed and planned just once, rather than each time they are
executed.
Prepared statements for use with pg_prepare() can also be created by executing SQL PREPARE statements. (But
pg_prepare() is more flexible since it does not require parameter types to be pre-specified.)
PARAMETERS:
Connection: When connection is unspecified, the default connection is used. The default connection is the last
connection made by pg_connect() or pg_pconnect()
Stmtname: The name to give the prepared statement. Must be unique per-connection. If "" is specified, then an
unnamed statement is created, overwriting any previously defined unnamed statement.
Query: The parameterized SQL statement. Must contain only a single statement. (multiple statements separated by
semi-colons are not allowed.) If any parameters are used, they are referred to as $1, $2, etc.
Exam
<?php
// Connect to a database named “ty100"
$dbco nn = pg_connect("dbname=ty100");

// Prepare a query for execution


$result = pg_prepare($dbconn , "my_query", 'SELECT * FROM company WHERE name = $1');

// Execute the prepared query. Note that it is not necessary to escape


// the string “aaa" in any way
$result = pg_execute($ dbconn, "my_query", array(“aaa"));

// Execute the same prepared query, this time with a different parameter
$result = pg_execute($ dbconn, "my_query", array(“aaa aaa aaa"));
?>
pg_execute()
 Sends a request to execute a prepared statement with given parameters , and waits for the result
 The syntax is:
pg_execute(Connection $connection = ?, string $stmtname, array $params)

You might also like