Skip to content

Instantly share code, notes, and snippets.

@HotelCalifornia
Last active September 5, 2015 22:43
Show Gist options
  • Save HotelCalifornia/6d0845f9236cdac1d07f to your computer and use it in GitHub Desktop.
Save HotelCalifornia/6d0845f9236cdac1d07f to your computer and use it in GitHub Desktop.
# instead of testing for the user existing, test if the user doesn't exist
function user_not_exists($username)
{
$connection = $this->get_connection();
$stmt = $connection->prepare("SELECT `username` FROM users WHERE username = ?;");
$stmt->bindValue(1, $username, PDO::PARAM_STR);
$stmt->execute();
return empty($stmt->fetchAll(PDO::FETCH_ASSOC));
# PDOStatement::fetchAll returns an array, so it's easiest to test if the array is empty (no results found)
# if you want to keep the function as user_exists(...), try returning $stmt->fetchAll(PDO::FETCH_ASSOC)[0]
# so that you get the actual result, and not just the array
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment