PHP PDOStatement:: bindValue Explanation

  • 2021-11-14 05:12:38
  • OfStack

PDOStatement::bindValue

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

Description

Grammar


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

Bind 1 value to the corresponding named placeholder or question mark placeholder in the SQL statement used as preprocessing.

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.

value

Bind to the value of the parameter

data_type

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

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

Summarize


Related articles: