Shifts in SQL Injection Defence Worth Knowing

The way developers defend against SQL injection has shifted a lot over the years. Knowing the history helps you recognise outdated advice when you meet it in old tutorials or forum answers.

Shift 1: from escaping to placeholders

Early advice often said to escape user input: add backslashes before quote characters so they could not end a string. PHP, for example, offered functions such as mysql_real_escape_string. Escaping is easy to get wrong: forget it once, or use the wrong function for a character set, and the hole is back. The old mysql_ functions were later removed from PHP entirely.

The modern answer is parameterised queries, also called prepared statements or placeholders. The SQL and the values travel separately, so the database never treats a value as code.

// PHP with PDO and a placeholder
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = ?');
$stmt->execute([$email]);

What to do: if a tutorial tells you to escape strings and glue them into SQL, find a different tutorial.

Shift 2: libraries made the safe way the easy way

Most modern database libraries put placeholders at the centre of their examples. Python's database libraries follow a common interface (PEP 249) that includes parameters. Node.js libraries such as node-postgres and mysql2 accept an array of values. The safe way is now usually also the shortest way.

Shift 3: ORMs and query builders

ORMs and query builders generate SQL for you and pass values as parameters by default. That removed injection from a lot of everyday code. The remaining risk is in raw SQL escape hatches that let you write SQL text by hand inside an ORM, where it is still possible to glue strings.

Shift 4: security tools in everyday development

Automated tools now look for injection patterns:

OWASP, an open community focused on web security, publishes free guidance, including a widely used cheat sheet on preventing SQL injection.

Shift 5: defence in depth

Modern guidance adds layers around the main fix:

LayerExample
PlaceholdersAlways, for every value
Allow-listsOnly accept known column names for sorting
Least privilegeThe app's account cannot drop tables
Error handlingDo not show raw database errors to visitors

What has not changed

The core idea is the same as it ever was: never let user input become part of the SQL code. Every shift above is a better way of making sure that happens.

Your takeaway

Learn placeholders first, use them every time, and be suspicious of any example that builds SQL with + or string formatting. Our short Q&A on SQL injection answers the follow-up questions beginners usually have.

A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.