我想打印我的 perl 脚本的所有 sql 结果,我已经连接到数据库,我想显示我的 sql 查询的结果:
MySQL("SELECT * FROM test");
# define subroutine to submit MySQL command
sub MySQL
{
#Connect to the database.
my $dbh = DBI->connect("DBI:mysql:database=database;host=ip",
"login", 'password',
{'RaiseError' => 1});
my $query = $_[0]; #assign argument to string
my $sth = $prepare($query); #prepare query
$sth->execute(); #execute query
while (my @row = $sth->fetchrow_array)
{
print "@row\n";
}
}
我有这个错误:
Global symbol "$prepare" requires explicit package name at test3.pl line 34.
syntax error at test3.pl line 34, near "$prepare("
Global symbol "$sth" requires explicit package name at test3.pl line 36.
Execution of test3.pl aborted due to compilation errors.
最佳答案
改变这一行:
my $sth = $prepare($query);
到
my $sth = $dbh->prepare($query);
并且不要忘记在 while 循环之后关闭 $sth
和 $dbh
句柄:
$sth->finish;
$dbh->disconnect;
关于linux - perl - 打印 sql 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58694393/