Easy Tutorial
❮ Func Math Decbin Func Mysqli Multi Query ❯

PDOStatement::bindColumn

PHP PDO Reference Manual

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 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: Column information is not always available to PDO until the statement is executed. Portable applications should call this method (function) after PDOStatement::execute().


Parameters

column

param

type

maxlen

driverdata


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 effective way to make the data from each row immediately available in the application. The following example demonstrates how PDO can bind and retrieve columns with various options and default values.

<?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

PHP PDO Reference Manual

❮ Func Math Decbin Func Mysqli Multi Query ❯