#!/usr/bin/boiler -hi
Content-Type: text/html

<HTML>
<BODY>
<?
string options="";

/* output a database--table as HTML-table */

void error()
{
 if (error) { printf("lib-error: "+perror+"\n"); exit(); };
 return;
};

long pgquery(string query)
{
 error=0;
 long result=pg_exec(connection,query);
 error();
 return result;
}

void gentable(options,long result)
{
 long zeilen = pg_numrows(result);
 long spalten = pg_numfields(result);
 long zeile,spalte;

 printf("<table border cellspacing=0 cellpadding=5>\n");

 printf("<tr>\n");
 spalte = 0;
 while (spalte < spalten)
 {
  printf("<th>"+pg_fieldname(result,spalte)+"</th>");
  spalte++;
 };
 printf("</tr>\n");

 zeile = 0;
 while (zeile < zeilen)
 {
  printf("<tr>\n");
  spalte = 0;
 
  while (spalte < spalten)
  {
   printf("<td>"+pg_result(result,zeile,spalte)+"</td>");
   spalte++;
  };

  printf("</tr>\n");
  zeile++;
 };

 printf("</table>\n");
 return;
}

long connection;

string method=getenv("REQUEST_METHOD");
if (!defined(method)) { method="GET"; };
error=0; /* if REQUEST_METHOD isn't set, dont abort */

/* { getpost(); error(); } doesn't work because all variables would be local */
if (method=="POST") getpost();
error();

if (isset("submit"))
{
 connection=pg_connect("", "", "", "",datenbank);
 error();

 long result=pgquery("select * from "+tabelle+";");
 gentable(result);
 pg_freeresult(result);
 pg_close(connection);
}
else
{
?>
<pre>
<form action="pgtest.cgi" method="post">
Database <input type="text" name="datenbank">
Table    <input type="text" name="tabelle">
<input type="submit" name="submit" value="Show">
</form>
</pre>
<?
};

?>
</BODY>
</HTML>
