Manuel PHP

Exemple #1 Requête basique

  1. <?php
  2.  
  3. $conn = oci_connect('hr', 'hr', 'orcl');  
  4. if (!$conn) { 
  5.    $e = oci_error(); 
  6.    print htmlentities($e['message']); 
  7.    exit;  
  8. }  
  9.  
  10. $query = 'SELECT * FROM DEPARTMENTS';  
  11.  
  12. $stid = oci_parse($conn, $query);  
  13. if (!$stid) { 
  14.    $e = oci_error($conn); 
  15.    print htmlentities($e['message']); 
  16.    exit;  
  17. }  
  18.  
  19. $r = oci_execute($stid, OCI_DEFAULT);  
  20. if (!$r) { 
  21.    $e = oci_error($stid); 
  22.    echo htmlentities($e['message']); 
  23.    exit;  
  24. }  
  25.  
  26. print '<table border="1">';  
  27. while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS)) { 
  28.    print '<tr>'; 
  29.    foreach ($row as $item) { 
  30.       print '<td>'.($item?htmlentities($item):' ').'</td>'; 
  31.    } 
  32.    print '</tr>';  
  33. }  
  34. print '</table>';  
  35.  
  36. oci_close($conn);  
  37. ?> 

Exemple #2 Requête Insert avec des variables liées

  1. <?php
  2.  
  3. // Avant d'exécuter, création de la table
  4. // CREATE TABLE MYTABLE (mid NUMBER, myd VARCHAR2(20));
  5.  
  6. $conn = oci_connect('scott', 'tiger', 'orcl');  
  7.  
  8. $query = 'INSERT INTO MYTABLE VALUES(:myid, :mydata)';  
  9.  
  10. $stid = oci_parse($conn, $query);  
  11.  
  12. $id = 60;  
  13. $data = 'Some data';  
  14.  
  15. oci_bind_by_name($stid, ':myid', $id);  
  16. oci_bind_by_name($stid, ':mydata', $data);  
  17.  
  18. $r = oci_execute($stid);  
  19.  
  20. if ($r) 
  21.    print "Une ligne a été insérée";  
  22.  
  23. oci_close($conn);  
  24.  
  25. ?> 

Exemple #3 Insertion de donnée dans une colonne CLOB

  1. <?php
  2.  
  3. // Avant l'exécution, création de la table
  4. //    CREATE TABLE MYTABLE (mykey NUMBER, myclob CLOB);
  5.  
  6. $conn = oci_connect('scott', 'tiger', 'orcl');  
  7.  
  8. $mykey = 12343; // clé arbitraire pour cet exemple;
  9.  
  10. $sql = "INSERT INTO mytable (mykey, myclob)
  11.       VALUES (:mykey, EMPTY_CLOB())
  12.       RETURNING myclob INTO :myclob";  
  13.  
  14. $stid = oci_parse($conn, $sql);  
  15. $clob = oci_new_descriptor($conn, OCI_D_LOB);  
  16. oci_bind_by_name($stid, ":mykey", $mykey, 5);  
  17. oci_bind_by_name($stid, ":myclob", $clob, -1, OCI_B_CLOB);  
  18. oci_execute($stid, OCI_DEFAULT);  
  19. $clob->save("A very long string");  
  20.  
  21. oci_commit($conn);  
  22.  
  23. // Récupération des données CLOB
  24.  
  25. $query = 'SELECT myclob FROM mytable WHERE mykey = :mykey';  
  26.  
  27. $stid = oci_parse ($conn, $query);  
  28. oci_bind_by_name($stid, ":mykey", $mykey, 5);  
  29. oci_execute($stid, OCI_DEFAULT);  
  30.  
  31. print '<table border="1">';  
  32. while ($row = oci_fetch_array($stid, OCI_ASSOC)) {  
  33. $result = $row['MYCLOB']->load();  
  34. print '<tr><td>'.$result.'</td></tr>';  
  35. }  
  36. print '</table>';  
  37.  
  38. ?> 

Vous pouvez facilement accéder aux procédures stockées, de la même façon que vous le feriez par ligne de commande :

Exemple #4 Utilisation de procédures stockées

  1. <?php
  2. // par webmaster at remoterealty dot com
  3. $sth = oci_parse($dbh, "begin sp_newaddress( :address_id, '$firstname',
  4. '$lastname', '$company', '$address1', '$address2', '$city', '$state',
  5. '$postalcode', '$country', :error_code );end;");  
  6.  
  7. // Cela appelle la procédure stockée sp_newaddress, avec la variable :address_id
  8. // pour les entrées/sorties et :error_code comme variable de sortie.
  9. // Ensuite, vous faites les liaisons suivantes :
  10.  
  11. oci_bind_by_name($sth, ":address_id", $addr_id, 10);  
  12. oci_bind_by_name($sth, ":error_code", $errorcode, 10);  
  13. oci_execute($sth);  
  14.  
  15. ?> 


Remonter Remonter
L'éditeur javascript - CSS - Gentoo - Tutoriaux PHP - Tutoriels PHP - Bretagne - php - Moto - Kit graphique