<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
   <title>base de dados de filmes</title>
   <meta  http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
    
<?php

include 'db.inc';
include 'error.inc';

// mostra $result como uma tabela HTML
function displayTable($title, $result)
{
  $ncols = pg_numfields($result);
  $nrows = pg_numrows($result);  
  if( $nrows > 0 ) {
    printf("<h2>$title</h2>\n");
    printf("<table border=2>\n");
    printf("<tr>\n");  
    for($j=0; $j<$ncols; $j++) {
      $column_name = pg_fieldname($result, $j);
      printf("<th align=left>$column_name</th>\n");
    }
    printf("</tr>\n\n");  

    for($i=0; $i<$nrows; $i++) {
      $tuple = pg_fetch_array($result,$i);
      printf("<tr>\n");  
      for($j=0; $j<$ncols; $j++)
	printf("<td>%s</td>\n", $tuple[$j] );
      printf("</tr>\n\n");

    } // end for
    printf("</table>\n");
  } // end if
}


// ligação à base de dados
$db = dbconnect($connection_string);  
if($db) {
  // criar query numa string 
  $query  = "SELECT * FROM filmes";
    
  // executar a query
  if(!($result = @ pg_exec($db, $query )))
    showerror($db);

  // mostra o resultado
  displayTable("Tabela de filmes", $result);

  // fechar a ligação à base de dados
  pg_close($db);
}
?>
          
</body>
</html>