How do you use Oracle Database XE's features within your PHP applications? This seven-part article series answers that question. It is excerpted from chapter 32 of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).
Now that you have a good understanding of Oracle Database XEs architecture, column datatypes, basic SQL commands, and security methods, its time to leverage these database features within your PHP applications by using the Oracle OCI8 extension. In Chapter 24, we introduced PDO, a database-independent abstraction layer. In contrast, however, the Oracle OCI8 extension provides you with access to most, if not all, Oracle Database XE features with a high level of performance compared to other extensions. If your PHP applications will access Oracle databases exclusively, OCI8 is your best choice.
In this chapter, you will learn how to perform database queries and DML (Data Manipulation Language) functions, such as table INSERT, DELETE, UPDATE, and SELECT, using PHP function calls. In addition, we show you how to extract database metadata using PHP functions and SELECT statements against database data dictionary views.
Prerequisites
The primary prerequisite for using OCI8 is to configure your installation of PHP to use it. As we showed you in Chapter 27 in the section on configuring Oracle and PHP, locate the PHP configuration file php.ini you created in Chapter 2 and locate this line in the file itself:
;extension=php_oci8.dll
Remove the semicolon at the beginning of the line and save the file in its original location. Restart the Apache HTTP server on your workstation, and the OCI8 extensions are available to all of your PHP applications.
Note In PHP 5, the OCI8 extension function names are more standardized; for example, the PHP 4 version of OCILogin() is now oci_connect() in PHP 5. The old names still exist as aliases, but you are highly encouraged to use the new naming conventions in all of your applications.
Using Database Connections
Before you can do anything with the database, you must connect to it first and provide the required authentication information and database name. When you installed the database in Chapter 27, you ran the script in Listing 32-1 to test connectivity with the database.
The call to oci_connect() establishes the connection to the database. In all future database requests, you use the variable $conn to reference the established connection. You also have three different options for specifying the target database in your connection request; well cover these options after reviewing the connection types. Finally, you'll most likely want to know how to close a connection, so we'll tell you how, as well as what happens behind the scenes.