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

Parameter identifier. For preprocessing statements that use named placeholders, it should be parameter names in the form of name. For preprocessing statements that use question mark placeholders, it should be the parameter position indexed with 1.

variable

The name of the PHP variable bound to the SQL statement parameter.

data_type

Use the PDO:: PARAM_* constant to explicitly specify the type of the parameter. To return an INOUT parameter from a stored procedure, you need to set the PDO:: PARAM_INPUT_OUTPUT bit using the bitwise OR operator for the data_type parameter.

length

Pre-allocation prompt.

driverdata

The length of the data type. This length must be explicitly set to indicate that the parameter is an OUT parameter of a stored procedure.

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


Related articles: