Programming paths / PHP

PHP learning path

Build secure PHP applications with explicit types, prepared database queries, and testable service boundaries.

Starter lesson

Use a prepared query for user input

Never build SQL by concatenating user-provided text. A prepared statement keeps the query structure separate from its values.

<?php

$query = $pdo->prepare(
    'SELECT id, display_name FROM users WHERE email = :email'
);
$query->execute(['email' => $email]);
$user = $query->fetch(PDO::FETCH_ASSOC);

if ($user === false) {
    echo 'No matching account';
} else {
    echo htmlspecialchars($user['display_name'], ENT_QUOTES, 'UTF-8');
}

What to expect

The query uses a bound parameter, and text rendered into HTML is escaped for that output context.

Key idea: Protect both database and HTML boundaries; each requires its own context-appropriate handling.

← Browse all programming paths