<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.pdo.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'pdo.prepare.php',
    1 => 'PDO::prepare',
    2 => 'Pr&eacute;pare une requ&ecirc;te &agrave; l\'ex&eacute;cution et retourne un objet',
  ),
  'up' => 
  array (
    0 => 'class.pdo.php',
    1 => 'PDO',
  ),
  'prev' => 
  array (
    0 => 'pdo.lastinsertid.php',
    1 => 'PDO::lastInsertId',
  ),
  'next' => 
  array (
    0 => 'pdo.query.php',
    1 => 'PDO::query',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'reference/pdo/pdo/prepare.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="pdo.prepare" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDO::prepare</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8, PHP 8,PECL pdo &gt;= 0.1.0)</p><p class="refpurpose"><span class="refname">PDO::prepare</span> &mdash; <span class="dc-title">
   Prépare une requête à l&#039;exécution et retourne un objet
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-pdo.prepare-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>PDO::prepare</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$query</code></span>, <span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$options</code><span class="initializer"> = []</span></span>): <span class="type"><span class="type"><a href="class.pdostatement.php" class="type PDOStatement">PDOStatement</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>


  <p class="para rdfs-comment">
   Prépare une requête SQL à être exécutée par la méthode
   <span class="methodname"><a href="pdostatement.execute.php" class="methodname">PDOStatement::execute()</a></span>. Le modèle de déclaration peut contenir
   zéro ou plusieurs paramètres nommés (<code class="literal">:nom</code>) ou marqueurs
   (<code class="literal">?</code>) pour lesquels les valeurs réelles seront substituées
   lorsque la requête sera exécutée.
   L&#039;utilisation à la fois des paramètres nommés ainsi que les marqueurs est
   impossible dans un modèle de déclaration ; seul l&#039;un ou l&#039;autre style de paramètre.
   Il faut utiliser ces paramètres pour lier les entrées utilisateurs et ne pas les inclure
   directement dans la requête.
  </p>
  <p class="para">
   Il faut inclure un marqueur avec un nom unique pour chaque valeur que
   l&#039;on souhaite passer dans la requête lorsque l&#039;on appelle
   <span class="methodname"><a href="pdostatement.execute.php" class="methodname">PDOStatement::execute()</a></span>. Il n&#039;est pas possible d&#039;utiliser
   un marqueur avec deux noms identiques dans une requête préparée, à moins que 
   le mode émulation ne soit actif.
  </p>
<blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Les marqueurs de paramètres peuvent représenter uniquement un littéral de 
    données complet.
    Ni une partie de littéral, ni un mot clé, ni un identifiant, ni toute autre 
    requête arbitraire ne peuvent être liés en utilisant les paramètres.       
    Par exemple, il n&#039;est pas possible d&#039;associer plusieurs valeurs à un seul marqueur de nom entrant, dans la clause IN() d&#039;une requête SQL. 
   </p>
  </p></blockquote>
  <p class="para">
   Appeler <span class="methodname"><strong>PDO::prepare()</strong></span> et
   <span class="methodname"><a href="pdostatement.execute.php" class="methodname">PDOStatement::execute()</a></span> pour les requêtes
   qui doivent être exécutées plusieurs fois avec différentes valeurs de
   paramètres optimisent les performances de l&#039;application en autorisant
   le pilote à négocier côté client et/ou serveur avec le cache des requêtes
   et les meta-informations. De plus appeler <span class="methodname"><strong>PDO::prepare()</strong></span> et
   <span class="methodname"><a href="pdostatement.execute.php" class="methodname">PDOStatement::execute()</a></span> aident à prévenir les attaques par
   injection SQL en éliminant le besoin de protéger les paramètres manuellement.
  </p>
  <p class="para">
   PDO émule les requêtes préparées / les paramètres liés pour les pilotes
   qui ne le supportent pas nativement, et peut également réécrire les
   paramètres nommés ou les marqueurs en quelques choses de plus
   approprié, si le pilote supporte un style et pas l&#039;autre.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    L&#039;analyseur utilisé pour les déclarations préparées
    émulées et pour réécrire les paramètres nommés ou du style point
    d&#039;interrogation supporte l&#039;échappement antislash non standard pour les
    guillemets simples et doubles. Cela signifie que les guillemets de fin
    précédés d&#039;un antislash ne sont pas reconnus comme tels, ce qui
    peut entraîner une mauvaise détection des paramètres provoquant l&#039;échec de la
    déclaration préparée lors de son exécution. Un contournement est de ne pas
    utiliser les requêtes émulées pour de telles requêtes SQL, et d&#039;éviter de
    réécrire les paramètres en utilisant un style de paramètre qui est
    supporté nativement par le pilote.
   </span>
  </p></blockquote>
  <p class="para">
   À partir de PHP 7.4.0, les points d&#039;interrogation peuvent être échappés en les dédoublant.
   Cela signifie que la chaîne <code class="literal">??</code> sera traduite en <code class="literal">?</code>
   lors de l&#039;envoi de la requête à la base de données.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-pdo.prepare-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">query</code></dt>
     <dd>
      <p class="para">
       Doit être un modèle de requête SQL valide pour le serveur de base de données ciblée.
      </p>
     </dd>
    
    
     <dt><code class="parameter">options</code></dt>
     <dd>
      <p class="para">
       Ce tableau contient une ou plusieurs paires clé=&gt;valeur pour définir
       les valeurs des attributs pour l&#039;objet <span class="classname"><a href="class.pdostatement.php" class="classname">PDOStatement</a></span>
       que cette méthode retourne. Il est possible d&#039;utiliser ceci pour définir la valeur
       <code class="literal">PDO::ATTR_CURSOR</code> à
       <code class="literal">PDO::CURSOR_SCROLL</code> pour demander un curseur scrollable.
       Quelques pilotes ont des options spécifiques qui peuvent être définies
       au moment de la préparation.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-pdo.prepare-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   Si le serveur de base de données prépare avec succès cette requête,
   <span class="methodname"><strong>PDO::prepare()</strong></span> retourne un objet <span class="classname"><a href="class.pdostatement.php" class="classname">PDOStatement</a></span>.
   Si le serveur de base de données ne réussit pas à préparer la requête,
   <span class="methodname"><strong>PDO::prepare()</strong></span> retourne <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> ou émet une exception
   <span class="classname"><a href="class.pdoexception.php" class="classname">PDOException</a></span> (suivant le
   <a href="pdo.error-handling.php" class="link">gestionnaire des erreurs</a>).
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    L&#039;émulation de requêtes préparées ne communique pas avec le serveur de base
    de données. Aussi, la fonction <span class="methodname"><strong>PDO::prepare()</strong></span> ne vérifie
    pas la requête.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 errors" id="refsect1-pdo.prepare-errors">
  <h3 class="title">Erreurs / Exceptions</h3>
  <p class="para">
Émet une erreur de niveau <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> si l&#039;attribut <strong><code><a href="pdo.constants.php#pdo.constants.attr-errmode">PDO::ATTR_ERRMODE</a></code></strong> est défini
à <strong><code><a href="pdo.constants.php#pdo.constants.errmode-warning">PDO::ERRMODE_WARNING</a></code></strong>.
</p>
<p class="para">
Lève une exception <span class="classname"><a href="class.pdoexception.php" class="classname">PDOException</a></span> si l&#039;attribut <strong><code><a href="pdo.constants.php#pdo.constants.attr-errmode">PDO::ATTR_ERRMODE</a></code></strong> est défini
à <strong><code><a href="pdo.constants.php#pdo.constants.errmode-exception">PDO::ERRMODE_EXCEPTION</a></code></strong>.
</p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdo.prepare-examples">
  <h3 class="title">Exemples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Exemple #1 Modèle de déclaration SQL avec des paramètres nommés</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/* Exécute une requête préparée en passant un tableau de valeurs */<br /></span><span style="color: #0000BB">$sql </span><span style="color: #007700">= </span><span style="color: #DD0000">'SELECT nom, couleur, calories<br />    FROM fruit<br />WHERE calories &lt; :calories AND couleur = :couleur'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$sth </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">, [</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">ATTR_CURSOR </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">CURSOR_FWDONLY</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">([</span><span style="color: #DD0000">'calories' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">150</span><span style="color: #007700">, </span><span style="color: #DD0000">'couleur' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'red'</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$red </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #FF8000">/* Les clés du tableau peuvent être préfixées par des deux-points ":" également (facultatif) */<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">([</span><span style="color: #DD0000">':calories' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">175</span><span style="color: #007700">, </span><span style="color: #DD0000">':couleur' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'yellow'</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$yellow </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <div class="example" id="example-2">
    <p><strong>Exemple #2 Modèle de déclaration SQL avec des marqueurs</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/* Exécute une requête préparée en passant un tableau de valeurs */<br /></span><span style="color: #0000BB">$sth </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT nom, couleur, calories<br />    FROM fruit<br />    WHERE calories &lt; ? AND couleur = ?'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">([</span><span style="color: #0000BB">150</span><span style="color: #007700">, </span><span style="color: #DD0000">'rouge'</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$red </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">([</span><span style="color: #0000BB">175</span><span style="color: #007700">, </span><span style="color: #DD0000">'jaune'</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$yellow </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <div class="example" id="example-3">
    <p><strong>Exemple #3 Modèle de déclaration SQL avec un point d&#039;interrogation échappé</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/* note: ceci n'est valable que pour les bases de données PostgreSQL */<br /></span><span style="color: #0000BB">$sth </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT * FROM issues WHERE tag::jsonb ?? ?'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">([</span><span style="color: #DD0000">'feature'</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$featureIssues </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">([</span><span style="color: #DD0000">'performance'</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$performanceIssues </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-pdo.prepare-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="methodname"><a href="pdo.exec.php" class="methodname" rel="rdfs-seeAlso">PDO::exec()</a> - Ex&eacute;cute une requ&ecirc;te SQL et retourne le nombre de lignes affect&eacute;es</span></li>
    <li><span class="methodname"><a href="pdo.query.php" class="methodname" rel="rdfs-seeAlso">PDO::query()</a> - Pr&eacute;pare et Ex&eacute;cute une requ&ecirc;te SQL sans marque substitutive</span></li>
    <li><span class="methodname"><a href="pdostatement.execute.php" class="methodname" rel="rdfs-seeAlso">PDOStatement::execute()</a> - Ex&eacute;cute une requ&ecirc;te pr&eacute;par&eacute;e</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>