The bindColumn() method in PHP PDO binds individual columns from a database result set directly to PHP variables. Each fetch operation automatically updates those variables, making it efficient for processing large datasets.
bindColumn() maps a column to a PHP variable.PDO::FETCH_BOUND fetch mode.After preparing and executing a statement, columns are bound using bindColumn(). When rows are fetched using PDO::FETCH_BOUND, the bound variables receive updated values automatically.
// Connecting to database and binding result columns
<?php
try {
$pdo = new PDO("mysql:host=localhost;dbname=testdb", "root", "");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare("SELECT id, name, email FROM users");
$stmt->execute();
$stmt->bindColumn(1, $id);
$stmt->bindColumn(2, $name);
$stmt->bindColumn(3, $email);
while ($stmt->fetch(PDO::FETCH_BOUND)) {
echo "ID: $id - Name: $name - Email: $email<br>";
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
Each database row updates the variables $id, $name, and $email. The loop prints user details directly without accessing an array.
Think of bindColumn() as a live wire: each fetch sends new data through the same variable. This is especially useful when streaming records one-by-one.
bindParam() when needed.name column.created_at.