I've got a weird problem here involving DBI under
mod_perl.

I have a simple Perl script that works from the
command line:
-----------------------------------------------------
#!/usr/local/bin/perl
use strict;
use DBI;
        my ($dbh, $sth, $f, $head) = undef;
        $dbh = DBI->connect("dbi:mysql:news", "root", "") || die "Can't open database";
        $sth = $dbh->prepare("SELECT headline, filename FROM story") || 
                die "Can't do execute!";
        $sth->execute;
        print "We got " . $sth->rows() . " records\n";
        while ( ($head, $f) = $sth->fetchrow()) {
                print "We read $head, $f\n";
                }
        $dbh->disconnect();
-------------------------------------------------------
When run, this program does as expected and prints
a few lines of data from the database.  I turned 
this little script into a simple mod_perl module,
thus:

-------------------------------------------------------
package IEO::headline;
# File: IEO/headline.pm

use strict;
use Apache::Constants qw(:common);
use CGI::Carp;
use CGI::Carp qw(fatalsToBrowser);
use DBI;

sub handler {
        my $r = shift;
        my ($dbh, $sth, $f, $head) = undef;
        $r->content_type('text/html');
        $r->send_http_header;
        carp "------------------------------\nAbout to open database";
        $dbh = DBI->connect("DBI:mysql:database=news", "root", "") || 
                die "Can't open database";
        $r->print("The db is now open<BR>\n");
        carp "The db is now open";
        $sth = $dbh->prepare("SELECT headline, filename FROM story") || 
                die "Can't do prepare!";
        $r->print("We have done the prepare<BR>\n");
        carp "We've done the prepare";
        $sth->execute();
        $r->print("We got " . $sth->rows() . " records \n");

        carp "We got " . $sth->rows() . " records";
        while ( ($head, $f) = $sth->fetchrow()) {
                $r->print("We read $head, $f\n");
                }
        $dbh->disconnect();

        $r->print("</BODY>\n</HTML>\n");
        return OK;
}
-------------------------------------------------------

When I put the appropriate links into my httpd.conf
file and go to the proper URL I get a page that reads:
"The db is now open"
"We have done the prepare"
"We got -2 records"

and the system error log records:


[Wed Oct 27 17:30:23 1999] null: ------------------------------
[Wed Oct 27 17:30:23 1999] null: About to open database at /dev/null line 0
[Wed Oct 27 17:30:23 1999] null: The db is now open at /dev/null line 0
[Wed Oct 27 17:30:23 1999] null: We've done the prepare at /dev/null line 0
[Wed Oct 27 17:30:23 1999] null: DBD::mysql::st execute failed: ase Selected at 
/usr/local/apache/smtp/perl-scripts/IEO/headline.pm line 25.
[Wed Oct 27 17:30:23 1999] null: We got -2 records
[Wed Oct 27 17:30:23 1999] null: DBD::mysql::st fetchrow failed: fetch() without 
execute() at /usr/local/apache/smtp/perl-scripts/IEO/headline.pm line 29.

For some reason the $sth->execute() works ok from
the command line but not inside my mod_perl code.
I'm sure I'm overlooking something simple, but repeated
scans of the eagle book don't shed any light on the
problem.

Could anyone offer any pointers into what I'm doing 
wrong?  I'm about to pull out the few hairs I've 
got left!

Dan Mahoney
[EMAIL PROTECTED]

Reply via email to