Latest web development tutorials

PDOStatement :: bindValue

PHP PDO Reference Manual PHP PDO Reference Manual

PDOStatement :: bindValue - bind a value to a parameter (PHP 5> = 5.1.0, PECL pdo> = 0.1.0)


Explanation

grammar

bool PDOStatement::bindValue ( mixed $parameter , mixed $value [, int $data_type = PDO::PARAM_STR ] )

Bind a value to the SQL statement as a pretreatment corresponding named or question mark placeholder placeholder.


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.

value
Bound to the value of the parameter

data_type
Use PDO :: PARAM_ * constants to explicitly specify the type of the parameter.


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->bindValue(':calories', $calories, PDO::PARAM_INT);
$sth->bindValue(':colour', $colour, PDO::PARAM_STR);
$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->bindValue(1, $calories, PDO::PARAM_INT);
$sth->bindValue(2, $colour, PDO::PARAM_STR);
$sth->execute();
?>

PHP PDO Reference Manual PHP PDO Reference Manual