Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DBD::mysql::st fetchrow_array failed: fetch() without execute()

Tags:

perl

fetchrow_hashref working fine, but when i use fetchrow_array getting following Error.

#!/usr/bin/perl

use warnings;
use DBI;

$DB_name    = 'database';
$DB_user    = 'root';
$DB_pwd     = '';
my $dsn = 'dbi:mysql:avm:localhost:3306';

$dbh = DBI->connect($dsn,"$DB_user","$DB_pwd");

print "\nConnection error: $DBI::errstr\n\n";

$sth  = $dbh->prepare("SELECT * FROM tblmanufacturer");
$sth->execute();

while ( ($id,$name) = $sth->fetchrow_array() ) 
{
        print "$id\t\t $name \n";
}

$sth->finish();

$dbh->disconnect();

DBD::mysql::st fetchrow_array failed: fetch() without execute() at

like image 992
bharanikumar Bs Avatar asked Sep 29 '11 14:09

bharanikumar Bs


2 Answers

I always use "die" on error at both "execute" and "prepare".

$sql = $dbh->prepare( $query ) or die "Unable to prepare $query" . $dbh->errstr;
$sql->execute() or die "Unable to execute '$query'.  " . $sql->errstr;
like image 63
mmrtnt Avatar answered Sep 22 '22 02:09

mmrtnt


Check the return value of execute() and/or print "$DBI::errstr\n\n" and see if execute is failing.

print $sth->execute(),"\n";
like image 43
dlamotte Avatar answered Sep 24 '22 02:09

dlamotte