Latest web development tutorials

PDOStatement :: bindParam

PHP PDO Reference Manual PHP PDO Reference Manual

PDOStatement :: bindParam - bind a parameter to the specified variable name (PHP 5> = 5.1.0, PECL pdo> = 0.1.0)


Explanation

grammar

bool PDOStatement::bindParam ( mixed $parameter , mixed &$variable [, int $data_type = PDO::PARAM_STR [, int $length [, mixed $driver_options ]]] )

Bind a PHP variable to an SQL statement as a pretreatment corresponding named or question mark placeholder placeholder. Unlike PDOStatement :: bindValue (), this variable is bound as a reference, and only :: execute in PDOStatement () is invoked to whichever value.

Most of the parameters are input parameters, that is, the parameters used to build the query read-only mode. Some drivers support call a stored procedure and return data as output parameters, some support as the input / output parameters, both send and receive data after the update.


parameter

parameter
Parameter identifier. For use named placeholders prepared statements should be similar to: name the form parameter name. For question mark placeholders in the prepared statement, it should be based on 1-indexed parameter.

variable
Bound to the SQL statement parameters PHP variable name.

data_type
Use PDO :: PARAM_ * constants to explicitly specify the type of the parameter. From a stored procedure returns an INOUT parameter, you need to use the bitwise OR operator to set the PDO :: PARAM_INPUT_OUTPUT bit data_type parameter.

length
Pre-allocated tips.

driverdata
Length data type. To show the parameter is an OUT parameter of a stored procedure, you must explicitly set this length.

driver_options


return value

Successful return TRUE, or on failure returns FALSE.


Examples

Executive using a prepared statement named placeholders

<?php
/* 通过绑定的 PHP 变量执行一条预处理语句  */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < :calories AND colour = :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();
?>

Implementation of a question mark placeholders in the prepared statement

<?php
/*  通过绑定的 PHP 变量执行一条预处理语句 */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < ? AND colour = ?');
$sth->bindParam(1, $calories, PDO::PARAM_INT);
$sth->bindParam(2, $colour, PDO::PARAM_STR, 12);
$sth->execute();
?>

INOUT parameter using a stored procedure call

<?php
/* 使用 INOUT 参数调用一个存储过程 */
$colour = 'red';
$sth = $dbh->prepare('CALL puree_fruit(?)');
$sth->bindParam(1, $colour, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 12);
$sth->execute();
print("After pureeing fruit, the colour is: $colour");
?>

PHP PDO Reference Manual PHP PDO Reference Manual