What Is SQL Injection?
SQL injection is a vulnerability where untrusted input is inserted directly into a SQL query, letting an attacker alter the query's meaning to read, modify,...
Published November 16, 2026
SQL injection happens when user input is concatenated into a SQL query so an attacker can change its meaning, reading or destroying data. Prevent it by always using parameterised queries (prepared statements).
The concept
If you build SELECT ... WHERE name = '$name', input like ' OR '1'='1 turns the condition into always-true and returns every row.
Prepared statements send the query structure and the values separately, so input can never become SQL. Escaping by hand is error-prone.
How the query is rewritten
Practical examples
Vulnerable PHP
$r = $pdo->query("SELECT * FROM users WHERE name = '$name'");Safe PHP (PDO)
$s = $pdo->prepare('SELECT * FROM users WHERE name = ?');
$s->execute([$name]);Safe Node (mysql2)
const [rows] = await db.execute('SELECT * FROM users WHERE name = ?', [name])ORMs and query builders
User::where('name', $name)->get(); // parameter boundCommon mistakes
- Concatenating input into SQL.
- Parameterising values but not table or column names (whitelist those).
- Showing raw database errors to users.
Key takeaways
- Never build SQL from strings.
- Use prepared statements.
- Whitelist identifiers and use least-privilege DB users.
FAQ
Do ORMs protect me?
Yes when using bound parameters; raw queries remain risky.
How do I test for it?
Try quotes in inputs, and use scanners on systems you own.