Exemples

Ces exemples utilisent, pour se connecter à la base de données, l'utilisateur HR qui est le schéma "Human Resources" fourni par la base de données Oracle. Ce compte doit avoir été déverrouillé et le mot de passe réinitialisé avant de pouvoir l'utiliser.

Ces exemples se connectent à la base de données XE de votre machine. Modifiez la chaîne de connexion afin de correspondre à votre base de données avant d'exécuter les exemples de cette documentation.

Exemple #1 Requête simple

<?php

$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
	$e = oci_error();
	trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

// Préparation de la requête
$stid = oci_parse($conn, 'SELECT * FROM departments');
if (!$stid) {
	$e = oci_error($conn);
	trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

// Exécution de la logique de la requête
$r = oci_execute($stid);
if (!$r) {
	$e = oci_error($stid);
	trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

// Récupération des résultats de la requête
print "<table border='1'>\n";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
	print "<tr>\n";
	foreach ($row as $item) {
		print "	<td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "") . "</td>\n";
	}
	print "</tr>\n";
}
print "</table>\n";

oci_free_statement($stid);
oci_close($conn);

?>

Exemple #2 Insertion de données en utilisant des variables liées

<?php

// Avant l'exécution, créez la table suivante :
//   CREATE TABLE MYTABLE (mid NUMBER, myd VARCHAR2(20));

$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
	$e = oci_error();
	trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$stid = oci_parse($conn, 'INSERT INTO MYTABLE (mid, myd) VALUES(:myid, :mydata)');

$id = 60;
$data = 'Some data';

oci_bind_by_name($stid, ':myid', $id);
oci_bind_by_name($stid, ':mydata', $data);

$r = oci_execute($stid);  // exécution et validation

if ($r) {
	print "Une ligne insérée";
}

oci_free_statement($stid);
oci_close($conn);

?>

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

<?php

// Avant l'exécution, créez la table suivante :
//	 CREATE TABLE MYTABLE (mykey NUMBER, myclob CLOB);

$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
	$e = oci_error();
	trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$mykey = 12343;  // clé arbitraire pour l'exemple

$sql = "INSERT INTO mytable (mykey, myclob)
		VALUES (:mykey, EMPTY_CLOB())
		RETURNING myclob INTO :myclob";

$stid = oci_parse($conn, $sql);
$clob = oci_new_descriptor($conn, OCI_D_LOB);
oci_bind_by_name($stid, ":mykey", $mykey, 5);
oci_bind_by_name($stid, ":myclob", $clob, -1, OCI_B_CLOB);
oci_execute($stid, OCI_NO_AUTO_COMMIT); // use OCI_DEFAULT for PHP <= 5.3.1
$clob->save("Une chaîne vraiment très longue");

oci_commit($conn);

// Récupération de données CLOB

$query = 'SELECT myclob FROM mytable WHERE mykey = :mykey';

$stid = oci_parse ($conn, $query);
oci_bind_by_name($stid, ":mykey", $mykey, 5);
oci_execute($stid);

print '<table border="1">';
while ($row = oci_fetch_array($stid, OCI_ASSOC)) {
	$result = $row['MYCLOB']->load();
	print '<tr><td>'.$result.'</td></tr>';
}
print '</table>';

?>

Exemple #4 Utilisation d'une procédure stockée PL/SQL

<?php

/*
  Avant d'exécuter le programme PHP, créez une fonction stockée
  en langage SQL*Plus ou SQL Developer :

  CREATE OR REPLACE FUNCTION myfunc(p IN NUMBER) RETURN NUMBER AS
  BEGIN
	  RETURN p * 3;
  END;

*/

$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
	$e = oci_error();
	trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$p = 8;

$stid = oci_parse($conn, 'begin :r := myfunc(:p); end;');
oci_bind_by_name($stid, ':p', $p);
oci_bind_by_name($stid, ':r', $r, 40);

oci_execute($stid);

print "$r\n";   // Affiche 24

oci_free_statement($stid);
oci_close($conn);

?>

Exemple #5 Utilisation d'une procédure stockée PL/SQL

<?php

/*
  Avant d'exécuter le programme PHP, créez une procédure stockée en
  langage SQL*Plus ou SQL Developer :

  CREATE OR REPLACE PROCEDURE myproc(p1 IN NUMBER, p2 OUT NUMBER) AS
  BEGIN
	  p2 := p1 * 2;
  END;

*/

$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
	$e = oci_error();
	trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$p1 = 8;

$stid = oci_parse($conn, 'begin myproc(:p1, :p2); end;');
oci_bind_by_name($stid, ':p1', $p1);
oci_bind_by_name($stid, ':p2', $p2, 40);

oci_execute($stid);

print "$p2\n";   // affiche 16

oci_free_statement($stid);
oci_close($conn);

?>

Exemple #6 Appel d'une fonction PL/SQL qui retourne un REF CURSOR

<?php
/*
  Créez une fonction stockée PL/SQL comme ceci :

  CREATE OR REPLACE FUNCTION myfunc(p1 IN NUMBER) RETURN SYS_REFCURSOR AS
	  rc SYS_REFCURSOR;
  BEGIN
	  OPEN rc FOR SELECT city FROM locations WHERE ROWNUM < p1;
	  RETURN rc;
  END;
*/

$conn = oci_connect('hr', 'welcome', 'localhost/XE');

$stid = oci_parse($conn, 'SELECT myfunc(5) AS mfrc FROM dual');
oci_execute($stid);

echo "<table border='1'>\n";
while (($row = oci_fetch_array($stid, OCI_ASSOC))) {
	echo "<tr>\n";
	$rc = $row['MFRC'];
	oci_execute($rc);  // La valeur de la colonne retournée depuis la requête est une référence de curseur
	while (($rc_row = oci_fetch_array($rc, OCI_ASSOC))) {   
		echo "	<td>" . $rc_row['CITY'] . "</td>\n";
	}
	oci_free_statement($rc);
	echo "</tr>\n";
}
echo "</table>\n";

// Affiche :
//   Beijing
//   Bern
//   Bombay
//   Geneva

oci_free_statement($stid);
oci_close($conn);

?>
LoadingChargement en cours