This article describes the PHP connection and operation of the PostgreSQL database method. Share to everyone for your reference.
The implementation methods are as follows:
Copy Code code as follows:
$PG = @pg_connect ("Host=localhost user=postgres password=sa Dbname=employes")
Or Die ("can ' t connect to database.");
$query = "SELECT * from Employes order by Serial_no";
$query = "INSERT into employes values (10008, ' Susan ', ' 1985-09-04 ', ' 80 ', ' 50 ')";
$result = @pg_query ($PG, $query) or Die ("can" t run query to table.);
echo pg_num_rows ($result); Output how many records are queried
if ($result)
//{
echo "Recrods inserted sucessfully!";
echo pg_affected_rows ($result);//Output How many records are inserted
//}
instance one [Pg_fetch_row]
echo "<table border=1>";
echo "<tr>";
echo "<td>serial_no</td>";
echo "<td>name</td>";
echo "<td>birthday</td>";
echo "</tr>";
for ($i =0; $i <pg_num_rows ($result); $i + +)
{
$row = @pg_fetch_row ($result) or Die ("can ' t fetch row from table.");
$serial _no= $row [0];
$name = $row [1];
$birthday = $row [2];
echo "<tr>";
echo "<td> $serial _no</td>";
echo "<td> $name </td>";
echo "<td> $birthday </td>";
echo "</tr>";
}
echo "</table>";
Example two [Pg_fetch_array]
echo "<table border=1>";
echo "<tr>";
echo "<td>serial_no</td>";
echo "<td>name</td>";
echo "<td>birthday</td>";
echo "</tr>";
//
for ($i =0; $i <pg_num_rows ($result); $i + +)
//{
//
$row = @pg_fetch_array ($result) or Die ("can ' t fetch row from table.");
$serial _no= $row [' serial_no '];
$name = $row [' name '];
$birthday = $row [' Birthday '];
echo "<tr>";
echo "<td> $serial _no</td>";
echo "<td> $name </td>";
echo "<td> $birthday </td>";
echo "</tr>";
//
//}
echo "</table>";
Add, delete, modify an instance
$newrow =array ("Serial_no" => "1006", "name" => "Peter", "Birthday" => "1990-07-03", "salary" => "the", "bonus "=>" 80 ");
$reusult = @pg_insert ($PG, "employes", $newrow) or Die ("can" insert data to table.);
if ($reusult)
//{
echo "Rechords inserted sucessfully!";
//}
//
Pg_close ($PG);
I hope this article will help you with your PHP program design.