tags:

views:

76

answers:

2

I have installed DBD::Pg version 2.17.1 but still still getting error while using below code

$res = $conn->prepare($query);
$res = $res->execute();
@tuple = $res->fetchrow_array;

error:

Can't call method "fetchrow_array" without a package or object reference at test.pl line 69.

Please suggest.

+4  A: 

$res is not an object instance of DBI. Try running ref $res: it should return an empty string.

Executes a previously prepared statement. In addition to UPDATE, DELETE, INSERT statements, for which it returns always the number of affected rows, the execute method can also be used for SELECT ... INTO table statements.

Your $res most likely contains the number of "affected rows".

$sth   = $conn->prepare($query);
$nrows = $sth->execute();
@tuple = $sth->fetchrow_array;
Pedro Silva
i tried as per your suggestion but no success. still getting the same error.
Space
I had a typo: `@tuple = $res->fetchrow_array;` should be `@tuple = $sth->fetchrow_array;`
Pedro Silva
+6  A: 

You should not be saying

$res = $res->execute();

$res before that statement is the statement handle that you will need to use to call fetchrow_array after the execute succeeds, but the above is replacing it with the return value of execute(), which is the number of rows affected if successful or undef if the execute failed. Instead, store that return value in a separate variable if desired and check it for success before calling fetchrow_array.

ysth
I tried as per your suggestion but no success.
Space
@Space, then you're doing it wrong, because this is your problem. Post your revised code.
cjm