PHP PDOStatement::bindColumn Tutorial
last modified April 19, 2025
The PDOStatement::bindColumn method in PHP binds a PHP variable to a column in the result set. This allows direct access to column values through variables as rows are fetched.
Basic Definition
PDOStatement::bindColumn binds a PHP variable to a column in the result set. The variable will automatically be updated with the column's value when fetching rows.
Syntax: PDOStatement::bindColumn(mixed $column, mixed &$param, int $type = PDO::PARAM_STR, int $maxlen = 0, mixed $driverdata = null): bool
.
The $column can be a column number (1-indexed) or name. $param is the PHP variable to bind. $type specifies the data type for the parameter.
Basic Column Binding
This example shows how to bind columns to variables for a simple query.
<?php declare(strict_types=1); try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->prepare('SELECT id, name, email FROM users'); $stmt->execute(); $stmt->bindColumn('id', $id); $stmt->bindColumn('name', $name); $stmt->bindColumn('email', $email); while ($stmt->fetch(PDO::FETCH_BOUND)) { echo "ID: $id, Name: $name, Email: $email\n"; } } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
This binds three columns to variables. When fetch(PDO::FETCH_BOUND) is called, the variables are automatically updated. The column names are used to specify which columns to bind.
Binding by Column Number
This demonstrates binding columns using their numeric positions instead of names.
<?php declare(strict_types=1); try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->prepare('SELECT id, name FROM users'); $stmt->execute(); $stmt->bindColumn(1, $id); $stmt->bindColumn(2, $name); while ($stmt->fetch(PDO::FETCH_BOUND)) { echo "ID: $id, Name: $name\n"; } } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
Columns are bound by their position in the result set (1-indexed). The first column is id (position 1), the second is name (position 2). This approach works even if column names change.
Specifying Data Types
This shows how to specify data types when binding columns to variables.
<?php declare(strict_types=1); try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->prepare('SELECT id, name, salary FROM users'); $stmt->execute(); $stmt->bindColumn('id', $id, PDO::PARAM_INT); $stmt->bindColumn('name', $name, PDO::PARAM_STR); $stmt->bindColumn('salary', $salary, PDO::PARAM_STR); // Using STR for decimal while ($stmt->fetch(PDO::FETCH_BOUND)) { echo "ID: $id (".gettype($id)."), Name: $name (".gettype($name)."), Salary: $salary\n"; } } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
The third parameter specifies the data type. PDO::PARAM_INT ensures the id is an integer. PDO::PARAM_STR is used for strings. For decimal numbers, STR is often used as there's no specific decimal type.
Binding to Class Properties
This demonstrates binding columns to properties of an object.
<?php declare(strict_types=1); class User { public $id; public $name; public $email; } try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->prepare('SELECT id, name, email FROM users'); $stmt->execute(); $user = new User(); $stmt->bindColumn('id', $user->id, PDO::PARAM_INT); $stmt->bindColumn('name', $user->name, PDO::PARAM_STR); $stmt->bindColumn('email', $user->email, PDO::PARAM_STR); while ($stmt->fetch(PDO::FETCH_BOUND)) { echo "User: {$user->id}, {$user->name}, {$user->email}\n"; } } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
Columns are bound to properties of a User object. Each fetch updates the object's properties. This approach is useful when working with object-oriented code.
Using Different Fetch Modes
This shows how bindColumn works with different fetch modes.
<?php declare(strict_types=1); try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->prepare('SELECT id, name FROM users'); $stmt->execute(); $stmt->bindColumn(1, $id); $stmt->bindColumn(2, $name); // FETCH_BOUND updates bound variables while ($stmt->fetch(PDO::FETCH_BOUND)) { echo "Bound: ID: $id, Name: $name\n"; } // Regular fetch doesn't update bound variables $stmt->execute(); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo "Regular: ID: {$row['id']}, Name: {$row['name']}\n"; } } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
Bound variables are only updated when using PDO::FETCH_BOUND. Regular fetch methods like FETCH_ASSOC don't affect bound variables. The example shows both approaches for comparison.
Binding Only Specific Columns
This demonstrates binding only some columns while fetching others normally.
<?php declare(strict_types=1); try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->prepare('SELECT id, name, email, created_at FROM users'); $stmt->execute(); $stmt->bindColumn('name', $name); $stmt->bindColumn('email', $email); while ($row = $stmt->fetch(PDO::FETCH_BOUND)) { echo "Name: $name, Email: $email\n"; echo "Full row: "; print_r($row); } } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
Only name and email columns are bound to variables. The other columns are still available in the $row array. This shows you can mix bound columns with regular fetching.
Binding with Maximum Length
This shows how to use the maxlen parameter to limit the size of returned data.
<?php declare(strict_types=1); try { $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'password'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->prepare('SELECT id, name, bio FROM users'); $stmt->execute(); $stmt->bindColumn('name', $name, PDO::PARAM_STR, 20); $stmt->bindColumn('bio', $bio, PDO::PARAM_STR, 100); while ($stmt->fetch(PDO::FETCH_BOUND)) { echo "Name (max 20 chars): $name\n"; echo "Bio preview (max 100 chars): " . substr($bio, 0, 30) . "...\n"; } } catch (PDOException $e) { echo "Error: " . $e->getMessage(); }
The fourth parameter limits the maximum length of data returned for each column. The name is limited to 20 characters, bio to 100. This can be useful for large text fields where you only need a preview.
Best Practices
- Use meaningful variable names: Match column names when possible.
- Specify data types: Especially for numbers to avoid string conversion.
- Consider memory usage: Binding is efficient for large result sets.
- Combine with other fetch methods: When you need both bound variables and full rows.
- Error handling: Always check for errors after binding.
Source
This tutorial covered the PDOStatement::bindColumn method with practical examples showing different ways to use it in database operations.
Author
List all PHP PDO Functions.