Binds a result column to a PHP variable filled by later fetch operations.
The objective is to understand the behavior and operational limits, not only the method name.
What It Does
Binds a result column to a PHP variable filled by later fetch operations.
Syntax and Return Value
PDOStatement::bindColumn(Parameter #0 [ <required> string|int $column ], 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 target variable is passed by reference; bind before fetching rows.
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, price FROM products WHERE id = :id");
$stmt->execute(["id" => 1]);
$stmt->bindColumn(1,$name);
$stmt->bindColumn(2,$price);
while ($stmt->fetch(PDO::FETCH_BOUND)) { echo $name . ":" . $price; }
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, price FROM products WHERE id = :id");
$stmt->execute(["id" => 2]);
$stmt->bindColumn(1,$name);
$stmt->bindColumn(2,$price);
while ($stmt->fetch(PDO::FETCH_BOUND)) { echo $name . ":" . $price; }
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, price FROM products WHERE id = :id");
$stmt->execute(["id" => 3]);
$stmt->bindColumn(1,$name);
$stmt->bindColumn(2,$price);
while ($stmt->fetch(PDO::FETCH_BOUND)) { echo $name . ":" . $price; }
Important Behavior and Edge Cases
The target variable is passed by reference; bind before fetching rows.
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::bindColumn | Binds a result column to a PHP variable filled by later fetch operations. |
| Related API / pattern | bindValue binds an input parameter to a SQL placeholder instead. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming the happy path covers edge cases | The target variable is passed by reference; bind before fetching rows. |
| Using a related API interchangeably | bindValue binds an input parameter to a SQL placeholder instead. |
Key Takeaways
- Binds a result column to a PHP variable filled by later fetch operations.
- The target variable is passed by reference; bind before fetching rows.
- bindValue binds an input parameter to a SQL placeholder instead.
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.