Keyboard Navigation
W
A
S
D
or arrow keys · M for map · Q to exit
← Back to The Archive
2003activeinjection

Use Prepared Statements for SQL Queries

Separate SQL structure from user-supplied data using parameterized queries

2 min read · Official Docs
Status

Still Active

Why It Made Sense

Parameterized queries structurally prevent SQL injection by ensuring user input is always treated as data, never as SQL syntax. The database engine compiles the query structure separately from the parameter values.

By 2003, the security community had converged on a consensus: the only reliable defense against SQL injection was to structurally separate the query from its parameters. Prepared statements accomplish this by sending the SQL template and the user data in separate channels — the database compiles the query plan first, then binds the data as literal values. The data can never modify the query structure because it is never part of the query structure.

Why it replaced concatenation: Escaping and sanitization proved brittle — they required developers to anticipate every possible bypass in every database engine. Prepared statements moved the defense from the application (where it was inconsistently applied) to the database protocol (where it was structurally enforced).

Why it persists as best practice: Despite ORMs and query builders adding higher-level abstractions, prepared statements remain the foundational mechanism. When an ORM generates a query, it uses prepared statements internally. When developers drop to raw SQL (and they always do), prepared statements are the last line of defense.

The limitation: Prepared statements protect data parameters but cannot parameterize table names, column names, or ORDER BY directions. These require whitelisting — a separate discipline that many developers forget once they believe parameterization has solved the problem.

Sources Where This Was Taught
OWASP SQL Injection Prevention Cheat SheetPHP PDO DocumentationJava JDBC PreparedStatement API
Languages Affected
PHPJavaPythonC#JavaScript