Skip to content

Commit 638b946

Browse files
authored
Add a new PHP example (#54)
1 parent 5b84347 commit 638b946

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

exadata-express/Example.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
/*----------------------------------------------------------------------
4+
Example.php
5+
6+
Demonstrate how to perform a database insert and query with PHP
7+
in Oracle Database Cloud services such as Exadata Express,
8+
Autonomous Transaction Processing, Autonomous Data Warehouse, and
9+
others.
10+
11+
Before running this script:
12+
- Install PHP and the OCI8 extension
13+
- Install Oracle Instant Client
14+
- Download and install the cloud service wallet
15+
- Modify the oci_connect() call below to use the credentials for your database.
16+
See your cloud service's documentation for details.
17+
18+
----------------------------------------------------------------------*/
19+
20+
// Uncomment for testing
21+
// error_reporting(E_ALL); // In PHP 5.3 use E_ALL|E_STRICT
22+
// ini_set('display_errors', 'On');
23+
24+
// Connect
25+
26+
$c = oci_connect("username", "password", "connect_string");
27+
if (!$c) {
28+
$m = oci_error();
29+
trigger_error("Could not connect to database: ". $m["message"], E_USER_ERROR);
30+
}
31+
32+
// Create a table
33+
34+
$stmtarray = array(
35+
"BEGIN
36+
EXECUTE IMMEDIATE 'DROP TABLE mycloudtab';
37+
EXCEPTION
38+
WHEN OTHERS THEN
39+
IF SQLCODE NOT IN (-00942) THEN
40+
RAISE;
41+
END IF;
42+
END;",
43+
44+
"CREATE TABLE mycloudtab (id NUMBER, data VARCHAR2(20))"
45+
);
46+
47+
foreach ($stmtarray as $stmt) {
48+
$s = oci_parse($c, $stmt);
49+
if (!$s) {
50+
$m = oci_error($c);
51+
trigger_error("Could not parse statement: ". $m["message"], E_USER_ERROR);
52+
}
53+
$r = oci_execute($s);
54+
if (!$r) {
55+
$m = oci_error($s);
56+
trigger_error("Could not execute statement: ". $m["message"], E_USER_ERROR);
57+
}
58+
}
59+
60+
// Insert some data
61+
62+
$data = [ [101, "Alpha" ], [102, "Beta" ], [103, "Gamma" ] ];
63+
64+
$s = oci_parse($c, "INSERT INTO mycloudtab VALUES (:1, :2)");
65+
if (!$s) {
66+
$m = oci_error($c);
67+
trigger_error("Could not parse statement: ". $m["message"], E_USER_ERROR);
68+
}
69+
70+
foreach ($data as $record) {
71+
oci_bind_by_name($s, ":1", $record[0]);
72+
oci_bind_by_name($s, ":2", $record[1]);
73+
oci_execute($s, OCI_NO_AUTO_COMMIT); // for PHP <= 5.3.1 use OCI_DEFAULT instead
74+
if (!$r) {
75+
$m = oci_error($s);
76+
trigger_error("Could not execute statement: ". $m["message"], E_USER_ERROR);
77+
}
78+
}
79+
oci_commit($c);
80+
81+
// Query the data
82+
83+
$s = oci_parse($c, "SELECT * FROM mycloudtab");
84+
if (!$s) {
85+
$m = oci_error($c);
86+
trigger_error("Could not parse statement: ". $m["message"], E_USER_ERROR);
87+
}
88+
$r = oci_execute($s);
89+
if (!$r) {
90+
$m = oci_error($s);
91+
trigger_error("Could not execute statement: ". $m["message"], E_USER_ERROR);
92+
}
93+
94+
echo "<table border='1'>\n";
95+
96+
// Print column headings
97+
98+
$ncols = oci_num_fields($s);
99+
echo "<tr>\n";
100+
for ($i = 1; $i <= $ncols; ++$i) {
101+
$colname = oci_field_name($s, $i);
102+
echo "<th><b>".htmlspecialchars($colname,ENT_QUOTES|ENT_SUBSTITUTE)."</b></th>\n";
103+
}
104+
echo "</tr>\n";
105+
106+
// Print data
107+
108+
while (($row = oci_fetch_array($s, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
109+
echo "<tr>\n";
110+
foreach ($row as $item) {
111+
echo "<td>";
112+
echo $item !== null ? htmlspecialchars($item, ENT_QUOTES|ENT_SUBSTITUTE) : "&nbsp;";
113+
echo "</td>\n";
114+
}
115+
echo "</tr>\n";
116+
}
117+
echo "</table>\n";
118+
119+
?>

0 commit comments

Comments
 (0)