Forum Moderators: open
if ($contents{'username'}) {
($found_password, $found_email) =
$dbh->selectrow_array("SELECT password, email FROM users WHERE username=? LIMIT 1", undef,
$contents{'username'}) or die "Couldn't execute SELECT FROM users: " . $dbh->errstr;
} Couldn't execute SELECT FROM users: at example.cgi line 383.
It works fine unless username isn't found, and then I just get an error message:
The first step I'd take would be to verify the contents of $contents{'username'} to make sure it contains EXACTLY what you expect.
I need to run a test but I think an empty list will return false in scalar context. If so, that would be the reason why the die statement is being executed.
If called in a list context, it returns the first row of data from the statement. The $statement parameter can be a previously prepared statement handle, in which case the prepare is skipped.
If any method fails, and "RaiseError" is not set, selectrow_array will return an empty list.
If called in a scalar context for a statement handle that has more than one column, it is undefined whether the driver will return the value of the first column or the last. So don't do that.
($found_password, $found_email) = ...; if ($contents{'username'}) {
($found_password, $found_email) =
$dbh->selectrow_array("SELECT password, email FROM users WHERE username=? LIMIT 1", undef,
$contents{'username'}) or die "Couldn't execute SELECT FROM users: " . $dbh->errstr;
}
If the username isn't found, then the statement is being executed successfully and the die statement won't be called. If it is being called, then the statement is failing.
That is the correct way to assign a list. What I'm not sure about (since my perl is a bit rusty) is what DBI is using when when testing the or conditional portion of the statement.