LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help with database perl (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-with-database-perl-326276/)

silhoutte 05-23-2005 12:16 PM

Need help with database perl
 
This is a "simple" perl that I made to access a database called "zoo". But it seems that it can't work. Is there an expert here to help a newbie like me? Thanks a lot

#!/usr/bin/perl
# A simple perl script to access the animal table in the ZOO database
use Pg;
$dbmain = "zoo";
$conn = Pg::connectdb("dbname=$dbmain");
# check if database connection was successful?
die $conn->errorMessage unless PGRES_CONNECTION_OK eq $conn->status;
print "connected to $dbmain\n";
# Select Animals and display them
$sql = "SELECT * FROM animal WHERE cageno = 143";
$result = $conn->exec($sql);
# check if sql query executed successfully?
die $conn->errorMessage unless PGRES_TUPLES_OK eq $result->resultStatus;
print "select from table: $sql\n\n";
# show a list of the database table field names from resulting query
print $result->fname(0);
for ($k = 1; $k < $result->nfields; $k++) {
print ",", $result->fname($k);
}
print "\n";
# display the data returned from the above query executed
while (@row = $result->fetchrow) {
print " ", join(",", @row), "\n";
}

rose_bud4201 05-23-2005 12:59 PM

I'm not seeing anything immediately wrong; you've got a lot of debug statements in there - which ones print out? i.e. where does the program fail to work? Are you getting any error messages that might help point us in the right direction?

The more information you can give, the more helpful we can be :)

glimmy 05-23-2005 09:34 PM

also try adding

edit:use warnings;

in your code. It may catch something.

rose_bud4201 05-23-2005 10:26 PM

Did you mean 'use warnings'? Because yup, that would definitely help also.
Alternatively, '#!/usr/bin/perl -w' can help too (esp. if you've got an older version of perl). See here for a better explanation.

silhoutte 05-23-2005 11:12 PM

That's the problem. Gee ... my tutor is checking me now >.<

Undefined subroutine &Pg::connectdb called at /var/www/cgi-bin/zoo.pl line 6.

Help plz, thx


All times are GMT -5. The time now is 08:45 PM.