Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Security boundaries

Bind data values to parameters in a prepared SQL statement, keeping them separate from query syntax.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

$stmt = $db->prepare($sql); $stmt->execute([$value]);

Bind data values to parameters in a prepared SQL statement, keeping them separate from query syntax. A marker can stand for a complete data value, not an identifier or keyword. When presenting retrieved text in HTML, encode it for the output context. SQL parameters and HTML encoding solve different boundary problems.

$db=new PDO("sqlite::memory:");$stmt=$db->prepare("SELECT ? AS label");$stmt->execute(["Tea & Milk"]);$label=$stmt->fetchColumn();echo htmlspecialchars($label,ENT_QUOTES | ENT_SUBSTITUTE,"UTF-8");
Tea & Milk

Examples

<?php
$db=new PDO("sqlite::memory:");$stmt=$db->prepare("SELECT ? AS label");$stmt->execute(["Tea & Milk"]);$label=$stmt->fetchColumn();echo htmlspecialchars($label,ENT_QUOTES | ENT_SUBSTITUTE,"UTF-8");

Result

Tea &amp; Milk

Sources