mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
Added example for binding values in an array using arbitrary unnamed placeholders in a prepared statement.
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@327811 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
99d2c44d4f
commit
4ff7ef044b
1 changed files with 24 additions and 1 deletions
|
@ -138,7 +138,6 @@ $sth->execute(array($calories, $colour));
|
|||
</programlisting>
|
||||
</example>
|
||||
|
||||
|
||||
<example><title>Execute a prepared statement with question mark placeholders</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
|
@ -153,6 +152,30 @@ $sth->bindParam(1, $calories, PDO::PARAM_INT);
|
|||
$sth->bindParam(2, $colour, PDO::PARAM_STR, 12);
|
||||
$sth->execute();
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
|
||||
<example><title>Execute a prepared statement using array for IN clause</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
/* Execute a prepared statement using an array of values for an IN clause */
|
||||
$params = array(1, 21, 63, 171);
|
||||
/* Create a string for the parameter placeholders filled to the number of params */
|
||||
$place_holders = implode(',', array_fill(0, count($values), '?'));
|
||||
|
||||
/*
|
||||
This prepares the statement with enough unnamed placeholders for every value
|
||||
in our $params array. The values of the $params array are then bound to the
|
||||
placeholders in the prepared statement when the statement is executed.
|
||||
This is not the same thing as using PDOStatement::bindParam() since this
|
||||
requires a reference to the variable. PDOStatement::execute() only binds
|
||||
by value instead.
|
||||
*/
|
||||
$sth = $dbh->prepare("SELECT id, name FROM contacts WHERE id IN ($place_holders)");
|
||||
$sth->execute($params);
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
|
|
Loading…
Reference in a new issue