SQL injection defense

Contents
How injection reads
Prepare everything
LIKE wildcards safely
LIMIT with ints

How injection reads

Injection happens when user input is glued into SQL text and the database reads part of that input as code. A quote in a name can then change the shape of the query.

The defense is structural and never relies on blocking bad words. Keep data and SQL text apart so input can only ever be a value.

$stmt = $pdo->prepare("SELECT id FROM users WHERE email = :email"); $stmt->execute([":email" => $email]); echo $stmt->rowCount();

Prepare everything

Prepared statements send the SQL shape first and the values separately. The database compiles the query before it ever sees user data.

Use named placeholders for every value that comes from outside. Never build SQL by joining input with dots.

$stmt = $pdo->prepare("INSERT INTO posts (title, body) VALUES (:title, :body)"); $stmt->execute([":title" => $title, ":body" => $body]); echo $pdo->lastInsertId();

LIKE wildcards safely

Search boxes often need partial matches with percent and underscore wildcards. Build the pattern in PHP and send it as one bound value.

The placeholder still carries the whole pattern. User text inside it can never escape into SQL syntax.

$term = "%" . $q . "%"; $stmt = $pdo->prepare("SELECT id FROM posts WHERE title LIKE :term"); $stmt->execute([":term" => $term]); echo $stmt->rowCount();

LIMIT with ints

Limits and offsets cannot always be bound as placeholders, so force them to integers in PHP. Casting drops everything except digits and sign.

Clamp the result to a sane range before it reaches SQL. The query then contains only digits built by your own code.

$limit = max(1, (int)$n); $sql = sprintf("SELECT id FROM posts LIMIT %d", $limit); $stmt = $pdo->query($sql); echo $limit;

Next: Send email

Article author: Arthur Isaev

Related articles
Development with PHP
PHP course: zero to hero in 50 lessons
What is PHP and where it runs
Install PHP 8.5 and run your first command
Your first PHP script
Comments in PHP
Variables and constants
Types in PHP
Strings in PHP
Numbers in PHP
Operators
if and else
switch and match
Ternary, null coalescing and nullsafe
Loops
Arrays
Associative arrays
Functions
Variable scope
include and require
Namespaces
Composer and autoloading
Everyday string functions
Dates and time
JSON in PHP
Reading and writing files
Errors: read, reproduce, fix
Classes and objects
Constructors and promotion
Inheritance
Interfaces and traits
Enums
Magic methods
Attributes
Exceptions
Strict types
Closures and arrow functions
Generators with yield
Forms: GET and POST
HTTP: headers and status codes
Cookies
Sessions
Login with sessions
JSON REST endpoint
Database with PDO
Uploads and .htaccess
Lint like a pro
Clean functions
Debugging
Modern PHP tour: 5 to 8.4
PHP 8.5 for busy developers
Hero roadmap: the whole course on one page
Install PHP 8.5 on Windows 11
Install PHP 8.5 on Ubuntu
Install PHP 8.5 on Rocky Linux
Install PHP 8.5 on macOS
Install PHP 8.5 on FreeBSD
PHP 8.5 in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: