Intro to PHP + Oracle

Thursday Aug 14th 2008 by Sean Hull

Sean Hull points you on the right track to get started with PHP and Oracle.

If you're already familiar with PHP for database backed web-applications, you're probably also familiar with LAMP, O'Reilly's acronym for the hugely popular Linux/Apache/MySQL/PHP stack of software. For you then to jump to Oracle will be a small step, as you'll simply have to learn some new calls that make up the oci8 library. If you're new to PHP as well, there's a bit more to learn, but we will point you on the right track to get you started with the basics, and links to resources that'll help you go further.

What is PHP?

PHP has one of those recursive names, PHP Hypertext Preprocessor. It was originally designed around web programming, but has since evolved to be a full-fledged scripting language like Perl. So for instance by adding #!/usr/bin/php to the top of a file, you can invoke the php processor for programs that are never called from a web server. Far and away the strength of PHP is in its community, and how it has evolved. Rather than being designed at the outset to be everyone's ideal language and framework, PHP has evolved a huge base of developers, and libraries of functions that people needed in the real world.

PHP is used everywhere on the internet, from high traffic sites like facebook.com to popular blog publishing software like wordpress. It is also rock solid, currently at release 5.2.6.

PHP executes with embedded code snippets in your HTML. So for instance the old famous "hello world" example could be done as follows:

  echo "<B>Hello World</B>";

Of course, inside that code section you can be doing all sorts of other things like connecting to a database, sifting through data, formatting it, and sending it back to the user as a nicely formatted HTML page in their browser.

Connecting with Oracle

In the MySQL space, the technology stack is called LAMP. In the Oracle space it's called OPAL, Oracle, PHP, Apache & Linux. You can talk to Oracle a few different ways; there are some database abstraction libraries like PDO or ADOdb. These allow you to build database independent code.

However, if you want the full power and performance of Oracle, you should use the OCI8 libraries.

# note to the parser that we have php code coming next
# Here's how we connect:
$dbh = oci_connect ('sean', 'hull', 'seansdatabase');
# Here's how we pass an sql statement:
$stmt = oci_parse ($dbh, 'select fname, lname from accounts');
# Here we execute the statement:
oci_execute ($stmt);
$cnt = 1;
# Then we fetch rows in a loop until we're done
while ($result = oci_fetch_array($stmt)) {
   echo "user: " . $cnt . " " . $result['fname'] . " " . $result['lname'] . "<br>";
   $cnt = $cnt +1;
# last we close the database handle
# and note to the parser that this is the end of the php code section

Object Oriented & Other Features

PHP also now supports a whole host of object oriented features, if that suits your fancy. You'll find abstract classes for prototyping, use of the final classes to prevent further subclassing. PHP also supports overloading, thus multiple functions with the same name but different parameters, allow the PHP interpreter to choose which function to call at runtime. You'll also find private and protected member variables as well as constructors, and destructors.

PHP also has great exception handling with the TRY, CATCH & THROW syntax. An example of how you might use this might be if you were going to perform a division. You could put a TRY before it, then a CATCH section displaying an error if you divide by zero.

Oracle's Moves

Justin Kestelyn & Chris Jones over at Oracle Technology Network have been building their PHP Developer Center for some time. There is a huge amount of support coming from Oracle now. Just a quick look at the PHP page at OTN and you'll see what I mean.

You'll find everything from installation guides, to more advanced topics like scalability and high availability. Even some podcasts as well. This is a really great site for PHP + Oracle information.

Chris Jones has also published "The Underground PHP and Oracle Manual" so if there are any questions in your mind, you're sure to find the answers here:

Chris' manual will give you everything from nuts and bolts details about drivers to code examples, and plenty of details on getting you started. There are details on installing Oracle XE, installing Apache, and installing PHP with various drivers. You'll find details for both Windows and Linux, and details on the instant client installs as well. All in all, this manual is a treasure trove of information for those building PHP applications on top of Oracle.


PHP is a powerful language for building sophisticated web applications as well as stand-alone scripts, which you can call from the command line. With a full compliment of database support, specifically for Oracle, you are on very solid ground building your web application with OPAL. What's more, you'll find tremendous support from Oracle Corporation besides.

PHP continues to grow in popularity and usage. If the number of user conferences is any indication, that trend shows no signs of slowing.

» See All Articles by Columnist Sean Hull

Mobile Site | Full Site