Ashley Jordan has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys,

i am currently stuck with a script at the moment,

This script is going to pull information from a .csv and then perform a MySQL select on a database, i then need to print one field of the Query as the result.

I have changed the modulenames, database,tables and field names due to DPA reasons.

Please see below;

#!/usr/bin/perl use warnings; use Text::CSV; use DBI; use DBD::mysql; use modulename::DB; use modulelogname::Log; use Term::ReadKey; my $username = get_username(); my $password = get_password(); my $host = get_host(); my $db = new modulename::DB::Hosting "$0.ini"; $db->setDriver('mysql'); $db->setUsername($username); $db->setPassword($password); $db->setHostname($host); $db->setDatabase('database'); my $log = new modulelogname::Log; $log->init('loglog'); $log->log("== log =="); my $file = 'customers.csv'; my $csv = Text::CSV->new(); open (CSV, "<", $file) or die $!; while (<CSV>) { if ($csv->parse($_)) { my @columns = $csv->fields(); $custid = $columns[0]; foreach ($custid) { my $sql = "SELECT email FROM database.table WHERE field= +'$custid';"; $db->query($sql) or $log->log("Failed to update on que +ry $sql"); # $sth->execute() or die "Execute exception: $DBI::errs +tr"; while (@row = $db->fetch_assoc($row[0])) { print "$row[0]...\n"; $log->log("Email found for: $custid"); } } } else{ my $err = $csv->error_input; print "Failed to parse line: $err"; $log->log("Failed to parse line: $err"); } } close CSV; sub get_username { ReadMode 0; print "Enter Username:\n"; $username = <STDIN>; chomp($username); $username=~s/\s//g; return $username; } sub get_password { ReadMode 2; print "Enter Password:\n"; $password = <STDIN>; chomp($password); $password =~s/\s//g; return $password; } sub get_host { ReadMode 0; print "Enter host:\n"; $host = <STDIN>; chomp($host); return $host; }

This is the results i get;

[2014-02-14 14:30:51] Started at Fri Feb 14 14:30:51 2014 [2014-02-14 14:30:51] == customer email script == Hosting DB: [DBI connect dbi:mysql:database=DATABASE;hostname=hostname +, user ashley.jordan] Hosting DB: [connect ok from pid 2997] Hosting DB: SELECT email FROM database.table WHERE field='1598'; Can't call method "fetchrow_hashref" on an undefined value at /usr/share/perl5/modulename/DB.pm line 877, <CSV> line 1 (#1) (F) You used the syntax of a method call, but the slot filled by t +he object reference or package name contains an undefined value. Som +ething like this will reproduce the error: $BADREF = undef; process $BADREF 1,2,3; $BADREF->process(1,2,3); Uncaught exception from user code: Can't call method "fetchrow_hashref" on an undefined value at /usr +/share/perl5/modulename/DB.pm line 877, <CSV> line 1. at /usr/share/perl5/modulename/DB.pm line 877 modulename::DB::fetch_assoc('modulename::DB::Hosting=HASH(0x30cdb6 +8)', undef) called at ./scriptnamet.pl line 45

Replies are listed 'Best First'.
Re: Issue with Print
by GotToBTru (Prior) on Feb 14, 2014 at 15:10 UTC

    Your issue isn't with print, it's with fetchrow_hashref. The following is very suspicious:

    while (@row = $db->fetch_assoc($row[0]))

    You are using a row in an array as an argument to define that array. That doesn't make sense to me. I suspect that somewhere in the bowels of fetch_assoc() is a call to fetchrow_hashref that uses the parameter passed to the former as a parameter for the latter. That would explain the observed error message.

      Hi,

      Thank you guys for your replies

      So would the correct format be fetch_assoc($row());?

      The module shows;

      877 my $row = $sth->fetchrow_hashref;
      Any ideas?
        The error is saying that $sth is undefined. I note you have $sth->execute() commented out in your script. My guess is that you should be calling $db->fetch_assoc($sth). If you can show us more of the fetch_assoc() method from modulename::DB that would help.
Re: Issue with Print
by RichardK (Parson) on Feb 14, 2014 at 14:57 UTC

    You didn't really ask a question, so I'm just guessing here.

    It looks like a bug in modulename::DB::fetch_assoc but as you're hiding its identity there's not much more to say.

    The error message you posted seems pretty clear, what more do you want to know?