Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Can't crack a DBI Error:

by Hammy (Scribe)
on Sep 24, 2002 at 02:30 UTC ( [id://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 library.pl 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?

    rdfield

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

    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...

    -Daruma
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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://200277]
Approved by defyance
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-25 02:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found