PDOStatement::bindColumn
(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
PDOStatement::bindColumn — Bind a column to a PHP variable
Description
$column
, mixed &$param
[, int $type
[, int $maxlen
[, mixed $driverdata
]]] ) : boolPDOStatement::bindColumn() arranges to have a particular variable bound to a given column in the result-set from a query. Each call to PDOStatement::fetch() or PDOStatement::fetchAll() will update all the variables that are bound to columns.
Note:
Since information about the columns is not always available to PDO until the statement is executed, portable applications should call this function after PDOStatement::execute().
However, to be able to bind a LOB column as a stream when using the PgSQL driver, applications should call this method before calling PDOStatement::execute(), otherwise the large object OID will be returned as an integer.
Parameters
-
column
-
Number of the column (1-indexed) or name of the column in the result set. If using the column name, be aware that the name should match the case of the column, as returned by the driver.
-
param
-
Name of the PHP variable to which the column will be bound.
-
type
-
Data type of the parameter, specified by the PDO::PARAM_* constants.
-
maxlen
-
A hint for pre-allocation.
-
driverdata
-
Optional parameter(s) for the driver.
Examples
Example #1 Binding result set output to PHP variables
Binding columns in the result set to PHP variables is an effective way to make the data contained in each row immediately available to your application. The following example demonstrates how PDO allows you to bind and retrieve columns with a variety of options and with intelligent 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 example will output:
apple red 150 banana yellow 175 kiwi green 75 orange orange 150 mango red 200 strawberry red 25
See Also
- PDOStatement::execute() - Executes a prepared statement
- PDOStatement::fetch() - Fetches the next row from a result set
- PDOStatement::fetchAll() - Returns an array containing all of the result set rows
- PDOStatement::fetchColumn() - Returns a single column from the next row of a result set
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-pdostatement.bindcolumn.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.