in reply to Global symbol "$dbh" requires explicit package name

You're 'using' strict.pm, yet you are attempting to create global variables by writing "$dbh = ... ". Just change that line to my $dbh =  DBI -> connect($DSN,$user,$pass);, and the same for $sth etc. That makes them local to the file and thus scoped.

C.

Replies are listed 'Best First'.
Re^2: Global symbol "$dbh" requires explicit package name
by ikegami (Patriarch) on Oct 23, 2004 at 12:33 UTC

    To clear things up concerning $sth, you'll have to say my $sth; before the eval instead of just adding my before the assignment like castaway suggested you do for $dbh, or else the variable won't exist when it comes to calling $sth->excute.

    use CGI; use CGI::Carp qw(fatalsToBrowser); use DBI; use strict; my $db_name="scent"; my $o = new CGI; my $tip2 =$o -> param("tip2"); my $DSN="dbi:mysql:$db_name"; my $user="root"; my $pass=""; my $dbh = DBI -> connect($DSN,$user,$pass); <------ #return; my $SQL= "select * from par;"; my $sth; <------ eval { $sth = $dbh -> prepare($SQL); }; if ($@) { $dbh -> disconnect; print $@ } else { $sth -> execute; }

    or even:

    use CGI; use CGI::Carp qw(fatalsToBrowser); use DBI; use strict; my $db_name="scent"; my $o = new CGI; my $tip2 =$o -> param("tip2"); my $DSN="dbi:mysql:$db_name"; my $user="root"; my $pass=""; my $dbh = DBI -> connect($DSN,$user,$pass); <------ #return; my $SQL= "select * from par;"; my $sth = eval { $dbh -> prepare($SQL) }; <------ if ($@) { $dbh -> disconnect; print $@ } else { $sth -> execute; }