PDOStatement::bindColumn — Bind a column to a PHP variable (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
bool PDOStatement::bindColumn ( mixed $column , mixed &$param [, int $type [, int $maxlen [, mixed $driverdata ]]] )
Arranges for a specific variable to be bound to a given column in a query result set. Each call to PDOStatement::fetch() or PDOStatement::fetchAll() will update all variables bound to the column.
Note: PDO information about columns is not always available before statement execution. Portable applications should call this function (method) after PDOStatement::execute(). However, when using the PgSQL driver, to be able to bind a LOB column as a stream, the application must call this method before calling PDOStatement::execute(), otherwise the large object OID is returned as an integer.
Column number (indexed starting from 1) or column name in the column result set. If using column names, note that the names should be consistent with the case of the column names returned by the driver.
param The PHP variable name that will be bound to the column
typeThe data type of the parameter specified by PDO::PARAM_* constants.
maxlen preallocation hint.
Optional parameter of driverdata driver.
Returns TRUE on success, or FALSE on failure.
Binding columns in a result set to PHP variables is an efficient way to make the data contained in each row immediately available in your application. The following example demonstrates how PDO binds and retrieves columns with various options and default values.
<?phpfunction readData($dbh) { $sql = 'SELECT name, colour, calories FROM fruit'; try { $stmt = $dbh->prepare($sql); $stmt->execute(); /* Pass columns Number binding*/ $stmt->bindColumn(1, $name); $stmt->bindColumn(2, $colour); /* Binding by column name*/ $stmt->bindColumn('calories', $cals); while ($row = $stmt->fetch(PDO::FETCH_BOUND)) { $data = $name . "t" . $colour . "t" . $cals . "n"; print $data; } } catch (PDOException $e) { print $e->getMessage(); }}readData($dbh);?>
The above routine will output:
apple red 150banana yellow 175kiwi green 75orange orange 150mango red 200strawberry red 25