从数据库更新数据时出现错误。
sub update
{
my $sql = "UPDATE Data SET NAME = ?, ADDRESS=?, PHONE_NO=?,
DATE_OF_JOINING=?, ROLE_ASSIGN=?, SALARY=? WHERE ID = ?";
my $sth = $dbh->prepare($sql);
print("Enter ID to update : ");
chomp(my $ID = <STDIN>);
print("NAME : ");
chomp(my $NAME = <STDIN>);
print("ADDRESS : ");
chomp(my $ADDRESS = <STDIN>);
print("PHONE_NO : ");
chomp(my $PHONE_NO = <STDIN>);
print("DATE_OF_JOINING : ");
chomp(my $DATE_OF_JOINING = <STDIN>);
print("ROLE_ASSIGN : ");
chomp(my $ROLE_ASSIGN = <STDIN>);
print("SALARY : ");
chomp(my $SALARY = <STDIN>);
$sth->bind_param(1,$NAME);
$sth->bind_param(2,$ADDRESS);
$sth->bind_param(3,$PHONE_NO);
$sth->bind_param(4,$DATE_OF_JOINING);
$sth->bind_param(5,$ROLE_ASSIGN);
$sth->bind_param(6,$SALARY);
$sth->bind_param(7,$ID);
$sth->execute();
print("The record has been updated successfully!");
$sth->finish();
$dbh->disconnect();
}
sub query_links
{
my ($dbh) = @_;
my $sql = "SELECT * FROM Data";
my $sth = $dbh->prepare($sql) or die $DBI::errstr;
$sth->execute() or die $DBI::errstr;
$sth->dump_results();
$sth->finish();
}
DBD::mysql::st execute failed: MySQL server has gone away at mini.plx line 94,
<STDIN> line 9.MySQL server has gone away
答案 0 :(得分:2)
错误消息表示与数据库的连接已丢失。
您没有向我们展示任何创建连接的代码,因此很难知道那里可能出现的问题。您可以考虑在连接到数据库时添加mysql_auto_reconnect
标志。
my $dbh = DBI->connect($dsn, $user, $password, {
mysql_auto_reconnect => 1,
});
您也可以在设置数据库连接后更改此设置。
$dbh->{mysql_auto_reconnect} = 1;
更新:正如ikegami在评论中指出的那样,您在update()
结束时断开了与数据库的连接 - 并且在使用之前您没有显示任何证明您重新连接的代码query_links()
再次连接。这将导致这个问题。