PHP PDOStatement:: bindColumn Explanation
- 2021-11-14 05:13:05
- OfStack
PDOStatement::bindColumn
PDOStatement:: bindColumn--Bind 1 column to 1 PHP variable (PHP 5 > = 5.1.0, PECL pdo > = 0.1.0)
Description
Grammar
bool PDOStatement::bindColumn ( mixed $column , mixed &$param [, int $type [, int $maxlen [, mixed $driverdata ]]] )
Arranges a specific variable to bind to a given column in a query result set. Each call
PDOStatement::fetch()
Or
PDOStatement::fetchAll()
All variables bound to columns are updated.
Note: PDO column information is not always available before the statement is executed, and portable applications should be in
PDOStatement::execute()
This function (method) is then called.
However, in order to bind an LOB column as a stream when using the PgSQL driver, the application must call the
PDOStatement::execute()
This method is called before, otherwise the large object OID is returned as an integer.
Parameter
column
param
type
maxlen
driverdata
Return value
Returns TRUE on success or FALSE on failure.
Instances
Bind the result set output to an PHP variable
Binding the columns in the result set to an PHP variable is an effective 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 multiple options and defaults.
<?php
function readData($dbh) {
$sql = 'SELECT name, colour, calories FROM fruit';
try {
$stmt = $dbh->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 routine outputs:
apple red 150
banana yellow 175
kiwi green 75
orange orange 150
mango red 200
strawberry red 25
Summarize