CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: DBI:
DBI->connect causing running of cgi script to fail

 



dopple
New User

Aug 31, 2009, 8:04 AM

Post #1 of 3 (3273 views)
DBI->connect causing running of cgi script to fail Can't Post

Hi. First time posting here so please bear with me. I have a problem where a script will run fine in the terminal, but doesn't work when called as a cgi script in my browser.

When I run it I get an error in the apache log as so

[Mon Aug 31 15:27:31 2009] [error] [client 192.168.158.162] at /usr/local/apache2/cgi-bin/assyst.pl line 20

Line 20 is the DBI->connect method.

I can output text before the DBI->connect statement but anything after it will not work.

One other thing that may be related is I get an "out of memory" error when I run this in the terminal but it finishes to completion.


Code
#!/usr/bin/perl -w 
##############################
# Assyst.pm
# 31/08/2009
# Connects to and queries assyst database
##############################
use strict;
use warnings;
use DBI;

print "Content-type: text/html\n\n";
my($DBTYPE)="DBI:Oracle";
my($HOST)="xxxxxxxxx";
my($SID)="xxxx";
my($PORT)="xxxxxx";
my($USER)="xxxxxxx";
my($PASS)="xxxxx";
my($CSTRING)="${DBTYPE}:host=${HOST};sid=${SID};port=${PORT}";
my($DBH)=DBI->connect($CSTRING, $USER, $PASS,{RaiseError=>1}) or die "$DBI::errstr";
my($UID);
my($EMAIL_ADD)="";
my($SQL)=qq{
select staff_no,email_add
from sa.usr T
where staff_no IS NOT NULL
AND stat_flag='n'
};
my($STH)=${DBH}->prepare(${SQL});
${STH}->execute();
${STH}->bind_columns(undef, \${UID}, \${EMAIL_ADD});
print "TEST";
my($HEADER)=qq{<html>
<head>
<title>Assyst Search</title>
</head>
<body>
<table>
<tr>
<th>UID</th><th>Email</th>
</tr>
};
print "${HEADER}";
while(${STH}->fetch())
{
print "\t\t\t<tr>\n";
print "\t\t\t\t<td>${UID}</td><td>";
if (${EMAIL_ADD})
{
print "${EMAIL_ADD}</td>\n";
}else{
print "</td>\n";
}
print "\t\t\t</tr>\n";
}
${STH}->finish();
${DBH}->disconnect;
my($FOOTER)=qq{
</table>
</body>
</html>};
print "${FOOTER}\n";
exit(0);



FishMonger
Veteran / Moderator

Aug 31, 2009, 9:31 AM

Post #2 of 3 (3270 views)
Re: [dopple] DBI->connect causing running of cgi script to fail [In reply to] Can't Post

How much memory is on the server and how much is the script using?

How much data (number of rows) are you retrieving from the DB?

What DBI and DBD::Oracle version are you using?

I don't see anything in your script that would cause the issue you're having, but I do see a number of things that could be done to make the script easier to read/maintain.

You might want to add the CGI::Carp module to get a better (more detailed) error message.

Code
use CGI::Carp qw(fatalsToBrowser);


Based on the syntax you're using on the vars I'd say you must be a shell programmer.

Using all uppercase var names makes the code harder to read and you rarely need to use the braces around the names. The parens in the var assignments are also not needed, unless you need list context, which your code doesn't.

The CGI or CGI::Simple module would help in cleaning up the html output. And maybe even use the CGI::Pretty module instead of hard coding the \t indentation.

Instead of using the $sth->fetch method, I'd probably use the $sth->fetchrow_hashref method.


dopple
New User

Aug 31, 2009, 10:23 AM

Post #3 of 3 (3267 views)
Re: [FishMonger] DBI->connect causing running of cgi script to fail [In reply to] Can't Post

Thanks for your reply. I'm not a shell programmer per se but I have done more shell than perl. Thanks for the hints. Unfortunately I can't check the memory etc just now as I'm not at the office. I'll check tomorrow and post back.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives