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.

Replies are listed 'Best First'.
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];
      A reply falls below the community's threshold of quality. You may see it by logging in.