Latest web development tutorials

PDOStatement :: bindColumn

PHP PDO Reference Manual PHP PDO Reference Manual

PDOStatement :: bindColumn - PHP bind one to one variable (PHP 5> = 5.1.0, PECL pdo> = 0.1.0)


Explanation

grammar

bool PDOStatement::bindColumn ( mixed $column , mixed &$param [, int $type [, int $maxlen [, mixed $driverdata ]]] )

Arrangements for a particular variable bound to a given query result set columns. Each call to PDOStatement :: fetch () or PDOStatement :: fetchAll () will update all the variables bound to columns.

Calling this function (method) in the statement before execution PDO information about a column is not always available, portable applications should :: execute in PDOStatement () NOTE: After.
However, when using PgSQL drive, in order to be able to bind a LOB column as stream, the application must :: calling PDOStatement execute () before calling this method, otherwise the large object OID is returned as an integer.


parameter

column
The result set column number (starting at 1 index) or column name. If you use a column name, the name should pay attention to the column names returned by the drive case consistent.

param
To bind to the column PHP variable names

type
By PDO :: PARAM_ * constants specified parameter data types.

maxlen
Pre-allocated tips.

driverdata
Drive optional parameters.


return value

Successful return TRUE, or on failure returns FALSE.


Examples

The result set output to PHP variables Binding

Bound columns in the result set to PHP variables is an effective method of data contained in each row immediately available in the application to make. The following example demonstrates how to use a variety of options and PDO defaults bind and retrieve columns.

<?php
function readData($dbh) {
  $sql = 'SELECT name, colour, calories FROM fruit';
  try {
    $stmt = $dbh->prepare($sql);
    $stmt->execute();

    /*  通过列号绑定  */
    $stmt->bindColumn(1, $name);
    $stmt->bindColumn(2, $colour);
    
    /*  通过列名绑定  */
    $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 PHP PDO Reference Manual