Oracle access is a six-step process. A connection is opened, and then a query is first prepared with OCIParse( )
and executed with OCIExecute( )
. Then, each row is retrieved with OCIFetch( )
and individual attributes are retrieved from the row with OCIResult( )
. Last, the connection is closed. Our treatment of Oracle functions is brief, and more detail can be found in the PHP manual.
The key functions are:
- resource OCILogon(string
username
, stringpassword
, stringdatabase
) -
Establishes a connection to an Oracle DBMS. On success, the function returns a connection handle that can access databases through subsequent commands. Parameters are the same as those for
mysql_connect( )
. - resource OCIParse(resource
connection
, stringSQL_command
) -
Returns a query resource handle that can subsequently be executed, or returns
false
on error. Theconnection
resource created withOCILogon( )
is passed as a parameter, along with anSQL_command
. The function doesn't execute the query-OCIExecute( )
does that-but this function is required to set up the query for execution. - int OCIExecute(resource
query_handle
) -
Runs the query set up with
OCIParse( )
, taking the return value ofOCIParse( )
as the only parameter. Results are subsequently fetched withOCIFetch( )
. Returnstrue
on success andfalse
on failure. - int OCIFetch(resource
query_handle
) -
Buffers a row from the last
OCIExecute( )
call specified with thequery_handle
returned fromOCIParse( )
. Returnstrue
if a row is retrieved andfalse
when no more rows are available. Attributes are fetched from this buffer withOCIResult( )
. - int OCINumCols(resource
query_handle
) -
Returns the number of attributes associated with the query specified in
OCIParse( )
. - mixed OCIResult(resource
query_handle
, intattribute_number
) -
Fetches the value of
attribute_number
from the current row retrieved withOCIFetch( )
. Takes the return result ofOCIParse( )
as the first parameter. - int OCILogoff(resource
connection
) -
Closes an Oracle connection opened with
OCILogon( )
.
Example 4-14. Connecting to an Oracle data source with PHP
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Wines</title> </head> <body><pre> <?php // (1) Open the database connections $connection = OCILogon("fred","shhh", "winestore"); // (2) Setup the query on the winestore through the // connection $query = OCIParse($connection, "SELECT * FROM wine"); // (3) Run the query OCIExecute($query); // (4) Output the results while (OCIFetch($query)) { // (5) Print out the attributes in this row for($x=1;$x<=OCINumCols($query);$x++) echo OCIResult($query,$x); echo "\n"; } // (6) Close the database connection OCILogoff($connection); ?> </pre> </body> </html>