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
| Approach | Important distinction |
|---|---|
| PDOStatement::bindParam | Binds a variable by reference to an input placeholder. |
| Related API / pattern | bindValue captures a value immediately instead of following later variable changes. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming the happy path covers edge cases | The variable is read at execute time; do not pass temporary expressions as references. |
| Using a related API interchangeably | bindValue 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.