in reply to Re: Issue with Print
in thread Issue with Print

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?

Replies are listed 'Best First'.
Re^3: Issue with Print
by tangent (Parson) on Feb 14, 2014 at 15:42 UTC
    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.

      Hi,

      Hope this is what you requested?

      cat DB.pm -n | grep fetch
      17 my $ref = $db->fetch_assoc($sth); 495 while ( my $ref = $self->fetch_assoc($sth) ) { 550 my $ref = $self->fetchOne('SELECT sql FROM sqlite_master + WHERE tbl_name = ?', $src_table); 661 selectOne($q) is a shortcut to prepare and execute a structu +re select, and fetch the first row 668 $self->select(@q)->fetchrow_hashref; 740 while ( my @arr = $sth->fetchrow_array() ) { 802 while ( my $ref = $self->fetch_assoc($sth) ) { 840 my $ref = $self->fetchOne($self->_count_sql($table, $whe +re)); 856 =item fetchOne 858 fetchOne($q) is a shortcut to prepare and execute a query, a +nd fetch the first row 862 sub fetchOne { 864 $self->query(@q)->fetchrow_hashref; 867 =item fetch_assoc 873 sub fetch_assoc { 877 my $row = $sth->fetchrow_hashref; 879 $self->die("fetching row failed ".$sth->err);
        As you can see from line 802
        while ( my $ref = $self->fetch_assoc($sth) )
        the method fetch_assoc() expects a statement handler. The usual way to do it would be:
        my $dbh = DBI->connect(@db_args); my $sql = "SELECT email FROM database.table"; my $sth = $dbh->prepare($sql); $sth->execute(); while (my $hashref = $sth->fetchrow_hashref) { ...
        But obviously your module is intervening, so you need to find how to acquire the statement handler $sth from the module - possibly:
        my $sth = $db->query($sql)...
        Also you need to work out what type of data structure fetch_assoc() is returning - I don't think it's returning an array (@row) as you expect in your code, more likely a hash reference, use Data::Dumper to find out.