Guides › HTML / PHP sites › Security & hacks

SQL Injection in PHP: What It Is and How to Prevent It With Prepared Statements

Never put user input directly into SQL. Use prepared statements with PDO or mysqli.

Reference guide. This guide is written from established practice and the official documentation of the tools involved. We have not yet tested every step on a live demo site. Back up your site before you change anything.

SQL injection happens when user input becomes part of the SQL command, letting attackers read or change your database.

Unsafe

$q = "SELECT * FROM users WHERE email = '" . $_POST['email'] . "'";

Safe with PDO

$stmt = $pdo->prepare('SELECT * FROM users WHERE email = ?');
$stmt->execute([$_POST['email']]);
$rows = $stmt->fetchAll();

Rules

Connection problems

See our PHP database guide.

Related guides

Did this solve it? If not, email hello@sitefixlab.org with the exact error text and we will look into it. Try the problem finder.

Last updated 2026-11-21.