PDO Named Placeholders
debt(d7/e1/b1/t5)
Closest to 'only careful code review or runtime testing' (d7). The detection_hints field states automated detection is 'no', and the code_pattern hint (execute([0=>val, 1=>val]) with many parameters) requires manual code review to spot. No tools in detection_hints.tools are listed that would catch mixing named/positional placeholders or misuse of named placeholders automatically.
Closest to 'one-line patch or single-call swap' (e1). The quick_fix is simply switching to :name placeholders in the SQL string and passing an associative array to execute() — a single-query, single-call change with no cross-cutting impact.
Closest to 'minimal commitment' (b1). Named placeholders are a localised, per-query style choice. They impose no persistent structural tax on the codebase — each query is independent and the choice doesn't propagate architectural constraints to other components.
Closest to 'notable trap' (t5). The misconception field identifies the canonical wrong belief: developers assume named placeholders provide extra security over positional '?' placeholders, but both are equally safe — the difference is purely readability. Additionally, mixing named and positional placeholders in the same query silently fails, and confusing PDO named placeholders with MySQLi (which only supports '?') is a documented gotcha.
Also Known As
TL;DR
Explanation
Named placeholders use :name syntax and are bound by name in the execute() array (with or without the colon prefix). Unlike positional ?, named placeholders can be used in any order and reused multiple times in the same query. They are PDO-only — MySQLi does not support named placeholders. Named placeholders improve readability significantly in INSERT/UPDATE statements with many columns.
Common Misconception
Why It Matters
Common Mistakes
- Mixing named and positional placeholders in the same query — PDO does not support this.
- Forgetting that MySQLi only supports ? placeholders — named placeholders are PDO-only.
- Passing extra keys in the execute() array — PDO ignores them, but it signals a logic error.
Avoid When
- Do not mix named and positional placeholders in the same query — PDO will throw an error.
- Named placeholders are PDO-only — use positional ? when targeting MySQLi.
When To Use
- Use named placeholders for queries with 3+ parameters — greatly improves readability.
- Use named placeholders when the same value appears multiple times in the same query.
Code Examples
// Positional ? — hard to follow with many params
$stmt = $pdo->prepare('INSERT INTO users (email, name, role, created_at) VALUES (?, ?, ?, ?)');
$stmt->execute([$email, $name, 'user', date('Y-m-d H:i:s')]); // which ? is which?
// Named placeholders — readable for multi-column inserts
$stmt = $pdo->prepare(
'INSERT INTO users (email, name, role, created_at)
VALUES (:email, :name, :role, :created_at)'
);
$stmt->execute([
':email' => $email,
':name' => $name,
':role' => 'user',
':created_at' => date('Y-m-d H:i:s'),
]);
// Reuse same placeholder
$stmt = $pdo->prepare('SELECT * FROM logs WHERE user_id = :uid OR created_by = :uid');
$stmt->execute([':uid' => $userId]);