On Tue, 5 Mar 2002, Simon wrote:
Does anyone know of any good links to help me, I'm trying to query a database (Postgresql) in CGI, I can get the database open without any problems, just trying to get any data displayed is the problem...
Hi Simon,
It's pretty straightforward, although there are several things you should do to make your life easier. But first: which language are you planning to use for your CGI? Perl? C? sh? Scheme?
I'll assume perl. The following bit of code should work:
-- begin code -- #!/usr/bin/perl -w use strict; use DBI;
my $dbh = DBI->connect("dbi:Pg:dbname=test","","", { RaiseError => 1, AutoCommit => 0} ) || die $DBI::errstr;
my $test_sth = $dbh->prepare("SELECT name FROM test") || die $DBI::errstr; $test_sth->execute() || die $test_sth->errstr;
print "Content-type: text/plain\n\n"; while (my ($name) = $test_sth->fetchrow_array()) { print "name: $name\n"; } $test_sth->finish(); $dbh->disconnect(); -- end code --
With a database called "test", and the following schema:
-- begin schema -- CREATE TABLE test ( name text );
INSERT INTO test VALUES ('Andrew');
GRANT SELECT ON test TO "www-data"; -- end schema --
If you can get that working, it's time to read up on CGI::FastTemplate ;-) You may also want to try "man DBI", as the DBI docs are pretty good.
Andrew.