Content · Web Development · PHP

Preventing SQL Injection in PHP

Treat SQL as code and user input as data; use bound parameters for every external SQL value. Never concatenate request values into ORDER BY, table names, or column names without an

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Treat SQL as code and user input as data; use bound parameters for every external SQL value.

The objective is to understand the behavior and operational limits, not only the method name.

What It Does


Treat SQL as code and user input as data; use bound parameters for every external SQL value.

Prerequisites


Use a dedicated development environment and sample data. Where a snippet uses an injected connection, repository, request value or mailer, supply it from your own application setup.

Example 1: Basic Use


A focused working pattern for this topic. Names such as $pdo, $authContext and $repo represent dependencies provided by the application unless initialized in the snippet.

<?php
$allowedSorts = ['newest' => 'created_at', 'name' => 'display_name'];
$sort = $allowedSorts[$_GET['sort'] ?? 'newest'] ?? 'created_at';
$stmt = $pdo->prepare("SELECT id, display_name FROM users WHERE status = :status ORDER BY $sort");
$stmt->execute(['status' => 'active']);
foreach ($stmt as $row) echo htmlspecialchars($row['display_name'], ENT_QUOTES, 'UTF-8');

Example 2: A Second Scenario


A second use case that adds a different operation or data flow specific to this topic.

<?php
$username = (string) ($_POST['username'] ?? '');
$stmt = $pdo->prepare('SELECT id FROM accounts WHERE username = :username');
$stmt->bindValue(':username', $username, PDO::PARAM_STR);
$stmt->execute(); $accountId = $stmt->fetchColumn();

Example 3: Practical Checks


A practical validation, failure or deployment concern specific to this topic.

<?php
$fields = ['email' => 'email', 'name' => 'display_name'];
$field = $fields[$_GET['field'] ?? 'name'] ?? 'display_name';
$stmt = $pdo->prepare("SELECT id, $field FROM accounts WHERE status = ?");
$stmt->execute(['active']);

Important Behavior and Edge Cases


Never concatenate request values into ORDER BY, table names, or column names without an allowlist.

For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.

Comparison with Related Approaches


ApproachImportant distinction
Preventing SQL Injection in PHPTreat SQL as code and user input as data; use bound parameters for every external SQL value.
Related API / patternUse this workflow as a focused starting point rather than a substitute for application-level tests.

Common Beginner Mistakes


MistakeCorrect approach
Assuming the happy path covers edge casesNever concatenate request values into ORDER BY, table names, or column names without an allowlist.
Using a related API interchangeablyUse this workflow as a focused starting point rather than a substitute for application-level tests.

Key Takeaways


  • Treat SQL as code and user input as data; use bound parameters for every external SQL value.
  • Never concatenate request values into ORDER BY, table names, or column names without an allowlist.
  • Use this workflow as a focused starting point rather than a substitute for application-level tests.

Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.

Share this resourceSend this page to someone who may find it useful.