in reply to Help on fetchrow_hashref!

One problem is your assignment %info = {$hashref};. You are assigning an improperly formatted hash reference containing a hash reference to a hash. Were you using warnings, it would have told you this. See Use strict warnings and diagnostics or die. What you likely mean is %info = %{$hashref};. This dereferences the hashref returned by fetchrow_hashref and then stores it in %info. See perlreftut.

I would strongly suggest adding some additional tests to your routine. For example, if your placeholder binding fails (implicitly performed in your execute), your script will continue on since your don't check if your execute was successful. For example, I would suggest changing $sth->execute($lname,$username); to $sth->execute($lname,$username) or die "Execute failed: ", $dbh->errmsg; As well, to test if the row existed after a successful query, how about

if (my $hashref = $sth->fetchrow_hashref) { %info = %{$hashref}; } else { die "lname, username combo did not match any records"; }

Update: Typo fixed, thanks wfsp.

Replies are listed 'Best First'.
Re^2: Help on fetchrow_hashref!
by Anonymous Monk on Nov 09, 2010 at 19:01 UTC
    Is there a way to avoid the usual "Software Error" message and redirect the errors from ""die to a customized error message so the user cant see information that doesn't matter for them?

      Yes. Don't call die but store the message somewhere and then call exit.

        OK, could you give example of that on this code?
        my $sth = $dbh->prepare("select * from mytable where user_name = ? ") +or or &die("Can't select: + ",$DBI::errstr); $sth->execute($user) die "Execute failed: ", $DBI::errstr;

        How could you redirect any errors here to a special page or a sub routine to print a better error to the users?