Jump to content

Check if $PreparedStatement->execute has returned any SELECT results


cmor

Recommended Posts

Hopefully a very simple question.  Looking to see if ->execute on a prepared SELECT statement returns any results.  ->execute simply returns true if it was successful (results or not).  It also returns an array with a count of 1 using ->fetch  Here's where I'm at:

public static function authenticate($user='',$password='')
{
global $db;
$sql = 'Select * FROM users WHERE username = :user AND password = :password LIMIT 1';
$stmt = $db->prepare($sql);
$stmt->bindValue(':user',$user);
$stmt->bindValue(':password',sha1($password));  
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return count($row)!=1 ? array_shift($row) : false;
}

 

It works but seems awkward.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.