<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.oci8.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'function.oci-execute.php',
    1 => 'oci_execute',
    2 => 'Ex&eacute;cute une commande SQL Oracle',
  ),
  'up' => 
  array (
    0 => 'ref.oci8.php',
    1 => 'Fonctions OCI8',
  ),
  'prev' => 
  array (
    0 => 'function.oci-error.php',
    1 => 'oci_error',
  ),
  'next' => 
  array (
    0 => 'function.oci-fetch.php',
    1 => 'oci_fetch',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'reference/oci8/functions/oci-execute.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.oci-execute" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">oci_execute</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8, PECL OCI8 &gt;= 1.1.0)</p><p class="refpurpose"><span class="refname">oci_execute</span> &mdash; <span class="dc-title">Exécute une commande SQL Oracle</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.oci-execute-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>oci_execute</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> <code class="parameter">$statement</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$mode</code><span class="initializer"> = <strong><code><a href="oci8.constants.php#constant.oci-commit-on-success">OCI_COMMIT_ON_SUCCESS</a></code></strong></span></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   Exécute une requête <code class="parameter">statement</code> précédemment
   retournée par la fonction <span class="function"><a href="function.oci-parse.php" class="function">oci_parse()</a></span>.
  </p>
  <p class="para">
   Après exécution, les requêtes comme <code class="literal">INSERT</code>
   auront par défaut les données validées (commitées) sur la base de données.
   Pour les requêtes comme <code class="literal">SELECT</code>, l&#039;exécution
   effectue la logique de la requête. Les résultats de la requête
   peuvent être récupérés par PHP avec des fonctions comme
   <span class="function"><a href="function.oci-fetch-array.php" class="function">oci_fetch_array()</a></span>.
  </p>
  <p class="para">
   Chaque requête analysée peut être exécutée plusieurs fois, inutile
   donc de les analyser de nouveau. Ceci est pratique pour des requêtes
   de type <code class="literal">INSERT</code> lorsque des données y sont liées
   grâce à la fonction <span class="function"><a href="function.oci-bind-by-name.php" class="function">oci_bind_by_name()</a></span>.
  </p>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.oci-execute-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">statement</code></dt>
     <dd>
      <p class="para">
       Un identifiant de requête OCI valide.
      </p>
     </dd>
    
    
     <dt><code class="parameter">mode</code></dt>
     <dd>
      <p class="para">
       Un second paramètre optionnel peut prendre une des constantes suivantes :
       <table class="doctable table">
        <caption><strong>Modes d&#039;exécution</strong></caption>
        
         <thead>
          <tr>
           <th>Constante</th>
           <th>Description</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td><strong><code><a href="oci8.constants.php#constant.oci-commit-on-success">OCI_COMMIT_ON_SUCCESS</a></code></strong></td>
           <td>Validation automatique sur la connexion lorsque la requête
            a été exécutée avec succès. C&#039;est le comportement par défaut.</td>
          </tr>

          <tr>
           <td><strong><code><a href="oci8.constants.php#constant.oci-describe-only">OCI_DESCRIBE_ONLY</a></code></strong></td>
           <td>Rend les métadonnées de la requête disponibles aux fonctions
            comme <span class="function"><a href="function.oci-field-name.php" class="function">oci_field_name()</a></span> mais ne crée pas de jeu
            de résultats. Tout appel à des fonctions de récupération comme
            <span class="function"><a href="function.oci-fetch-array.php" class="function">oci_fetch_array()</a></span> échouera.</td>
          </tr>

          <tr>
           <td><strong><code><a href="oci8.constants.php#constant.oci-no-auto-commit">OCI_NO_AUTO_COMMIT</a></code></strong></td>
           <td>Ne valide pas automatiquement les modifications.</td>
          </tr>

         </tbody>
        
       </table>

      </p>
      <p class="para">
       L&#039;utilisation du mode <strong><code><a href="oci8.constants.php#constant.oci-no-auto-commit">OCI_NO_AUTO_COMMIT</a></code></strong> démarre
       ou continue une transaction. Les transactions sont automatiquement annulées
       lorsque la connexion est fermée ou lorsque le script se termine.
       Appelez explicitement la fonction <span class="function"><a href="function.oci-commit.php" class="function">oci_commit()</a></span> pour
       valider la transaction ou la fonction <span class="function"><a href="function.oci-rollback.php" class="function">oci_rollback()</a></span>
       pour l&#039;annuler.
      </p>
      <p class="para">
       Lors de l&#039;insertion ou de la mise à jour de données, l&#039;utilisation
       de transactions est fortement recommandée pour garantir la consistance
       relationnelle des données, mais aussi en raison d&#039;un gain non négligeable
       de performance.
      </p>
      <p class="para">
       Si le mode <strong><code><a href="oci8.constants.php#constant.oci-no-auto-commit">OCI_NO_AUTO_COMMIT</a></code></strong> est utilisé pour
       toutes opérations y compris les requêtes, et que les fonctions
       <span class="function"><a href="function.oci-commit.php" class="function">oci_commit()</a></span> et <span class="function"><a href="function.oci-rollback.php" class="function">oci_rollback()</a></span>
       ne sont jamais appelées, OCI8 effectuera une annulation à la fin du
       script même si les données ont changé. Pour éviter ce comportement,
       la plupart des scripts n&#039;utilisent pas le mode
       <strong><code><a href="oci8.constants.php#constant.oci-no-auto-commit">OCI_NO_AUTO_COMMIT</a></code></strong> pour les requêtes ou 
       les procédures stockées PL/SQL. Il faut s&#039;assurer de la consistance
       transactionnelle appropriée des applications lors de l&#039;utilisation
       de la fonction <span class="function"><strong>oci_execute()</strong></span> avec différents modes dans
       le même script.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.oci-execute-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   Cette fonction retourne <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> en cas de succès ou <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> si une erreur survient.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.oci-execute-examples">
  <h3 class="title">Exemples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Exemple #1 Exemple avec <span class="function"><strong>oci_execute()</strong></span> pour des requêtes</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">, </span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">, </span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stid </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">'SELECT * FROM employees'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"&lt;table border='1'&gt;\n"</span><span style="color: #007700">;<br />while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">, </span><span style="color: #0000BB">OCI_ASSOC</span><span style="color: #007700">+</span><span style="color: #0000BB">OCI_RETURN_NULLS</span><span style="color: #007700">)) {<br />    echo </span><span style="color: #DD0000">"&lt;tr&gt;\n"</span><span style="color: #007700">;<br />    foreach (</span><span style="color: #0000BB">$row </span><span style="color: #007700">as </span><span style="color: #0000BB">$item</span><span style="color: #007700">) {<br />        echo </span><span style="color: #DD0000">"    &lt;td&gt;" </span><span style="color: #007700">. (</span><span style="color: #0000BB">$item </span><span style="color: #007700">!== </span><span style="color: #0000BB">null </span><span style="color: #007700">? </span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$item</span><span style="color: #007700">, </span><span style="color: #0000BB">ENT_QUOTES</span><span style="color: #007700">) : </span><span style="color: #DD0000">""</span><span style="color: #007700">) . </span><span style="color: #DD0000">"&lt;/td&gt;\n"</span><span style="color: #007700">;<br />    }<br />    echo </span><span style="color: #DD0000">"&lt;/tr&gt;\n"</span><span style="color: #007700">;<br />}<br />echo </span><span style="color: #DD0000">"&lt;/table&gt;\n"</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-2">
    <p><strong>Exemple #2 Exemple avec <span class="function"><strong>oci_execute()</strong></span> sans spécifier de mode</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">// Avant exécution, créez la table :<br />//   CREATE TABLE MYTABLE (col1 NUMBER);<br /><br /></span><span style="color: #0000BB">$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">, </span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">, </span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stid </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">'INSERT INTO mytab (col1) VALUES (123)'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">); </span><span style="color: #FF8000">// La ligne est validée et est immédiatement visible par les autres utilisateurs<br /><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 Exemple avec <span class="function"><strong>oci_execute()</strong></span> et <strong><code><a href="oci8.constants.php#constant.oci-no-auto-commit">OCI_NO_AUTO_COMMIT</a></code></strong></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">// Avant exécution, créez la table :<br />//   CREATE TABLE MYTABLE (col1 NUMBER);<br /><br /></span><span style="color: #0000BB">$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">, </span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">, </span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stid </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">'INSERT INTO mytab (col1) VALUES (:bv)'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_bind_by_name</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">, </span><span style="color: #DD0000">':bv'</span><span style="color: #007700">, </span><span style="color: #0000BB">$i</span><span style="color: #007700">, </span><span style="color: #0000BB">10</span><span style="color: #007700">);<br />for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">&lt;= </span><span style="color: #0000BB">5</span><span style="color: #007700">; ++</span><span style="color: #0000BB">$i</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">, </span><span style="color: #0000BB">OCI_NO_AUTO_COMMIT</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">oci_commit</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);  </span><span style="color: #FF8000">// valide toutes les nouvelles valeurs : 1, 2, 3, 4, 5<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 Exemple avec <span class="function"><strong>oci_execute()</strong></span> et différents modes dans le même script</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">// Avant exécution, créez la table :<br />//   CREATE TABLE MYTABLE (col1 NUMBER);<br /><br /></span><span style="color: #0000BB">$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">, </span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">, </span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stid </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">'INSERT INTO mytab (col1) VALUES (123)'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">, </span><span style="color: #0000BB">OCI_NO_AUTO_COMMIT</span><span style="color: #007700">);  </span><span style="color: #FF8000">// data not committed<br /><br /></span><span style="color: #0000BB">$stid </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">'INSERT INTO mytab (col1) VALUES (456)'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">);  </span><span style="color: #FF8000">// valide à la fois les valeurs 123 et 456<br /><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 Exemple avec <span class="function"><strong>oci_execute()</strong></span> et
     <strong><code><a href="oci8.constants.php#constant.oci-describe-only">OCI_DESCRIBE_ONLY</a></code></strong></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">, </span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">, </span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$stid </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">'SELECT * FROM locations'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$s</span><span style="color: #007700">, </span><span style="color: #0000BB">OCI_DESCRIBE_ONLY</span><span style="color: #007700">);<br />for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">&lt;= </span><span style="color: #0000BB">oci_num_fields</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">); ++</span><span style="color: #0000BB">$i</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">oci_field_name</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">, </span><span style="color: #0000BB">$i</span><span style="color: #007700">) . </span><span style="color: #DD0000">"&lt;br&gt;\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

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

 
 <div class="refsect1 notes" id="refsect1-function.oci-execute-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Les transactions sont automatiquement annulées lorsque les connexions
    sont fermées, ou lorsque le script se termine, suivant ce qui arrive en premier.
    Appelez explicitement la fonction <span class="function"><a href="function.oci-commit.php" class="function">oci_commit()</a></span> pour valider
    une transaction.
   </p>
   <p class="para">
    Tout appel à la fonction <span class="function"><strong>oci_execute()</strong></span> qui utilise le mode
    <strong><code><a href="oci8.constants.php#constant.oci-commit-on-success">OCI_COMMIT_ON_SUCCESS</a></code></strong> explicitement ou par défaut
    validera toutes les transactions en cours.
   </p>
   <p class="para">
    Toutes les requêtes Oracle DDL comme <code class="literal">CREATE</code>
    ou <code class="literal">DROP</code> valideront toutes les transactions en cours.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    À cause du fait que la fonction <span class="function"><strong>oci_execute()</strong></span> envoie
    généralement la requête à la base de données, la fonction
    <span class="function"><strong>oci_execute()</strong></span> peut identifier les erreurs de syntaxe
    de la requête que la fonction <span class="function"><a href="function.oci-parse.php" class="function">oci_parse()</a></span> aurait pu
    ne pas détecter.
   </p>
  </p></blockquote>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.oci-execute-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.oci-parse.php" class="function" rel="rdfs-seeAlso">oci_parse()</a> - Pr&eacute;pare une requ&ecirc;te SQL avec Oracle</span></li>
   </ul>
  </p>
 </div>

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