best may to use $dbh->disconnect; and $sth->finish;

best may to use $dbh->disconnect; and $sth->finish;

am 06.09.2006 13:33:02 von mamod20

Please advise, I have the following example and want to know the best
way to use $dbh->disconnect; and $sth->finish;

--------------
$sql_host="localhost";
$sql_dataname = "database";
$sql_userid = "root";
$sql_password = "password";

&connect_sql;

sub connect_sql{
use DBI;
$DSN = "DBI:mysql:$sql_dataname:$sql_host";
$dbh = DBI->connect($DSN,$sql_userid,$sql_password)
|| &error("Database connection failed: $DBI::errstr") unless $dbh;
return;
}

sub my_sql{
eval{ $sth = $dbh->prepare($SQL); };
if($@){$dbh->disconnect;&error("Database error $@");exit;}
else {$sth->execute; }
return ($sth);
}

foreach $x(@xs){
$SQL = "INSERT INTO articles VALUES ('ID','$x')";
&my_sql;
$sth->finish;
}

$dbh->disconnect;

or is it better to put $sth->finish; out of the loop above? like

foreach $x(@xs){
$SQL = "INSERT INTO articles VALUES ('ID','$x')";
&my_sql;
}
$sth->finish;
$dbh->disconnect;