pg_connect() opens a connection to a PostgreSQL database specified by the connection_string.
If a second call is made to pg_connect() with the same connection_string as an existing connection, the existing connection will be returned unless you pass PGSQL_CONNECT_FORCE_NEW as connect_type.
The old syntax with multiple parameters $conn = pg_connect("host", "port", "options", "tty", "dbname") has been deprecated.
For example :-
$con = pg_connect("host=$dbhost user=$dbuser password=$dbpasswd dbname=$dbname" )
pg_close() closes the non-persistent connection to a PostgreSQL database associated with the given connection resource.
For example :-
pg_close($con);
pg_connection_status()
returns the status of the specified
connection.
For example :-
<?php
$dbconn = pg_connect("dbname=publisher") or die("Could not connect");
$stat = pg_connection_status($dbconn);
if ($stat === PGSQL_CONNECTION_OK) {
echo 'Connection status ok';
} else {
echo 'Connection status bad';
}
?>
pg_fetch_all()
returns an array that contains all rows (records) in the result resource.
For example :-
$arr = pg_fetch_all($result);
print_r($arr);
pg_fetch_result()
returns the value of a particular row and field (column) in a PostgreSQL result resource.
<?php
$db = pg_connect("dbname=users user=me") || die();
$res = pg_query($db, "SELECT 1 UNION ALL SELECT 2");
$val = pg_fetch_result($res, 1, 0);
echo "First field in the second row is: ", $val, "\n";
?>
pg_fetch_array()
returns an array that corresponds to the fetched row (record).
pg_fetch_array() is an extended version of pg_fetch_row(). In addition to storing the data in the numeric indices (field number) to the result array, it can also store the data using associative indices (field name). It stores both indicies by default.
For example :-
$arr = pg_fetch_array($result);
echo $arr["author"] . " <- Row 3 Author\n";
echo $arr[1] . " <- Row 3 E-mail\n";
pg_query()
executes the
query on the specified database
connection.
For example :-
$result = pg_query($con, "SELECT * FROM $tablename where sr_no = 10");
pg_num_rows()
will return the number of rows in a PostgreSQL result resource.
For example :-
<?php
$result = pg_query($conn, "SELECT 1");
$rows = pg_num_rows($result);
echo $rows . " row(s) returned.\n";
?>