<title>Show PostgreSQL database tables</title>
// Simple testscript which reads a PostgreSQL database table and outputs a HTML table.
// v0.11 12-Aug-2002, by G. Knauf <efash@gmx.net>
// Connecting to PostgreSQL database
$conn = pg_connect("host=$host dbname=$dbase user=$user password=$pass ");
if (!$conn) {
    echo "Error occured.\n";

$result = pg_query ($conn, "SELECT * FROM $table");
if (!$result) {
    echo "Error occured.\n";

$num = pg_num_rows($result); 
$nfields = pg_num_fields($result); 

print "<h3>Table <font color=green>$table</font> contains <font color=blue>$num</font> records with <font color=blue>$nfields</font> fields:</h3>\n";
print "<table border>\n<tr>";

for ($i=0; $i < $nfields; $i++) {
  echo '<th>'.pg_field_name($result, $i).'</th>';
print "</tr>\n";

for ($i=0; $i < $num; $i++) {
  echo '<tr>';
  $r = pg_fetch_row($result, $i);

  for ($j=0; $j < count($r); $j++) {
    echo '<td>'.$r[$j].'&nbsp;</td>';

  print "</tr>\n";
print "</table>\n";
Published in: MySQL

Related snippets