Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Can't crack a DBI Error:

by Hammy (Scribe)
on Sep 24, 2002 at 02:30 UTC ( #200277=perlquestion: print w/replies, xml ) Need Help??
Hammy has asked for the wisdom of the Perl Monks concerning the following question:

I continually get the following error when running the code below. It still runs the code and returns me the results, but it gives an error. Any ideas? It does not matter what query I run - even simple queries give the error. One more thing, it give the error on the fetchrow. Thanks

flexreportmap.cgi: DBD::mysql::st fetchrow_arrayref failed: fetch() wi +thout execute() at line 311, <HTML> line 193. $sth = $dbh->prepare($sql); unless ($sth->execute) { fatal ("SQL query failed: " . $sth->errstr . "\n$sql"); } $ref = $sth->fetchrow_arrayref(); $sth->finish; return $ref;

Replies are listed 'Best First'.
Re: Can't crack a DBI Error:
by Abstraction (Friar) on Sep 24, 2002 at 03:05 UTC
    I may be way off base on this one, so this may not be much help, but I recall having this same error and removing the call to finish seemed to clear things up.

    Take a look at finish for more information.

    You may also want to looking into using selectrow_arrayref instead of $sth->fetchrow() if you only want 1 row.

    Anyway, hope this helps.

Re: Can't crack a DBI Error:
by spartacus9 (Beadle) on Sep 24, 2002 at 02:34 UTC
    instead of using "unless($sth->execute)", try using:
    $sth->execute() or die "SQL query failed: $sth->errstr \n$sql";
    I can't say why your method would not work but I can say I have not had a problem at all with the "or die" method above.
      Thanks, but that is not where the problem is. The error occurs on the fetch - I switched it around as sugggested and still get the same error - any suggestions?
Re: Can't crack a DBI Error:
by rdfield (Priest) on Sep 24, 2002 at 11:19 UTC
    Have you checked the return code for the prepare?


Re: Can't crack a DBI Error:
by Daruma (Curate) on Sep 24, 2002 at 14:50 UTC

    I tested the following (using DBD::Oracle, mind you)

    use strict; use DBI; use diagnostics; my $user = 'foo'; my $pass = 'bar'; my $dbh= DBI->connect( "dbi:Oracle:database", "$user", "$pass", ) or die "Can't connect to the database: $DBI::errstr\n"; my $sql = ' Select employee_id, employee_last_name, employee_grade from employees'; my $sth = $dbh->prepare($sql) or die "Can't prepare SQL statement: $DBI::errstr\n"; unless ($sth->execute) { fatal ("SQL query failed: " . $sth->errstr . "\n$sql"); } my $ref = $sth->fetchrow_arrayref() or die "Can't fetch: $DBI::errstr\n"; $sth->finish; print "@$ref[0] - @$ref[1] - @$ref[2]\n"; $dbh->disconnect;
    If I remove the $sth->finish; line, It returns an error when attempting to disconnect against an open database handle... the fetch is still fetching... With the finish method, I do return just one row. Does that meet your need?

    Beyond the fetchrow_array / finish vs. selectrow_array difference pointed out by Abstraction++, The code works fine for me.

    Are you using strict, warning & diagnostics?? Is your code giving you feedback for every DBI package usage? (Have it die and give you feedback...

Re: Can't crack a DBI Error:
by MZSanford (Curate) on Sep 24, 2002 at 21:27 UTC
    I have to point back to rdfield ... it may seem simple, but it simple tends to work.
    from the frivolous to the serious

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://200277]
Approved by defyance
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2018-06-23 03:01 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.