2007-12-23 21:31:43 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 08:26:23 +00:00
|
|
|
<!-- $Revision$ -->
|
2007-12-23 21:31:43 +00:00
|
|
|
<chapter xml:id="oci8.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
|
|
&reftitle.examples;
|
|
|
|
<para>
|
2009-11-11 18:10:51 +00:00
|
|
|
Basic queries in oci8 use a prepare-execute-fetch sequence of steps.
|
2007-12-23 21:31:43 +00:00
|
|
|
<example>
|
|
|
|
<title>Basic query</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
|
2009-11-11 18:10:51 +00:00
|
|
|
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
|
|
|
|
if (!$conn) {
|
2007-12-23 21:31:43 +00:00
|
|
|
$e = oci_error();
|
2009-11-11 18:10:51 +00:00
|
|
|
trigger_error(htmlentities($e['message']), E_USER_ERROR);
|
|
|
|
}
|
2007-12-23 21:31:43 +00:00
|
|
|
|
2009-11-11 18:10:51 +00:00
|
|
|
// Prepare the statement
|
|
|
|
$stid = oci_parse($conn, 'SELECT * FROM departments');
|
|
|
|
if (!$stid) {
|
2007-12-23 21:31:43 +00:00
|
|
|
$e = oci_error($conn);
|
2009-11-11 18:10:51 +00:00
|
|
|
trigger_error(htmlentities($e['message']), E_USER_ERROR);
|
|
|
|
}
|
2007-12-23 21:31:43 +00:00
|
|
|
|
2009-11-11 18:10:51 +00:00
|
|
|
// Perform the logic of the query
|
|
|
|
$r = oci_execute($stid, OCI_DEFAULT);
|
|
|
|
if (!$r) {
|
2007-12-23 21:31:43 +00:00
|
|
|
$e = oci_error($stid);
|
2009-11-11 18:10:51 +00:00
|
|
|
trigger_error(htmlentities($e['message']), E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the results of the query
|
|
|
|
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) : " ")."</td>\n";
|
|
|
|
}
|
|
|
|
print "</tr>\n";
|
|
|
|
}
|
|
|
|
print "</table>\n";
|
|
|
|
|
|
|
|
oci_free_statement($stid);
|
|
|
|
oci_close($conn);
|
|
|
|
|
2007-12-23 21:31:43 +00:00
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
2009-11-11 18:10:51 +00:00
|
|
|
|
|
|
|
|
2007-12-23 21:31:43 +00:00
|
|
|
<para>
|
2009-11-11 18:10:51 +00:00
|
|
|
Bind variables improve performance by allowing reuse of execution
|
|
|
|
contexts and caches. Bind variables improve security by
|
|
|
|
preventing some kinds of SQL Injection problems.
|
2007-12-23 21:31:43 +00:00
|
|
|
<example>
|
2009-11-11 18:10:51 +00:00
|
|
|
<title>Inserting with bind variables</title>
|
2007-12-23 21:31:43 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
|
2009-11-11 18:10:51 +00:00
|
|
|
// Before running, create the table:
|
|
|
|
// CREATE TABLE MYTABLE (mid NUMBER, myd VARCHAR2(20));
|
2007-12-23 21:31:43 +00:00
|
|
|
|
2009-11-11 18:10:51 +00:00
|
|
|
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
|
|
|
|
if (!$conn) {
|
|
|
|
$e = oci_error();
|
|
|
|
trigger_error(htmlentities($e['message']), E_USER_ERROR);
|
|
|
|
}
|
2007-12-23 21:31:43 +00:00
|
|
|
|
2009-11-11 18:10:51 +00:00
|
|
|
$stid = oci_parse($conn, 'INSERT INTO MYTABLE (mid, myd) VALUES(:myid, :mydata)');
|
2007-12-23 21:31:43 +00:00
|
|
|
|
2009-11-11 18:10:51 +00:00
|
|
|
$id = 60;
|
|
|
|
$data = 'Some data';
|
2007-12-23 21:31:43 +00:00
|
|
|
|
2009-11-11 18:10:51 +00:00
|
|
|
oci_bind_by_name($stid, ':myid', $id);
|
|
|
|
oci_bind_by_name($stid, ':mydata', $data);
|
2007-12-23 21:31:43 +00:00
|
|
|
|
2009-11-11 18:10:51 +00:00
|
|
|
$r = oci_execute($stid); // executes and commits
|
2007-12-23 21:31:43 +00:00
|
|
|
|
2009-11-11 18:10:51 +00:00
|
|
|
if ($r)
|
2007-12-23 21:31:43 +00:00
|
|
|
print "One row inserted";
|
|
|
|
|
2009-11-11 18:10:51 +00:00
|
|
|
oci_free_statement($stid);
|
|
|
|
oci_close($conn);
|
2007-12-23 21:31:43 +00:00
|
|
|
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
|
|
<para>
|
2009-11-11 18:10:51 +00:00
|
|
|
For large data use binary long object (BLOB) or character long object (CLOB) types.
|
2007-12-23 21:31:43 +00:00
|
|
|
<example>
|
|
|
|
<title>Inserting data into a CLOB column</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
|
|
|
|
// Before running, create the table:
|
|
|
|
// CREATE TABLE MYTABLE (mykey NUMBER, myclob CLOB);
|
|
|
|
|
2009-11-11 18:10:51 +00:00
|
|
|
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
|
|
|
|
if (!$conn) {
|
|
|
|
$e = oci_error();
|
|
|
|
trigger_error(htmlentities($e['message']), E_USER_ERROR);
|
|
|
|
}
|
2007-12-23 21:31:43 +00:00
|
|
|
|
|
|
|
$mykey = 12343; // arbitrary key for this example;
|
|
|
|
|
|
|
|
$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_DEFAULT);
|
|
|
|
$clob->save("A very long string");
|
|
|
|
|
|
|
|
oci_commit($conn);
|
|
|
|
|
|
|
|
// Fetching CLOB data
|
|
|
|
|
|
|
|
$query = 'SELECT myclob FROM mytable WHERE mykey = :mykey';
|
|
|
|
|
|
|
|
$stid = oci_parse ($conn, $query);
|
|
|
|
oci_bind_by_name($stid, ":mykey", $mykey, 5);
|
|
|
|
oci_execute($stid, OCI_DEFAULT);
|
|
|
|
|
|
|
|
print '<table border="1">';
|
|
|
|
while ($row = oci_fetch_array($stid, OCI_ASSOC)) {
|
|
|
|
$result = $row['MYCLOB']->load();
|
|
|
|
print '<tr><td>'.$result.'</td></tr>';
|
|
|
|
}
|
|
|
|
print '</table>';
|
|
|
|
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
2009-11-11 18:10:51 +00:00
|
|
|
|
|
|
|
|
2007-12-23 21:31:43 +00:00
|
|
|
<para>
|
2009-11-11 18:10:51 +00:00
|
|
|
You can easily access stored PL/SQL functions and procedures.
|
2007-12-23 21:31:43 +00:00
|
|
|
<example>
|
2009-11-11 18:10:51 +00:00
|
|
|
<title>Using a stored function </title>
|
2007-12-23 21:31:43 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
|
2009-11-11 18:10:51 +00:00
|
|
|
/*
|
|
|
|
Before running the PHP program, create a stored function in
|
|
|
|
SQL*Plus or 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']), 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"; // prints 24
|
|
|
|
|
|
|
|
oci_free_statement($stid);
|
|
|
|
oci_close($conn);
|
|
|
|
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title>Using a stored procedure</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
Before running the PHP program, create a stored procedure in
|
|
|
|
SQL*Plus or 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']), 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"; // prints 16
|
2007-12-23 21:31:43 +00:00
|
|
|
|
2009-11-11 18:10:51 +00:00
|
|
|
oci_free_statement($stid);
|
|
|
|
oci_close($conn);
|
2007-12-23 21:31:43 +00:00
|
|
|
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
</chapter>
|
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode: sgml
|
|
|
|
sgml-omittag:t
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
sgml-parent-document:nil
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2007-12-23 21:31:43 +00:00
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:nil
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
|
|
vim: et tw=78 syn=sgml
|
|
|
|
vi: ts=1 sw=1
|
|
|
|
-->
|
|
|
|
|