Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : PDO Functions : PDOStatement->bindColumn()

PDOStatement->bindColumn()

Bind a column to a PHP variable ()

Example 1770. 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

Change Language


Follow Navioo On Twitter
PDO->beginTransaction()
PDO->commit()
PDO->__construct()
PDO->errorCode()
PDO->errorInfo()
PDO->exec()
PDO->getAttribute()
PDO->getAvailableDrivers()
PDO->lastInsertId()
PDO->prepare()
PDO->query()
PDO->quote()
PDO->rollBack()
PDO->setAttribute()
PDOStatement->bindColumn()
PDOStatement->bindParam()
PDOStatement->bindValue()
PDOStatement->closeCursor()
PDOStatement->columnCount()
PDOStatement->errorCode()
PDOStatement->errorInfo()
PDOStatement->execute()
PDOStatement->fetch()
PDOStatement->fetchAll()
PDOStatement->fetchColumn()
PDOStatement->fetchObject()
PDOStatement->getAttribute()
PDOStatement->getColumnMeta()
PDOStatement->nextRowset()
PDOStatement->rowCount()
PDOStatement->setAttribute()
PDOStatement->setFetchMode()
eXTReMe Tracker