Content · Web Development · PHP

PHP PDOStatement::bindParam() Method: Usage and Examples

Binds a variable by reference to an input placeholder. The variable is read at execute time; do not pass temporary expressions as

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

Binds a variable by reference to an input placeholder.

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

What It Does


Binds a variable by reference to an input placeholder.

Syntax and Return Value


PDOStatement::bindParam(Parameter #0 [ <required> string|int $param ], Parameter #1 [ <required> mixed &$var ], Parameter #2 [ <optional> int $type = PDO::PARAM_STR ], Parameter #3 [ <optional> int $maxLength = 0 ], Parameter #4 [ <optional> mixed $driverOptions = null ]): mixed

The return value and exceptional cases depend on the call described here: The variable is read at execute time; do not pass temporary expressions as references.

Example 1: Basic Use


The statement has a real query and result set; database-specific metadata and statement options may be unsupported.

<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$stmt = $pdo->prepare("SELECT name FROM products WHERE id = :id");
$id = 1;
$stmt->bindParam(":id", $id, PDO::PARAM_INT);
$stmt->execute();
echo $stmt->fetchColumn();

Example 2: A Second Scenario


The statement has a real query and result set; database-specific metadata and statement options may be unsupported.

<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$stmt = $pdo->prepare("SELECT name FROM products WHERE id = :id");
$id = 2;
$stmt->bindParam(":id", $id, PDO::PARAM_INT);
$stmt->execute();
echo $stmt->fetchColumn();

Example 3: Practical Checks


The statement has a real query and result set; database-specific metadata and statement options may be unsupported.

<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$stmt = $pdo->prepare("SELECT name FROM products WHERE id = :id");
$id = 3;
$stmt->bindParam(":id", $id, PDO::PARAM_INT);
$stmt->execute();
echo $stmt->fetchColumn();

Important Behavior and Edge Cases


The variable is read at execute time; do not pass temporary expressions as references.

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

Comparison with Related Approaches


ApproachImportant distinction
PDOStatement::bindParamBinds a variable by reference to an input placeholder.
Related API / patternbindValue captures a value immediately instead of following later variable changes.

Common Beginner Mistakes


MistakeCorrect approach
Assuming the happy path covers edge casesThe variable is read at execute time; do not pass temporary expressions as references.
Using a related API interchangeablybindValue captures a value immediately instead of following later variable changes.

Key Takeaways


  • Binds a variable by reference to an input placeholder.
  • The variable is read at execute time; do not pass temporary expressions as references.
  • bindValue captures a value immediately instead of following later variable changes.

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.