Author Topic: PostgreSQL Example  (Read 16593 times)

Support

  • Administrator
  • *****
  • Posts: 19
    • View Profile
PostgreSQL Example
« on: May 21, 2006, 02:55:09 PM »
http://www.scriptbasic.org/cgi-bin/pgsqltest.bas

I would like to thank Peter Szabo (pgsql extension module author) for updating his work to the current version of the RedHat LAMP stack.

Code: [Select]

#!/usr/bin/scriba -c

INCLUDE cgi.bas
INCLUDE pgsql.bas

OPTION cgi$Method cgi::Post or cgi::Get

cgi::Header 200,"text/html"

cgi::FinishHeader

PRINT """
<html>
<head>
<title>PostgreSQL</title>
</head>
<body>
<font face="Verdana, Arial, Helvetica, sans-serif">
<table border="1" cellpadding="3">
"""

c = PGSQL::PGopen("dbname='database' user='user' password='password'")
IF ISSTRING(c) THEN
  PRINT "Connect Error: (", RTRIM(c), ")\n"
  END
END IF

r = PGSQL::PGexec(c, "SELECT * FROM contact")
IF ISSTRING(r) THEN
  PRINT "Query Error: (", RTRIM(r), ")\n"
  END
END IF

PRINT "<b>PGSQL Handle: </b>" & c & "<br>"
PRINT "<b>Command Status: </b>" & PGSQL::PGcmdStatus(r) & "<br>"
PRINT "<b>Command Tuples: </b>" & PGSQL::PGcmdTuples(r) & "<br>"
PRINT "<br>"

' Column Names
PRINT "<tr>"
nf=PGSQL::PGncols(r)
FOR i=0 TO nf-1
  PRINT "<td>" & PGSQL::PGcol(r,i) & "</td>"
NEXT
PRINT "</tr>"

' Data
nrows=PGSQL::PGnrows(r)
FOR row=0 TO nrows-1
  PRINT "<tr>"
  FOR col=0 TO nf-1
    IF PGSQL::PGgetisnull(r,row,col) THEN
      PRINT "<td>NULL</td>"
    ELSE
      PRINT "<td>" & PGSQL::PGgetvalue(r,row,col) & "</td>"
    END IF
  NEXT
  PRINT "</tr>"
NEXT

PRINT """
</table>
</body>
</html>
"""

PGSQL::PGclose(r)
PGSQL::PGclose(c)

END