<!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 os filmes como uma tabela HTML
function displayFilmes($result)
{
  $nrows  = pg_numrows($result);
  if( $nrows > 0 ) {
    printf("<h2>Filmes</h2>\n");
    printf("<table border=2>\n");
    printf("<tr>");  
    printf("<th align=left>nome</th>");
    printf("<th align=left>ano</th>");
    printf("<th align=left>duração</th>");
    printf("<th align=left>aCores</th>");
    printf("<th align=left>estúdio</th>");
    printf("<th align=left>realizador</th>\n");
    printf("</tr>");  
    for($i=0; $i<$nrows; $i++) {
      $tuple = pg_fetch_array($result,$i);
      $nome = $tuple['nome'];
      $ano = $tuple['ano'];
      $duracao = $tuple['duracao'];
      $aCores = $tuple['acores'];
      $nomeEstudio = $tuple['nomeestudio'];
      $nomeRealizador = $tuple['nomerealizador'];
      
      printf("<tr>");  
      printf("<td>%s</td>", $nome );
      printf("<td>%s</td>", $ano );
      printf("<td align=right>%s</td>", $duracao );
      printf("<td align=center>%s</td>", $aCores );
      printf("<td>%s</td>", $nomeEstudio );
      printf("<td>%s</td>", $nomeRealizador );
      printf("</tr>\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
  displayFilmes($result);

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