Pdostatement Bindcolumn
# PDOStatement::bindColumn
[PHP PDO Reference](#)
PDOStatement::bindColumn β Binds a column to a PHP variable (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
* * *
## Description
### Syntax
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 the result set. Each call to PDOStatement::fetch() or PDOStatement::fetchAll() will update all variables bound to columns.
**Note:** Information about columns is not always available to PDO before a statement is executed. Portable applications should call this method (function) 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.
* * *
## Parameters
**column**
The column number in the result set (1-indexed) or the column name. If using the column name, note that the name should match the case of the column name as returned by the driver.
**param**
The name of the PHP variable to be bound to the column.
**type**
The data type of the parameter, specified by the PDO::PARAM_* constants.
**maxlen**
A hint for preallocation.
**driverdata**
Optional parameter for the driver.
* * *
## Return Value
Returns TRUE on success or FALSE on failure.
* * *
## Examples
### Binding result set output to PHP variables
Binding columns in the 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 can bind and retrieve columns using various options and defaults.
prepare($sql); $stmt->execute(); /* Bind by column number */ $stmt->bindColumn(1, $name); $stmt->bindColumn(2, $colour); /* Bind 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 example will output:
apple red 150 banana yellow 175 kiwi green 75 orange orange 150 mango red 200 strawberry red 25
* * PHP PDO Reference](#)
YouTip