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?
Prepared statements separate code from data, so input can’t change the query.