Code for show_snacks.php
<html>
<head>
<title>Snacks: ProstgreSQL Database Test</title>
</head>
<body>
<?php
// include key database variables for connection string
include 'DBVars.php';
echo "\nConnection string >" . $gDB_conn_string . "<" . "\n<p>\n";
// Try to make a connection
$db = pg_connect($gDB_conn_string);
if (!$db) {
die("Error in connection: " . pg_last_error());
}
// Create and run a query
$sql = "SELECT * FROM play.snack";
echo "The SQL query >" . $sql . "<\n<p>\n";
$result = pg_query($db, $sql);
if (!$result) {
die("Error in SQL query: " . pg_last_error());
}
// Show some snacks
while ($row = pg_fetch_array($result)) {
echo "Snack " . $row[0] . " (" . $row[1] . ") has " . $row[2] . " calories!<br/>\n";
}
// wrap up
pg_free_result($result);
pg_close($db);
?>
</body>
</html>