Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with CGI and DBI

Status
Not open for further replies.

StickyBit

Technical User
Jan 4, 2002
264
CA
I'm a novice to cgi programming, and have my first problem. I'm trying to display some data from a database using the script below. I'm receiving the following error from apache:

[Sat Jun 15 11:17:15 2002] [error] [client 142.214.184.25] Premature end of script headers: /
Here is the script.

#!/usr/bin/perl -w
use DBI;
my ($i, $sth, $dbh, @row, @storage);
$dbh = DBI->connect("dbi:Oracle:ORACLE","scott", "tiger", {
PrintError => 0,
RaiseError => 1
} );
$sth = $dbh->prepare("select author.author_number, author_last,author_first, book_code, sequence_number
from author, wrote
where author.author_number = wrote.author_number");
$sth->execute();
$i=0;
while (@row = $sth->fetchrow_array()){
$storage[$i] = "@row";
$i++;
}

$dbh->disconnect();
print "Content-type: text/html\n\n";
print &quot;<HTML>\n&quot;;
print &quot;<HEAD><TITLE>Database Output</TITLE></HEAD>\n&quot;;
print &quot;<BODY>\n&quot;;
foreach (@storage){
print $_ . &quot;<br>\n&quot;;
}
print &quot;</BODY>\n&quot;;
print &quot;</HTML>\n&quot;;

I'm about to start reading CGI Programming; I thought I would try somthing first.

Thanks,

StickyBit.
 
...The script looks like this on the command line:

Content-type: text/html

<HTML>
<HEAD><TITLE>Database Output</TITLE></HEAD>
<BODY>
20 Zinbardo Philip 0180 1<br>
01 Archer Jeffrey 0189 1<br>
18 Camus Albert 0200 1<br>
16 Lovecraft H.P. 0378 1<br>
04 Francis Dick 079X 1<br>
04 Francis Dick 0808 1<br>
06 King Stephen 1351 1<br>
17 Paz Octavio 1382 1<br>
02 Christie Agatha 138X 1<br>
03 Clarke Arthur C. 2226 1<br>
19 Castleman Riva 2281 1<br>
01 Archer Jeffrey 2766 1<br>
15 Novalis Michael 2908 1<br>
10 Harmon Willis 3350 1<br>
11 Rheingold Howard 3350 2<br>
01 Archer Jeffrey 3743 1<br>
05 Cussler Clive 3906 1<br>
12 Owen Barbara 5163 2<br>
13 Williams Peter 5163 1<br>
07 Pratt Philip 5790 1<br>
08 Adamski Joseph 5790 2<br>
02 Christie Agatha 6128 1<br>
05 Cussler Clive 6328 1<br>
07 Pratt Philip 669X 1<br>
22 Southworth Rod 6908 1<br>
05 Cussler Clive 7405 1<br>
06 King Stephen 7443 1<br>
06 King Stephen 7559 1<br>
07 Pratt Philip 7947 1<br>
23 Wray Robert 7947 2<br>
21 Gimferrer Pere 8092 1<br>
14 Kafka Franz 8720 1<br>
14 Kafka Franz 9611 1<br>
</BODY>
</HTML>
 
I don't see anything obvious. You might try reorganizing your code so you can get a better idea of what is going wrong. I am suspicious that something in you database interaction is failling and you are getting some error messages from it. If that happens before the 'Content-type....' line, then the web server will think you CGI has misbehaved (because it has) and the web server will
tell you the CGI is not producing compliant headers(Premature end of script headers). I would move the 'print Content-type... ' line and opening HTML stuff up early in the code. Get the HTML output started so the web server will see a compliant page start. Then, do your DBI stuff where the errors can get to your browser.

#!/usr/bin/perl -w
# Produce a good header first, then any DBI errors may
# show up in the browser.
print &quot;Content-type: text/html\n\n&quot;;
print &quot;<HTML><HEAD><TITLE></TITLE></HEAD><BODY>\n&quot;;


use DBI;
my ($i, $sth, $dbh, @row, @storage);
$dbh = DBI->connect(&quot;dbi:Oracle:ORACLE&quot;,&quot;scott&quot;, &quot;tiger&quot;, {
PrintError => 0,
RaiseError => 1
} );
$sth = $dbh->prepare(&quot;select author.author_number, author_last,author_first, book_code, sequence_number
from author, wrote
where author.author_number = wrote.author_number&quot;);
$sth->execute();
$i=0;
while (@row = $sth->fetchrow_array()){
$storage[$i] = &quot;@row&quot;;
$i++;
}

$dbh->disconnect();
[red]# [/red]Move initial HTML output up before DBI stuff
[red]# [/red]print &quot;Content-type: text/html\n\n&quot;;
[red]# [/red]print &quot;<HTML>\n&quot;;
[red]# [/red]print &quot;<HEAD><TITLE>Database Output</TITLE></HEAD>\n&quot;;
[red]# [/red]print &quot;<BODY>\n&quot;;

foreach (@storage){
print $_ . &quot;<br>\n&quot;;
}
print &quot;</BODY>\n</HTML>\n&quot;; 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Thanks for your response goBoating, but I have fixed the problem (with help). I didn't set the oracle home env variable in my code e.g. $ENV{ORACLE_HOME}=&quot;/oracle1/app/oracle/product/8.1.7&quot;; I don't understand why I have to do this however, when I do the code works like a charm. I also used use CGI::Carp qw(fatalsToBrowser); to find out where the code died and you're correct, it died at the DBI->connect().

Thank you again.

StickyBit.
 
I see one omission and one problem area. There is no call to CGI (use CGI;) needed to create HTML. A call to use CGI::Carp qw(fatalsToBrowser);[/] and the use of the die directive will help you trap any errors. The problem area is:

while (@row = $sth->fetchrow_array()){
$storage[$i] = &quot;@row&quot;;
$i++;
}

I think you'll be safer by loading the array first like this:

my @row = $sth->fetchrow_array;

foreach $row (@row){
statement....
statement....
statement....
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top