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; }
In reply to Re^2: Global symbol "$dbh" requires explicit package name
by ikegami
in thread Global symbol "$dbh" requires explicit package name
by tudor
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |