Display MySQL Query Results from Perl Script

mysql, perl, sql

Solution

You're calling `fetchrow_array` on the query string; you want to call it on the statement.

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

Problem

My Perl script is supposed to print the results from my query. However, at the moment I'm getting the 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 imagine the problem is an easy one to fix.. but my perl / MySQL skills have much to be desired. 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"; } } ``` Thanks so much!

Original source