PDOStatement::bindColumn

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)

PDOStatement::bindColumn Bind a column to a PHP variable

Descrierea

public PDOStatement::bindColumn ( mixed $column , mixed &$param , int $type = ? , int $maxlen = ? , mixed $driverdata = ? ) : bool

PDOStatement::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.

Notă:

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.

Parametri

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.

Valorile întoarse

Întoarce valoarea true în cazul succesului sau false în cazul eșecului.

Exemple

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

Exemplul de mai sus va afișa:

apple   red     150
banana  yellow  175
kiwi    green   75
orange  orange  150
mango   red     200
strawberry      red     25

A se vedea și

add a note add a note

User Contributed Notes 1 note

up
-11
Moses Blumenstiel
3 years ago
I wanted to efficiently bind columns and then put everything into one array.

<?php

        $columns
= array(
           
'DEVICE_CONFIG' => '',
           
'CONSUMABLE_INFO' => '',
           
'DEVICE_UNIQUE_ID' => '',
           
'STATUS' => '',
           
'STATUS_MESSAGE' => '',
           
'LAST_MESSAGE_TIME' => '',
           
'DEVICE_DESCRIPTION' => ''
       
);

       
$statement = $this->connection->prepare($sql);

       
$statement->execute(); 

       
$x = 1;       
        foreach(
$columns as $column => $index){
            $
$column = $index;           
            eval(
'$statement->bindColumn(' . $x++ . ', $' . $column . ');');           
        }       
       
       
$output= array();
        while(
$statement->fetch(\PDO::FETCH_BOUND)){
           
$temp = array();
            foreach(
$columns as $columnName => $val){               
                eval(
"\$data = \$$columnName;");
               
$temp[$columnName] = $data;
            }
           
$output[] = $temp;
        } 
?>
To Top