Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm trying to get some data from database using method fetchrow_array, but it seems one of the return variables is empty.

my $db=$database->prepare($query) or croak $db->errstr; $database->bind_param(':user',$user) or croak $db->errstr; $database->bind_param(':num',$num) or croak $db->errstr; $datbase->execute() or croak $db->errstr; ($user_type,$user_num)=$database->fetchrow_array();

After this, I do an if where i verify if either one of these variables is empty, and for some reason one of them is. But, if I connect to the database directly I get a result. Can someone help me with this? Thanks.

Replies are listed 'Best First'.
Re: Fetchrow_array returns empty value
by davido (Cardinal) on Mar 24, 2014 at 16:21 UTC

    I don't think this has been stated yet, which surprises me because I saw it mentioned in the CB an hour ago:

    The bind_param method is a method of statement handle objects. So is the execute method, as well as the fetchrow_array method. The prepare method is a database handle object method. So your code would be better written as:

    my $sth = $database->prepare($query) or croak $database->errstr; $sth->bind_param( ':user', $user ) or croak $sth->errstr; $sth->bind_param( ':num', $num ) or croak $sth->errstr; ( $user_type, $user_num ) = $sth->fetchrow_array;

    In your case, $db is holding a statement handle. But I think that's a confusing choice of variable name; it defies the convention demonstrated in the DBI documentation, and makes it hard to guess whether it represents a database handle (your $database variable), or a statement handle. That's just a style/maintainability issue. The big issue I see is using database handles to call statement-handle object methods. That's not going to do what you want.


    Dave

      Thanks Dave, I will try this.
Re: Fetchrow_array returns empty value
by GotToBTru (Prior) on Mar 24, 2014 at 14:29 UTC

    We really need to see the contents of $query.

      my $query ="select tp.user_type, nm.user_num from users us inner join nums nm on nm.user_num=us.user_num inner join type tp on us.user_type=tp.user_type where nm.user_id =:user_id and nm.num=:num";

        Your parameter binding has :user. Your query has :user_id.

Re: Fetchrow_array returns empty value
by erix (Prior) on Mar 24, 2014 at 14:47 UTC
    $datbase->execute()

    datbase? That should probably be database.

    use strict; would have cought that ...

      Yes you're right. I replaced that, but i still have the same problem.
Re: Fetchrow_array returns empty value
by erix (Prior) on Mar 24, 2014 at 15:46 UTC

    Please provide some basic info.

    What DBMS, what DBD? What versions?

      Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi