Display MySQL query results from Perl Script

My Perl script should print the results of my query. However, at the moment I am getting an error:

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

I assume the problem is easy to fix. But my perl / MySQL skills have many desires. My script is below:

#!/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";
    }
}

Many thanks!

+4
source share
1 answer

You call fetchrow_arrayin the query string; You want to call it in a statement.

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

Source: https://habr.com/ru/post/1540710/


All Articles