<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/book.pdo.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'pdo.prepared-statements.php',
    1 => 'Requ&ecirc;tes pr&eacute;par&eacute;es et proc&eacute;dures stock&eacute;es',
    2 => 'Requ&ecirc;tes pr&eacute;par&eacute;es et proc&eacute;dures stock&eacute;es',
  ),
  'up' => 
  array (
    0 => 'book.pdo.php',
    1 => 'PDO',
  ),
  'prev' => 
  array (
    0 => 'pdo.transactions.php',
    1 => 'Transactions et validation automatique (autocommit)',
  ),
  'next' => 
  array (
    0 => 'pdo.error-handling.php',
    1 => 'Les erreurs et leur gestion',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'reference/pdo/prepared-statements.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="pdo.prepared-statements" class="chapter">
 <h1 class="title">Requêtes préparées et procédures stockées</h1>

 <p class="para">
  La plupart des bases de données supportent le concept des requêtes préparées.
  Qu&#039;est-ce donc ? Il est possible de les voir comme une sorte de modèle compilé
  pour le SQL que l&#039;on veut exécuter, qui peut être personnalisé en utilisant
  des variables en guise de paramètres. Les requêtes préparées offrent
  deux fonctionnalités essentielles :
 </p>
 <ul class="itemizedlist">
  <li class="listitem">
   <span class="simpara">
    La requête ne doit être analysée (ou préparée) qu&#039;une seule fois, mais peut
    être exécutée plusieurs fois avec des paramètres identiques ou différents.
    Lorsque la requête est préparée, la base de données va analyser, compiler
    et optimiser son plan pour exécuter la requête. Pour les requêtes complexes,
    ce processus peut prendre assez de temps, ce qui peut ralentir les applications
    si l&#039;on doit répéter la même requête plusieurs fois avec différents paramètres.
    En utilisant les requêtes préparées, l&#039;on évite ainsi de répéter le cycle
    analyse/compilation/optimisation. Pour résumer, les requêtes préparées
    utilisent moins de ressources et s&#039;exécutent plus rapidement.
   </span>
  </li>
  <li class="listitem">
   <span class="simpara">
    Les paramètres pour préparer les requêtes n&#039;ont pas besoin d&#039;être entre
    guillemets ; le pilote gère cela. Si l&#039;application utilise exclusivement
    les requêtes préparées, il est possible d&#039;être sûr qu&#039;aucune injection SQL
    n&#039;est possible (Cependant, si l&#039;on construit d&#039;autres parties de la requête
    en se basant sur des entrées utilisateurs, l&#039;on continue à prendre un risque).
   </span>
  </li>
 </ul>
 <p class="para">
  Les requêtes préparées sont tellement pratiques que c&#039;est l&#039;unique fonctionnalité
  que PDO émule pour les pilotes qui ne les prennent pas en charge. Ceci assure de pouvoir
  utiliser la même technique pour accéder aux données, sans se soucier des capacités
  de la base de données.
 </p>
 <p class="para">
  <div class="example" id="example-1">
   <p><strong>Exemple #1 Insertions répétitives en utilisant les requêtes préparées</strong></p>
   <div class="example-contents"><p>
    Cet exemple effectue une requête INSERT en y substituant un
    <code class="literal">nom</code> et une <code class="literal">valeur</code> pour les marqueurs nommés.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$stmt </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">"INSERT INTO REGISTRY (name, value) VALUES (:name, :value)"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bindParam</span><span style="color: #007700">(</span><span style="color: #DD0000">':name'</span><span style="color: #007700">, </span><span style="color: #0000BB">$name</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bindParam</span><span style="color: #007700">(</span><span style="color: #DD0000">':value'</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// insertion d'une ligne<br /></span><span style="color: #0000BB">$name </span><span style="color: #007700">= </span><span style="color: #DD0000">'one'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$value </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// insertion d'une autre ligne avec des valeurs différentes<br /></span><span style="color: #0000BB">$name </span><span style="color: #007700">= </span><span style="color: #DD0000">'two'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$value </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="para">
  <div class="example" id="example-2">
   <p><strong>Exemple #2 Insertions répétées en utilisant des requêtes préparées</strong></p>
   <div class="example-contents"><p>
    Cet exemple effectue une requête INSERT en y substituant un <code class="literal">nom</code>
    et une <code class="literal">valeur</code> pour les marqueurs <code class="literal">?</code>.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$stmt </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">"INSERT INTO REGISTRY (name, value) VALUES (?, ?)"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bindParam</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">$name</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bindParam</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// insertion d'une ligne<br /></span><span style="color: #0000BB">$name </span><span style="color: #007700">= </span><span style="color: #DD0000">'one'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$value </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// insertion d'une autre ligne avec différentes valeurs<br /></span><span style="color: #0000BB">$name </span><span style="color: #007700">= </span><span style="color: #DD0000">'two'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$value </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="para">
  <div class="example" id="example-3">
   <p><strong>Exemple #3 Récupération des données en utilisant des requêtes préparées</strong></p>
   <div class="example-contents"><p>
    Cet exemple récupère des données basées sur la valeur d&#039;une clé fournie
    par un formulaire. L&#039;entrée utilisateur est automatiquement échappée, il n&#039;y a
    donc aucun risque d&#039;attaque par injection SQL.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$stmt </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 REGISTRY where name = ?"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">([</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">]]);<br />foreach (</span><span style="color: #0000BB">$stmt </span><span style="color: #007700">as </span><span style="color: #0000BB">$row</span><span style="color: #007700">) {<br />  </span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$row</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 
 <p class="para">
  <div class="example" id="example-4">
   <p><strong>Exemple #4 Appel d&#039;une procédure stockée avec un paramètre de sortie</strong></p>
   <div class="example-contents"><p>
    Si le pilote de la base de données le prend en charge, il est également possible de lier
    des paramètres aussi bien pour l&#039;entrée que pour la sortie. Les paramètres de sortie
    sont utilisés typiquement pour récupérer les valeurs d&#039;une procédure stockée.
    Les paramètres de sortie sont un peu plus complexes à utiliser que les paramètres d&#039;entrée
    car on doit savoir la longueur qu&#039;un paramètre donné pourra atteindre lorsqu&#039;on le
    lie. Si la valeur retournée est plus longue que la taille que l&#039;on aura suggérée,
    une erreur sera émise.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$stmt </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">"CALL sp_returns_string(?)"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bindParam</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">$return_value</span><span style="color: #007700">, </span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">PARAM_STR</span><span style="color: #007700">, </span><span style="color: #0000BB">4000</span><span style="color: #007700">); <br /><br /></span><span style="color: #FF8000">// Appel de la procédure stockée<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br />print </span><span style="color: #DD0000">"La procédure a retourné : </span><span style="color: #0000BB">$return_value</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 
 <p class="para">
  <div class="example" id="example-5">
   <p><strong>Exemple #5 Appel d&#039;une procédure stockée avec un paramètre d&#039;entrée/sortie</strong></p>
   <div class="example-contents"><p>
    Il faut également spécifier les paramètres qui gèrent les valeurs
    aussi bien pour l&#039;entrée que pour la sortie ; la syntaxe est similaire aux
    paramètres de sortie. Dans le prochain exemple, la chaîne &#039;Bonjour&#039; est passée
    à la procédure stockée et lorsqu&#039;elle retourne la valeur, &#039;Bonjour&#039; est remplacée
    par la valeur retournée par la procédure.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$stmt </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">"CALL sp_takes_string_returns_string(?)"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$value </span><span style="color: #007700">= </span><span style="color: #DD0000">'hello'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bindParam</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">, </span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">PARAM_STR</span><span style="color: #007700">|</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">PARAM_INPUT_OUTPUT</span><span style="color: #007700">, </span><span style="color: #0000BB">4000</span><span style="color: #007700">); <br /><br /></span><span style="color: #FF8000">// appel de la procédure stockée<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br />print </span><span style="color: #DD0000">"La procédure a retourné : </span><span style="color: #0000BB">$value</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="para">
  <div class="example" id="example-6">
   <p><strong>Exemple #6 Utilisation invalide de marqueur</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$stmt </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 REGISTRY where name LIKE '%?%'"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">([</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">]]);<br /><br /></span><span style="color: #FF8000">// un marqueur doit être utilisé à la place d'une valeur complète<br /></span><span style="color: #0000BB">$stmt </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 REGISTRY where name LIKE ?"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">([</span><span style="color: #DD0000">"%</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #0000BB">name</span><span style="color: #007700">]</span><span style="color: #DD0000">%"</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

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