LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Perl Noob + DBI : Can't call method without a package or object ref (http://www.linuxquestions.org/questions/programming-9/perl-noob-dbi-cant-call-method-without-a-package-or-object-ref-668780/)

urzumph 09-09-2008 06:36 PM

Perl Noob + DBI : Can't call method without a package or object ref
 
The following code:

Code:

use DBI;
<snip>
my %preparedStatements;
<snip>
$preparedStatements{"latest_config_id"} = $db_handle->prepare("SELECT MAX(sequence) FROM configs WHERE id = ?")
    or die $db_handle->errstr;
<snip>
sub get_latest_config_id($) {
    my ($id) = @_;
    my $ps = $preparedStatements{"latest_config_id"};
   
    my $result = $ps->execute(($id))
        or die $ps->errstr;
   
    my @row = $result->fetchrow_array();
    return $row[0];
}

Dies with "Can't call method "fetchrow_array" without a package or object reference at /usr/lib/perl5/liblegacydb.pl line 38."

Do I need to cast $result somehow?

Thanks in advance.

nadroj 09-09-2008 07:13 PM

first, ive used perl only for the past few months (and not very often) and i have never used DBI--only did basic perl stuff. however, the execute function i believe returns a true or false value. you then try to call the "fetchrow_array()" function on this variable, but i think you should be using $ps not $result.

urzumph 09-09-2008 07:47 PM

Ah, I understand now. You're quite right -

"For SELECT statements, execute simply ``starts'' the query within the database engine. Use one of the fetch methods to retrieve the data after calling execute. The execute method does not return the number of rows that will be returned by the query (because most databases can't tell in advance), it simply returns a true value."

(from http://www.mathematik.uni-ulm.de/hel...5/doc/DBI.html)

Thanks.


All times are GMT -5. The time now is 04:42 AM.