PHP PDOStatement:: bindParam Explanation
- 2021-11-14 05:12:57
- OfStack
PDOStatement::bindParam
PDOStatement:: bindParam--Bind a parameter to the specified variable name (PHP 5 > = 5.1.0, PECL pdo > = 0.1.0)
Description
Grammar
bool PDOStatement::bindParam ( mixed $parameter , mixed &$variable [, int $data_type = PDO::PARAM_STR [, int $length [, mixed $driver_options ]]] )
Bind 1 PHP variable to the corresponding named placeholder or question mark placeholder in the SQL statement used as preprocessing. Different from
PDOStatement::bindValue()
This variable is bound as a reference and is only specified in the
PDOStatement::execute()
Take its value when it is called.
Most parameters are input parameters, that is, parameters are used to build queries in a read-only manner. Some drivers support calling stored procedures and returning data as output parameters, while others support sending and receiving updated data as input/output parameters.
Parameter
parameter
variable
data_type
length
driverdata
driver_options
Return value
Returns TRUE on success or FALSE on failure.
Instances
Execute 1 preprocessing statement using named placeholders
<?php
/* Through the bound PHP Variable execution 1 Bar preprocessing statement */
$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();
?>
Execute 1 preprocessing statement using question mark placeholders
<?php
/* Through the bound PHP Variable execution 1 Bar preprocessing statement */
$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();
?>
Calling a stored procedure with INOUT parameters
<?php
/* Use INOUT Parameter call 1 Stored procedures */
$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");
?>
Summarize