PHP
Public
PDO Select One Row
A safe PDO example for selecting a single row with a prepared statement.
#php
#pdo
#sql
#security
PHP
<?php
function findUserByEmail(PDO $pdo, string $email): ?array
{
$stmt = $pdo->prepare(
'SELECT id, email, firstname, lastname
FROM users
WHERE email = :email
LIMIT 1'
);
$stmt->execute([
'email' => $email,
]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
return $user ?: null;
}
Notes
Prepared statements help protect against SQL injection.