PDOStatement::bindColumn
PDOStatement::bindColumn - Bind 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 a result set. Every call to PDOStatement::fetch() or PDOStatement::fetchAll() updates all variables bound to columns.
**Note:** Before the statement is executed, PDO's information about columns is not always available, so 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.
Parameters
** column**
The column number in the result set (1-based index) or column name. If using a column name, note that the name should match the case of the column name returned by the driver.
** param**
The PHP variable name to bind to the column
** type**
The data type of the parameter specified by the PDO::PARAM_* constant.
** maxlen**
Pre-allocation hint.
** driverdata**
Optional driver parameter.
Return Value
Returns TRUE on success, or FALSE on failure.
Example
Output a bound result set to a PHP variable
Binding columns in a result set to PHP variables is an effective way to make the data contained in each row immediately available in the application. The example below demonstrates how PDO binds and retrieves columns using various options and default values.
prepare($sql);
$stmt->execute();
/* Bind by column number */
$stmt->bindColumn(1, $name);
$stmt->bindColumn(2, $colour);
/* By column name binding */
$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 Manual