On Fri, 21 Jun 2002 00:34:11 -0700 Emma Grant <[EMAIL PROTECTED]> wrote:

> Hey Everyone, 
>  
> I have a strange problem. 
>  
> I have been working with mysql and dbi most of the day, I am just new to
> it all.  
>  
> I am able to connect my  database using the DBI module. When I run the
> script on my command line it runs fine, and I get the data that I asked
> for. But when I try to run this through a web browser, the cgi script
> seems to stop in the middle and not print out any more text or data. 
>  
> By placing simple text messages between the commands, I worked out that
> the script stops outputting data after the 'prepare' statement. 
>  
> EG. 
>  
>
#!/usr/bin/perl -w
use strict;
use warnings;   # Alternative to -w in the #! line
> Use DBI; 
use DBI;
>  
> Print "dbi"; 
print "dbi\n";
>  
> $dbh = DBI->connect('DBI:mysql:mydb');

# You could enable implicit error checking
my $dbh = DBI -> connect( 'DBI:mysql:mydb', '', '',
   { AutoCommit => 1, PrintErrors => 0, RaiseErrors => 1 } );

>  
> print "connect"; 
>  
> $statement = "Select * from customers"; 
>  
> $sth = $dbh->prepare($statement);

# If you don't set RaiseErrors,
#    you should check all statements for errors
my $sth = $dbh -> prepare()
   or die "prepare failed: $DBI::errstr\n";
>  
> print "prepare"; 
>  
>  
> $sth ->execute;

$sth -> execute or die "execute failed: $DBI::errstr";

# It is pointless to prepare and execute a SELECT
#    without fetching

# finish() is only needed if you stop fetching rows
#    from a SELECT before all rows have been received.

> $sth->finish();
> $dbh->disconnect();
>  
> The print comments do not appear after 'connect'. Yet if I comment out
> the prepare statement, they appear all the way through the script. 
>  
> Any Ideas? 

Your first problem is that you are not using 'use strict;' or 'use
warnings;',  Between the two, many minor problems can be caught before they
become big ones.

Your second problem is that you are not checking for errors.  I've added
suggested alternate statements to your code above that demonstrates.

You aren't fetching your results.

There is lots of useful information in the fine manual, run 'perldoc DBI'
to read it.
-- 
Mac :})
** I normally forward private questions to the appropriate mail list. **
Ask Smarter: http://www.tuxedo.org/~esr/faqs/smart-questions.html
Give a hobbit a fish and he eats fish for a day.
Give a hobbit a ring and he eats fish for an age.


Reply via email to