Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

hashref problem

by Nik
on Feb 20, 2008 at 19:14 UTC ( #669077=perlquestion: print w/ replies, xml ) Need Help??
Nik has asked for the wisdom of the Perl Monks concerning the following question:

hello i'm trying to change this code:
my $select = $db->prepare( "INSERT INTO memberlog (username, g +amename, host, date) VALUES (?, ?, ?, ?)" ); $select->execute( $userauth, $gamename, $host, $date); my $update = $db->prepare("UPDATE vault SET gamecounter=gameco +unter+1 WHERE gamename=?"); $update->execute($gamename); my $select = $db->prepare("SELECT * from vault WHERE gamename= +?"); $select->execute($gamename); return $select->fetchrow_hashref();
with this one:
$db->do('INSERT INTO memberlog (username, gamename, host, date) VA +LUES (?, ?, ?, ?)', undef, $userauth, $gamename, $host, $date); $db->do('UPDATE vault SET gamecounter=gamecounter+1 WHERE gamename +=?', undef, $gamename); my ($select) = $db->selectrow_array('SELECT * from vault WHERE gam +ename=?', undef, $gamename); return $select->selectrow_hashref;
Can't call method "selectrow_hashref" without a package or object reference at D:\www\cgi-bin\vault.pl line 87. Even if i try return $select; i get an error too. What should i use to return out of the sub the row that select picked?

Thank you.

Comment on hashref problem
Select or Download Code
Re: hashref problem
by friedo (Prior) on Feb 20, 2008 at 19:18 UTC
    selectrow_array returns a list, not a statement handle object. Per the docs,
    This utility method combines "prepare", "execute" and "fetchrow_array" into a single call.
    So your $select just contains the first record in the returned query. It's not an object, therefore you can't call a method on it.

      So the solution is

      $db->do('INSERT INTO memberlog (username, gamename, host, date) VA +LUES (?, ?, ?, ?)', undef, $userauth, $gamename, $host, $date); $db->do('UPDATE vault SET gamecounter=gamecounter+1 WHERE gamename +=?', undef, $gamename); return $db->selectrow_hashref('SELECT * FROM vault WHERE gamename= +?', undef, $gamename);

      Or if you just want the updated gamecounter,

      $db->do('INSERT INTO memberlog (username, gamename, host, date) VA +LUES (?, ?, ?, ?)', undef, $userauth, $gamename, $host, $date); $db->do('UPDATE vault SET gamecounter=gamecounter+1 WHERE gamename +=?', undef, $gamename); return ( $db->selectrow_array('SELECT gamecounter FROM vault WHERE game +name=?', undef, $gamename) )[0];
        Thank you guys

        Is there a basic difference between fetchrow_array and selectrow_array?

        Both commands are used to select multiple rows from a mysql table, isnt this correct?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2014-12-20 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls