Example of using PHP and Postgres

When using mi-linux (csl-student), the hostname must be: localhost.

<!--
-- PHP sample program (Postgres) -- output
-->
<html>
<body>
<?php
function select_data($conn) {

$sql="SELECT * FROM Emp ORDER BY name";
$result=pg_exec($conn,$sql);
# note pg_exec becomes pg_query in PHP ver 4.1.2.
if (!$result)
  { print "<p>An error occured.</p>";
    exit;
    }

$num=pg_numrows($result);
print "<p><b>". $sql . "</b></p>";
print "<p>No of records found: ";
print $num;
print "<pre>";

for ($i=0; $i < $num; $i++) {
    $cols=pg_fetch_row($result, $i);
    print "<p>";

for ($j=0; $j < count($cols); $j++){
    $column_value=$cols[$j];
    $column_name=pg_fieldname($result,$j);
    print "<b>" . $column_name . "</b>: " . $column_value . "<br >";
    }
print " <br></p>";

    }
print "</pre>";
} # select_data

echo ("<h2>Accessing Postgres via PHP</h2>");

/* Set the login settings */

$host="localhost";
$db="dbYour_student_number";
$user="your_student_number";
$passwd="your_postgres_password";

/* Now login to the database */

$connect=pg_connect("host=".$host." dbname=".$db." user=".$user." password=".$passwd);
select_data($connect);
pg_close($connect);
?>

</body>
</html>