← Back to excelora.in Free courses by the Excelora team · Learn by doing
Hire Excelora

Lesson 2 of 4 · 14 min read

Talking to MySQL safely with PDO

The #1 way websites get hacked is SQL injection — when user input is glued directly into a query. Never do this:

// ❌ DANGEROUS
$db->query("SELECT * FROM users WHERE email = '$_POST[email]'");

Use prepared statements — the data travels separately from the SQL, so it can never be executed:

// ✅ SAFE
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email');
$stmt->execute([':email' => $_POST['email']]);
$user = $stmt->fetch();

⚡ Quick check

What stops SQL injection?