THE UNDERGROUND PHP
®
AND ORACLE MANUAL
CHRISTOPHER JONES AND ALISON HOLLOWAY
The Underground PHP and Oracle® Manual, Release 1.5, December 2008.
Copyright © 2008, Oracle. All rights reserved.
Authors: Christopher Jones and Alison Holloway
Contributors and acknowledgments: Vladimir Barriere, Luxi Chidambaran, Robert Clevenger, Antony
Dovgal, Wez Furlong, Sue Harper, Manuel Hoßfeld, Ken Jacobs, Srinath Krishnaswamy, Shoaib Lari, Simon
Law, Krishna Mohan, Chuck Murray, Kevin Neel, Kant Patel, Charles Poulsen, Karthik Rajan, Richard
Rendell, Roy Rossebo, Michael Sekurski, Sreekumar Seshadri, Mohammad Sowdagar, Makoto Tozawa,
Todd Trichler, Simon Watt, Zahi, Shuping Zhou.
The latest edition of this book is available online at:
/>The Programs (which include both the software and documentation) contain proprietary information; they
are provided under a license agreement containing restrictions on use and disclosure and are also
protected by copyright, patent, and other intellectual and industrial property laws. Reverse engineering,
disassembly, or decompilation of the Programs, except to the extent required to obtain interoperability with
other independently created software or as specified by law, is prohibited.
The information contained in this document is subject to change without notice. If you find any
problems in the documentation, please report them to us in writing. This document is not warranted to be
error-free. Except as may be expressly permitted in your license agreement for these Programs, no part of
these Programs may be reproduced or transmitted in any form or by any means, electronic or mechanical,
for any purpose.
U.S. GOVERNMENT RIGHTS
Programs, software, databases, and related documentation and technical data delivered to U.S.
Government customers are "commercial computer software" or "commercial technical data" pursuant to the
applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, use,
duplication, disclosure, modification, and adaptation of the Programs, including documentation and
technical data, shall be subject to the licensing restrictions set forth in the applicable Oracle license
agreement, and, to the extent applicable, the additional rights set forth in FAR 52.227-19, Commercial
Computer Software--Restricted Rights (June 1987). Oracle USA, Inc., 500 Oracle Parkway, Redwood City,
CA 94065.
The Programs are not intended for use in any nuclear, aviation, mass transit, medical, or other
inherently dangerous applications. It shall be the licensee's responsibility to take all appropriate fail-safe,
backup, redundancy and other measures to ensure the safe use of such applications if the Programs are
used for such purposes, and we disclaim liability for any damages caused by such use of the Programs.
The Programs may provide links to Web sites and access to content, products, and services from third
parties. Oracle is not responsible for the availability of, or any content provided on, third-party Web sites.
You bear all risks associated with the use of such content. If you choose to purchase any products or
services from a third party, the relationship is directly between you and the third party. Oracle is not
responsible for: (a) the quality of third-party products or services; or (b) fulfilling any of the terms of the
agreement with the third party, including delivery of products or services and warranty obligations related
to purchased products or services. Oracle is not responsible for any loss or damage of any sort that you
may incur from dealing with any third party.
Oracle, JD Edwards, and PeopleSoft are registered trademarks of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective owners.
CONTENTS
Chapter 1 Introduction..............................................................................................................1
Who Should Read This Book?............................................................................................................1
Introduction to Oracle.........................................................................................................................1
Databases and Instances................................................................................................................2
Tablespaces....................................................................................................................................2
Schemas and Users........................................................................................................................2
Introduction to PHP............................................................................................................................2
Chapter 2 Getting Started With PHP.......................................................................................5
Creating and Editing PHP Scripts.......................................................................................................5
PHP Syntax Overview........................................................................................................................5
Running PHP Scripts..........................................................................................................................8
Running PHP Scripts in a Browser..................................................................................................8
Running Scripts with Command Line PHP......................................................................................8
Debugging PHP Scripts...................................................................................................................8
Chapter 3 PHP Oracle Extensions.........................................................................................11
PHP Oracle Extensions....................................................................................................................11
Oracle Extension...........................................................................................................................11
OCI8 Extension.............................................................................................................................11
PDO Extension..............................................................................................................................12
PHP Database Abstraction Libraries.................................................................................................13
ADOdb..........................................................................................................................................13
PEAR DB......................................................................................................................................14
PEAR MDB2.................................................................................................................................14
Getting the OCI8 Extension..............................................................................................................14
OCI8 and Oracle Installation Options............................................................................................15
Getting the PDO Extension...............................................................................................................16
Zend Core for Oracle........................................................................................................................17
The PHP Release Cycle...................................................................................................................17
Chapter 4 Installing Oracle Database 10g Express Edition................................................19
Oracle Database Editions.................................................................................................................19
Oracle Database XE.........................................................................................................................19
Installing Oracle Database XE on Linux...........................................................................................20
Installing Oracle Database XE on Debian, Ubuntu, and Kubuntu.....................................................21
Installing Oracle Database XE on Windows......................................................................................22
Testing the Oracle Database XE Installation.....................................................................................24
Configuring Oracle Database XE......................................................................................................25
Setting the Oracle Database XE Environment Variables on Linux.................................................25
iii
Enabling Database Startup and Shutdown from Menus on Linux..................................................26
Starting and Stopping the Listener and Database.........................................................................26
Enabling Remote Client Connection..............................................................................................29
Chapter 5 Using Oracle Database.........................................................................................31
Oracle Application Express...............................................................................................................31
Logging In To Oracle Application Express.....................................................................................31
Unlocking the HR User..................................................................................................................32
Creating Database Objects...........................................................................................................33
Working with SQL Scripts..............................................................................................................37
Creating a PL/SQL Procedure.......................................................................................................38
Creating a Database User.............................................................................................................40
Monitoring Database Sessions......................................................................................................42
Database Backup and Recovery...................................................................................................44
Oracle SQL*Plus..............................................................................................................................47
Starting SQL*Plus.........................................................................................................................48
Executing SQL and PL/SQL Statements in SQL*Plus...................................................................49
Controlling Query Output in SQL*Plus...........................................................................................49
Running Scripts in SQL*Plus.........................................................................................................50
Information On Tables in SQL*Plus...............................................................................................50
Accessing the Demonstration Tables in SQL*Plus........................................................................51
Oracle SQL Developer......................................................................................................................51
Creating a Database Connection..................................................................................................51
Creating a Table............................................................................................................................54
Executing a SQL Query.................................................................................................................55
Editing, Compiling and Running PL/SQL.......................................................................................57
Running Reports...........................................................................................................................59
Creating Reports...........................................................................................................................61
Chapter 6 Installing Apache HTTP Server............................................................................63
Installing Apache HTTP Server on Linux..........................................................................................63
Starting and Stopping Apache HTTP Server.................................................................................64
Configuring Apache HTTP Server on Linux...................................................................................64
Installing Apache HTTP Server on Windows....................................................................................64
Starting and Stopping Apache HTTP Server.................................................................................65
Chapter 7 Installing PHP........................................................................................................67
Installing PHP with OCI8 on Linux....................................................................................................67
Installing OCI8 Using a Local Database........................................................................................67
Installing OCI8 Using Oracle Instant Client...................................................................................69
Upgrading PHP with PECL OCI8 on Linux.......................................................................................70
Upgrading OCI8 as a Static Library on Linux ................................................................................70
Upgrading OCI8 on Linux Using the PECL Channel......................................................................71
Upgrading OCI8 as a Shared Library on Linux..............................................................................72
iv
Installing PHP With OCI8 on Windows.............................................................................................73
Installing OCI8 Using a Local Database on Windows....................................................................73
Installing OCI8 with Instant Client on Windows.............................................................................74
Upgrading OCI8 on Windows........................................................................................................75
Installing OCI8 with Oracle Application Server on Linux...................................................................76
Installing PHP With PDO..................................................................................................................78
Installing PDO on Linux.................................................................................................................79
Installing PDO on Windows...........................................................................................................80
Checking OCI8 and PDO_OCI Installation.......................................................................................80
Chapter 8 Installing Zend Core for Oracle............................................................................83
Installing Zend Core for Oracle.........................................................................................................83
Installing Zend Core for Oracle on Linux.......................................................................................83
Testing the Zend Core for Oracle Installation on Linux..................................................................89
Installing Zend Core for Oracle on Windows.................................................................................90
Testing the Zend Core for Oracle Installation on Windows............................................................97
Configuring Zend Core for Oracle.....................................................................................................97
Chapter 9 Connecting to Oracle Using OCI8.....................................................................101
Oracle Connection Types...............................................................................................................101
Standard Connections.................................................................................................................101
Multiple Unique Connections.......................................................................................................101
Persistent Connections...............................................................................................................101
Oracle Database Name Connection Identifiers...............................................................................102
Easy Connect String....................................................................................................................103
Database Connect Descriptor String...........................................................................................104
Database Connect Name............................................................................................................104
Common Connection Errors...........................................................................................................105
Setting Oracle Environment Variables for Apache..........................................................................106
Closing Oracle Connections...........................................................................................................108
Close Statement Resources Before Closing Connections...........................................................109
Transactions and Connections....................................................................................................110
Session State with Persistent Connections..................................................................................110
Optional Connection Parameters....................................................................................................111
Connection Character Set............................................................................................................111
Connection Session Mode...........................................................................................................112
Changing the Database Password..................................................................................................114
Changing Passwords On Demand...............................................................................................114
Changing Expired Passwords......................................................................................................115
Tuning Oracle Connections in PHP.................................................................................................117
Use the Best Connection Function..............................................................................................117
Pass the Character Set................................................................................................................117
Do Not Set the Date Format Unnecessarily.................................................................................117
v
The Underground PHP and Oracle® Manual
About this Book
This book is for PHP programmers developing applications for
an Oracle database. It bridges the gap between the many PHP
and the many Oracle books available. It shows how to use the
PHP scripting language with the Oracle database, from
installation to using them together efficiently.
You may be starting out with PHP for your Oracle
database. You may be a PHP programmer wanting to learn
Oracle. You may be unsure how to install PHP or Oracle. Or
you may just want to know the latest best practices. This book
gives you the fundamental building blocks needed to create
high performance PHP Oracle web applications.
About the Authors
Christopher Jones works for Oracle on dynamic scripting languages with a strong focus on
PHP. He is is a lead maintainer of PHP's open source OCI8 extension and liaises closely with
the PHP community. He also helps make future versions of the Oracle database better for PHP.
He is the author of various technical articles on PHP and Oracle, and has presented at
conferences including PHP|Tek, the International PHP Conference, the O'Reilly Open Source
Convention, and ZendCon. He also helps present Oracle-PHP tutorials and PHPFests
worldwide.
Alison Holloway is a Senior Product Manager at Oracle with a number of years experience in
advanced technology. She has presented at various PHP conferences. Most recently she has
been working with Oracle VM.