CodeBlanks Logo Icon CodeBlanks

Named placeholders

PHP course · Advanced · Lesson 13/92

Learn to write queries with named placeholders in the :name form.

The :name named placeholder

A named placeholder is written as :name. It matches by name, not by order like ?. For example 'SELECT name FROM products WHERE category = :cat AND price >= :minPrice' has two named placeholders, :cat and :minPrice.

For queries with many parameters, name-based matching prevents order mistakes and improves readability. Each name must be unique.

You cannot mix ? and :name in the same template; use either all positional or all named.

execute() takes an associative array

With named placeholders, execute() takes an associative array. The keys match the placeholder names exactly. For example execute([':cat' => 'stationery', ':minPrice' => 30]) binds values to the :cat and :minPrice placeholders.

The key order does not matter; writing :minPrice first still works. This adds flexibility.

Missing or extra keys throw an error; every placeholder must receive a value.

Exercise Filter with named placeholders

Build the template 'SELECT name FROM products WHERE category = :cat AND price >= :minPrice' with prepare(). Run it with execute([':cat' => 'stationery', ':minPrice' => 30]). Fetch all rows with fetchAll(PDO::FETCH_ASSOC) and print the name field of each row.

Show example solution
cat AND price >= :minPrice"). Call $stmt->execute([':cat' => 'stationery', ':minPrice' => 30]). Get $rows = $stmt->fetchAll(PDO::FETCH_ASSOC). Print with foreach ($rows as $row) { echo $row['name'] . "\n"; }.
Example solution:
<?php
// Build a query with :name named placeholders

$pdo = new PDO('sqlite::memory:');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec("CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT, category TEXT, price INTEGER)");
$pdo->exec("INSERT INTO products (name, category, price) VALUES ('Pen', 'stationery', 25)");
$pdo->exec("INSERT INTO products (name, category, price) VALUES ('Notebook', 'stationery', 40)");

$stmt = $pdo->prepare("SELECT name FROM products WHERE category = :cat AND price >= :minPrice");
$stmt->execute([':cat' => 'stationery', ':minPrice' => 30]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    echo $row['name'] . "\n";
}

Test yourself

  1. Match the placeholder types

    :cat
    Named placeholder
    ?
    Positional placeholder
    [':cat' => v]
    execute() associative array
    :minPrice
    Name-matched key
  2. Complete the named execute call

    $stmt->____([':cat' => 'stationery', ':min' => 30]);
    Show answer
    $stmt->execute([':cat' => 'stationery', ':min' => 30]);
  3. What is the advantage of named placeholders?

    • Always runs the query faster
    • Lets you write fewer parameters
    • Matches by name instead of order, improving readability
    • Only used in UPDATE queries
    Show answer

    Matches by name instead of order, improving readability — Correct! :name placeholders match by name, not order; this improves readability in multi-parameter queries.

All lessons in Advanced