<?php
$c1 = oci_connect("hr", "welcome", 'localhost/XE');
$c2 = oci_connect("hr", "welcome", 'localhost/XE');
/ À la fois $c1 et $c2 affichent le même identifiant de ressources PHP, ce qui signifie
/ qu'il s'agit de la même connexion à la base de données
echo "c1 is $c1<br>\n";
echo "c2 is $c2<br>\n";
function create_table($conn)
{
$stmt = oci_parse($conn, "create table hallo (test varchar2(64))");
oci_execute($stmt);
echo "Created table<br>\n";
}
function drop_table($conn)
{
$stmt = oci_parse($conn, "drop table hallo");
oci_execute($stmt);
echo "Dropped table<br>\n";
}
function insert_data($connname, $conn)
{
$stmt = oci_parse($conn, "insert into hallo
values(to_char(sysdate,'DD-MON-YY HH24:MI:SS'))");
oci_execute($stmt, OCI_DEFAULT);
echo "$connname inserted row without committing<br>\n";
}
function rollback($connname, $conn)
{
oci_rollback($conn);
echo "$connname rollback<br>\n";
}
function select_data($connname, $conn)
{
$stmt = oci_parse($conn, "select * from hallo");
oci_execute($stmt, OCI_DEFAULT);
echo "$connname ----selecting<br>\n";
while (oci_fetch($stmt)) {
echo " " . oci_result($stmt, "TEST") . "<br>\n";
}
echo "$connname ----done<br>\n";
}
create_table($c1);
insert_data('c1', $c1); / Insère une ligne en utilisant c1
sleep(2); / On attend afin de voir un timestamp différent pour la seconde ligne
insert_data('c2', $c2); / Insère une ligne en utilisant c2
select_data('c1', $c1); / Les résultats des 2 insertions sont retournés
select_data('c2', $c2); / Les résultats des 2 insertions sont retournés
rollback('c1', $c1); / Annulation de la transaction en utilisant c1
select_data('c1', $c1); / Les 2 insertions ont été annulées
select_data('c2', $c2);
drop_table($c1);
/ La fermeture d'une connexion rend les variables PHP indisponibles, mais les autres
/ peuvent être toujours utilisées
oci_close($c1);
echo "c1 is $c1<br>\n";
echo "c2 is $c2<br>\n";
/ Affichage :
/ c1 is Resource id #5
/ c2 is Resource id #5
/ Created table
/ c1 inserted row without committing
/ c2 inserted row without committing
/ c1 ----selecting
/ 09-DEC-09 12:14:43
/ 09-DEC-09 12:14:45
/ c1 ----done
/ c2 ----selecting
/ 09-DEC-09 12:14:43
/ 09-DEC-09 12:14:45
/ c2 ----done
/ c1 rollback
/ c1 ----selecting
/ c1 ----done
/ c2 ----selecting
/ c2 ----done
/ Dropped table
/ c1 is
/ c2 is Resource id #5
?>