从Perl脚本显示MySQL查询结果

时间:2014-05-15 13:37:31

标签: mysql sql perl

我的Perl脚本应该打印查询结果。但是,目前我收到错误:

Can't locate object method "fetchrow_array" via package "SELECT * FROM SERVER" (perhaps you forgot to load "SELECT * FROM SERVER"?) at updateDB.pl line 32

我认为问题很容易解决..但我的perl / MySQL技能有很多不足之处。我的脚本如下:

#!/usr/bin/perl

use DBI;
use DBD::mysql;

use strict;
use warnings;

MySQL("SELECT * FROM SERVER");

# define subroutine to submit MySQL command
sub MySQL
{
    # establish connection with 'serverDNA' database
    my $connection = DBI->connect("DBI:mysql:database=serverDNA;host=localhost");

    my $query = $_[0];  #assign argument to string

    my $statement = $connection->prepare($query);   #prepare query

    $statement->execute();   #execute query

    #loop to print MySQL results
    while (my @row = $query->fetchrow_array)
    {
            print "@row\n";
    }
}

非常感谢!

1 个答案:

答案 0 :(得分:5)

您在查询字符串上调用fetchrow_array;你想在声明中称呼它。

while (my @row = $statement->fetchrow_array)
{
  print "@row\n";
}