PHP PDOStatement:: execute Explanation

  • 2021-11-14 05:11:17
  • OfStack

PDOStatement::execute

PDOStatement:: execute-Executes 1 preprocessing statement (PHP 5 > = 5.1.0, PECL pdo > = 0.1.0)

Description

Grammar


bool PDOStatement::execute ([ array $input_parameters ] )

Executes the preprocessed statement. If the preprocessed statement contains parameter markers, you must choose one of the following:

Call PDOStatement::bindParam() Bind an PHP variable to a parameter marker: If any, pass the input value and get the output value by associating the variable bound by the parameter marker Or pass an array of input parameter values only

Parameter

input_parameters

An array of 1 elements and 1 parameter bound in the SQL statement to be executed. All values are treated as PDO:: PARAM_STR. Cannot bind multiple values to a single parameter; For example, you cannot bind two values to a single named parameter in the IN () clause. The value of the binding cannot exceed the specified number. If there are more key names in input_parameters than specified in PDO:: prepare () preprocessed SQL, this statement will fail and issue 1 error.

Return value

Returns TRUE on success or FALSE on failure.

Instances

Execute 1 preprocessing statement for binding variables


<?php
/*  By binding  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 (named parameter) with 1 array containing inserted values


<?php
/*  By passing 1 Execute an array containing inserted values 1 Bar preprocessing statement  */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
 FROM fruit
 WHERE calories < :calories AND colour = :colour');
$sth->execute(array(':calories' => $calories, ':colour' => $colour));
?>

Execute 1 preprocessing statement (placeholder) with 1 array containing inserted values


<?php
/*  By passing 1 Execute an array of inserted values 1 Bar preprocessing statement  */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
 FROM fruit
 WHERE calories < ? AND colour = ?');
$sth->execute(array($calories, $colour));
?>

Execute a preprocessing statement for 1 question mark placeholder


<?php
/*  By binding  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();
?>

Execute a preprocessing statement with an IN clause using an array


<?php
/*  Use 1 Execute the values of an array 1 Article contains  IN  Preprocessing statement of clause  */
$params = array(1, 21, 63, 171);
/*  Create 1 Filled with and params Strings with the same number of placeholders  */
$place_holders = implode(',', array_fill(0, count($params), '?'));
/*
  For  $params  For each value in the array, the statement to be preprocessed contains enough unnamed placeholders   . 
  Statement is executed,  $params  The values in the array are bound to placeholders in the preprocessing statement. 
  This and the use of  PDOStatement::bindParam()  No 1 Sample, because it needs 1 Reference variables. 
 PDOStatement::execute()  Only as an alternative to binding by value. 
*/
$sth = $dbh->prepare("SELECT id, name FROM contacts WHERE id IN ($place_holders)");
$sth->execute($params);
?>

Summarize


Related articles: