You are on page 1of 290

The Underground PHP and Oracle Manual

CH R I STO P H E R JO N E S A N D A L I S O N HO L LOWAY

PH

The Underground PHP and Oracle Manual, Release 1.5, December 2008. o!"r#gh$ % 2008, Oracle. &ll r#gh$s reser'ed. &u$hors( hr#s$o!her )ones and &l#son Hollo*a" on$r#bu$ors and ac+no*ledgmen$s( ,lad#m#r -arr#ere, .u/# h#dambaran, Rober$ le'enger, &n$on" Do'gal, 0e1 2urlong, 3ue Har!er, Manuel Ho45eld, 6en )acobs, 3r#na$h 6r#shnas*am", 3hoa#b .ar#, 3#mon .a*, 6r#shna Mohan, huc+ Murra", 6e'#n 7eel, 6an$ Pa$el, harles Poulsen, 6ar$h#+ Ra8an, R#chard Rendell, Ro" Rossebo, M#chael 3e+urs+#, 3ree+umar 3eshadr#, Mohammad 3o*dagar, Ma+o$o To1a*a, Todd Tr#chler, 3#mon 0a$$, 9ah#, 3hu!#ng 9hou. The la$es$ ed#$#on o5 $h#s boo+ #s a'a#lable onl#ne a$( http://otn.oracle.com/goto/underground-php-oracle-manual The Programs :*h#ch #nclude bo$h $he so5$*are and documen$a$#on; con$a#n !ro!r#e$ar" #n5orma$#on< $he" are !ro'#ded under a l#cense agreemen$ con$a#n#ng res$r#c$#ons on use and d#sclosure and are also !ro$ec$ed b" co!"r#gh$, !a$en$, and o$her #n$ellec$ual and #ndus$r#al !ro!er$" la*s. Re'erse eng#neer#ng, d#sassembl", or decom!#la$#on o5 $he Programs, e/ce!$ $o $he e/$en$ re=u#red $o ob$a#n #n$ero!erab#l#$" *#$h o$her #nde!enden$l" crea$ed so5$*are or as s!ec#5#ed b" la*, #s !roh#b#$ed. The #n5orma$#on con$a#ned #n $h#s documen$ #s sub8ec$ $o change *#$hou$ no$#ce. >5 "ou 5#nd an" !roblems #n $he documen$a$#on, !lease re!or$ $hem $o us #n *r#$#ng. Th#s documen$ #s no$ *arran$ed $o be error?5ree. @/ce!$ as ma" be e/!ressl" !erm#$$ed #n "our l#cense agreemen$ 5or $hese Programs, no !ar$ o5 $hese Programs ma" be re!roduced or $ransm#$$ed #n an" 5orm or b" an" means, elec$ron#c or mechan#cal, 5or an" !ur!ose. U.S. GOVERNMENT RIGHTS Programs, so5$*are, da$abases, and rela$ed documen$a$#on and $echn#cal da$a del#'ered $o U.3. Ao'ernmen$ cus$omers are Bcommerc#al com!u$er so5$*areB or Bcommerc#al $echn#cal da$aB !ursuan$ $o $he a!!l#cable 2ederal &c=u#s#$#on Regula$#on and agenc"?s!ec#5#c su!!lemen$al regula$#ons. &s such, use, du!l#ca$#on, d#sclosure, mod#5#ca$#on, and ada!$a$#on o5 $he Programs, #nclud#ng documen$a$#on and $echn#cal da$a, shall be sub8ec$ $o $he l#cens#ng res$r#c$#ons se$ 5or$h #n $he a!!l#cable Oracle l#cense agreemen$, and, $o $he e/$en$ a!!l#cable, $he add#$#onal r#gh$s se$ 5or$h #n 2&R 52.22C?1D, ommerc#al om!u$er 3o5$*are??Res$r#c$ed R#gh$s :)une 1D8C;. Oracle U3&, >nc., 500 Oracle Par+*a", Red*ood #$", & DE0F5. The Programs are no$ #n$ended 5or use #n an" nuclear, a'#a$#on, mass $rans#$, med#cal, or o$her #nheren$l" dangerous a!!l#ca$#ons. >$ shall be $he l#censeeGs res!ons#b#l#$" $o $a+e all a!!ro!r#a$e 5a#l?sa5e, bac+u!, redundanc" and o$her measures $o ensure $he sa5e use o5 such a!!l#ca$#ons #5 $he Programs are used 5or such !ur!oses, and *e d#scla#m l#ab#l#$" 5or an" damages caused b" such use o5 $he Programs. The Programs ma" !ro'#de l#n+s $o 0eb s#$es and access $o con$en$, !roduc$s, and ser'#ces 5rom $h#rd !ar$#es. Oracle #s no$ res!ons#ble 5or $he a'a#lab#l#$" o5, or an" con$en$ !ro'#ded on, $h#rd?!ar$" 0eb s#$es. Hou bear all r#s+s assoc#a$ed *#$h $he use o5 such con$en$. >5 "ou choose $o !urchase an" !roduc$s or ser'#ces 5rom a $h#rd !ar$", $he rela$#onsh#! #s d#rec$l" be$*een "ou and $he $h#rd !ar$". Oracle #s no$ res!ons#ble 5or( :a; $he =ual#$" o5 $h#rd?!ar$" !roduc$s or ser'#ces< or :b; 5ul5#ll#ng an" o5 $he $erms o5 $he agreemen$ *#$h $he $h#rd !ar$", #nclud#ng del#'er" o5 !roduc$s or ser'#ces and *arran$" obl#ga$#ons rela$ed $o !urchased !roduc$s or ser'#ces. Oracle #s no$ res!ons#ble 5or an" loss or damage o5 an" sor$ $ha$ "ou ma" #ncur 5rom deal#ng *#$h an" $h#rd !ar$". Oracle, )D @d*ards, and Peo!le3o5$ are reg#s$ered $rademar+s o5 Oracle or!ora$#on andIor #$s a55#l#a$es. O$her names ma" be $rademar+s o5 $he#r res!ec$#'e o*ners.

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 ! ............................................................................................................................2

Chapter

!ettin" Started With PHP.......................................................................................#

"reatin# and $ditin# ! Scripts.......................................................................................................% ! S&nta' O(er(ie)........................................................................................................................% Runnin# ! Scripts..........................................................................................................................* Runnin# ! Scripts in a Bro)ser..................................................................................................* Runnin# Scripts )ith "ommand +ine ! ......................................................................................* Debu##in# ! Scripts...................................................................................................................*

Chapter $ PHP Orac%e E&ten'ion'.........................................................................................11


! Oracle $'tensions....................................................................................................................11 Oracle $'tension...........................................................................................................................11 O"I* $'tension.............................................................................................................................11 DO $'tension..............................................................................................................................12 ! Database ,bstraction +ibraries.................................................................................................1,DOdb..........................................................................................................................................1$,R DB......................................................................................................................................1. $,R /DB2.................................................................................................................................1. 0ettin# the O"I* $'tension..............................................................................................................1. O"I* and Oracle Installation Options............................................................................................1% 0ettin# the DO $'tension...............................................................................................................11 2end "ore 3or Oracle........................................................................................................................14 The ! Release "&cle...................................................................................................................14

Chapter ( In'ta%%in" Orac%e Data)a'e 1*" E&pre'' Edition................................................1+


Oracle Database $ditions.................................................................................................................15 Oracle Database 6$.........................................................................................................................15 Installin# Oracle Database 6$ on +inu'...........................................................................................27 Installin# Oracle Database 6$ on Debian8 Ubuntu8 and 9ubuntu.....................................................21 Installin# Oracle Database 6$ on Windo)s......................................................................................22 Testin# the Oracle Database 6$ Installation.....................................................................................2. "on3i#urin# Oracle Database 6$......................................................................................................2% Settin# the Oracle Database 6$ $n(ironment :ariables on +inu'.................................................2%

iii

$nablin# Database Startup and Shutdo)n 3rom /enus on +inu'..................................................21 Startin# and Stoppin# the +istener and Database.........................................................................21 $nablin# Remote "lient "onnection..............................................................................................25

Chapter # ,'in" Orac%e Data)a'e.........................................................................................$1


Oracle ,pplication $'press...............................................................................................................-1 +o##in# In To Oracle ,pplication $'press.....................................................................................-1 Unlockin# the !R User..................................................................................................................-2 "reatin# Database Ob;ects...........................................................................................................-Workin# )ith S<+ Scripts..............................................................................................................-4 "reatin# a +=S<+ rocedure.......................................................................................................-* "reatin# a Database User..............................................................................................................7 /onitorin# Database Sessions.......................................................................................................2 Database Backup and Reco(er&..................................................................................................... Oracle S<+> lus...............................................................................................................................4 Startin# S<+> lus..........................................................................................................................* $'ecutin# S<+ and +=S<+ Statements in S<+> lus....................................................................5 "ontrollin# <uer& Output in S<+> lus............................................................................................5 Runnin# Scripts in S<+> lus.........................................................................................................%7 In3ormation On Tables in S<+> lus...............................................................................................%7 ,ccessin# the Demonstration Tables in S<+> lus........................................................................%1 Oracle S<+ De(eloper......................................................................................................................%1 "reatin# a Database "onnection..................................................................................................%1 "reatin# a Table............................................................................................................................%. $'ecutin# a S<+ <uer&.................................................................................................................%% $ditin#8 "ompilin# and Runnin# +=S<+.......................................................................................%4 Runnin# Reports...........................................................................................................................%5 "reatin# Reports...........................................................................................................................11

Chapter - In'ta%%in" Apache HTTP Ser.er............................................................................-$


Installin# ,pache !TT Ser(er on +inu'..........................................................................................1Startin# and Stoppin# ,pache !TT Ser(er.................................................................................1. "on3i#urin# ,pache !TT Ser(er on +inu'...................................................................................1. Installin# ,pache !TT Ser(er on Windo)s....................................................................................1. Startin# and Stoppin# ,pache !TT Ser(er.................................................................................1%

Chapter / In'ta%%in" PHP........................................................................................................-/


Installin# ! )ith O"I* on +inu'....................................................................................................14 Installin# O"I* Usin# a +ocal Database........................................................................................14 Installin# O"I* Usin# Oracle Instant "lient...................................................................................15 Up#radin# ! )ith $"+ O"I* on +inu'.......................................................................................47 Up#radin# O"I* as a Static +ibrar& on +inu' ................................................................................47 Up#radin# O"I* on +inu' Usin# the $"+ "hannel......................................................................41 Up#radin# O"I* as a Shared +ibrar& on +inu'..............................................................................42
iv

Installin# ! With O"I* on Windo)s.............................................................................................4Installin# O"I* Usin# a +ocal Database on Windo)s....................................................................4Installin# O"I* )ith Instant "lient on Windo)s.............................................................................4. Up#radin# O"I* on Windo)s........................................................................................................4% Installin# O"I* )ith Oracle ,pplication Ser(er on +inu'...................................................................41 Installin# ! With DO..................................................................................................................4* Installin# DO on +inu'.................................................................................................................45 Installin# DO on Windo)s...........................................................................................................*7 "heckin# O"I* and DO?O"I Installation.......................................................................................*7

Chapter 0 In'ta%%in" 1end Core 2or Orac%e............................................................................0$


Installin# 2end "ore 3or Oracle.........................................................................................................*Installin# 2end "ore 3or Oracle on +inu'.......................................................................................*Testin# the 2end "ore 3or Oracle Installation on +inu'..................................................................*5 Installin# 2end "ore 3or Oracle on Windo)s.................................................................................57 Testin# the 2end "ore 3or Oracle Installation on Windo)s............................................................54 "on3i#urin# 2end "ore 3or Oracle.....................................................................................................54

Chapter + Connectin" to Orac%e ,'in" OCI0.....................................................................1*1


Oracle "onnection T&pes...............................................................................................................171 Standard "onnections.................................................................................................................171 /ultiple Uni@ue "onnections.......................................................................................................171 ersistent "onnections...............................................................................................................171 Oracle Database Aame "onnection Identi3iers...............................................................................172 $as& "onnect Strin#....................................................................................................................17Database "onnect Descriptor Strin#...........................................................................................17. Database "onnect Aame............................................................................................................17. "ommon "onnection $rrors...........................................................................................................17% Settin# Oracle $n(ironment :ariables 3or ,pache..........................................................................171 "losin# Oracle "onnections...........................................................................................................17* "lose Statement Resources Be3ore "losin# "onnections...........................................................175 Transactions and "onnections....................................................................................................117 Session State )ith ersistent "onnections..................................................................................117 Optional "onnection arameters....................................................................................................111 "onnection "haracter Set............................................................................................................111 "onnection Session /ode...........................................................................................................112 "han#in# the Database ass)ord..................................................................................................11. "han#in# ass)ords On Demand...............................................................................................11. "han#in# $'pired ass)ords......................................................................................................11% Tunin# Oracle "onnections in ! .................................................................................................114 Use the Best "onnection Bunction..............................................................................................114 ass the "haracter Set................................................................................................................114 Do Aot Set the Date Bormat Unnecessaril&.................................................................................114
v

/ana#in# ersistent "onnections..................................................................................................115 /a'imum Aumber o3 ersistent "onnections ,llo)ed.................................................................115 Timeout 3or Unused ersistent "onnections................................................................................115 in#in# 3or "losed ersistent "onnections..................................................................................115 ,pache "on3i#uration arameters...............................................................................................127 Reducin# Database Ser(er /emor& Used B& ersistent "onnections........................................127 Oracle Aet and ! .......................................................................................................................121 "onnection Rate +imitin#............................................................................................................121 Settin# "onnection Timeouts.......................................................................................................122 "on3i#urin# ,uthentication /ethods............................................................................................122 Detectin# Dead ! ,pache Sessions.......................................................................................12Other Oracle Aet OptimiCations...................................................................................................12Tracin# Oracle Aet......................................................................................................................12"onnection /ana#ement in Scalable S&stems...............................................................................12.

Chapter 1* E&ecutin" S3L State4ent' With OCI0............................................................1 #


S<+ Statement $'ecution Steps....................................................................................................12% <uer& $'ample............................................................................................................................12% Oracle Datat&pes.........................................................................................................................124 Betch Bunctions..............................................................................................................................124 Betchin# as a Aumeric ,rra&.......................................................................................................12* Betchin# as an ,ssociati(e ,rra&.................................................................................................125 Betchin# as an Ob;ect.................................................................................................................1-7 De3inin# Output :ariables............................................................................................................1-1 Betchin# and Workin# )ith Aumbers...........................................................................................1-1 Betchin# and Workin# )ith Dates................................................................................................1-2 Insert8 Update8 Delete8 "reate and Drop........................................................................................1-. Transactions...................................................................................................................................1-. ,utonomous Transactions...........................................................................................................1-1 The Transactional Beha(ior o3 "onnections................................................................................1-4 ! $rror !andlin#........................................................................................................................1-* !andlin# O"I* $rrors..................................................................................................................1-* Tunin# S<+ Statements in ! ,pplications..................................................................................1.1 Usin# Bind :ariables...................................................................................................................1.1 Tunin# the re3etch SiCe.............................................................................................................1.5 Tunin# the Statement "ache SiCe...............................................................................................1%7 Usin# the Ser(er and "lient <uer& Result "aches......................................................................1%1 +imitin# Ro)s and "reatin# a#ed Datasets..................................................................................1%,utoDIncrement "olumns................................................................................................................1%% 0ettin# the +ast Insert ID................................................................................................................1%4 $'plorin# Oracle.............................................................................................................................1%4 "ase Insensiti(e <ueries.............................................................................................................1%4
vi

,nal&tic Bunctions in S<+...........................................................................................................1%*

Chapter 11 ,'in" PL5S3L With OCI0..................................................................................1#+


+=S<+ O(er(ie)...........................................................................................................................1%5 Blocks8 rocedures8 acka#es and Tri##ers..................................................................................117 ,non&mous Blocks......................................................................................................................117 Stored or Standalone rocedures and Bunctions........................................................................117 acka#es....................................................................................................................................111 Tri##ers.......................................................................................................................................112 "reatin# +=S<+ Stored rocedures in ! ..................................................................................112 $nd o3 +ine Terminators in +=S<+ )ith Windo)s ! ...............................................................112 "allin# +=S<+ "ode......................................................................................................................11"allin# +=S<+ rocedures.........................................................................................................11"allin# +=S<+ Bunctions...........................................................................................................11. Bindin# arameters to rocedures and Bunctions......................................................................11. ,rra& Bindin# and +=S<+ Bulk rocessin#...................................................................................11% +=S<+ Success With In3ormation Warnin#s..................................................................................114 Usin# R$B "URSORS 3or Result Sets...........................................................................................11* "losin# "ursors...........................................................................................................................147 "on(ertin# 3rom R$B "URSOR to I $+IA$D Results..............................................................142 Oracle "ollections in ! ..............................................................................................................14Usin# +=S<+ and S<+ Ob;ect T&pes in ! ................................................................................14% Usin# O"I* "ollection Bunctions.................................................................................................141 Usin# a R$B "URSOR................................................................................................................144 Bindin# an ,rra&..........................................................................................................................145 Usin# a I $+IA$D Bunction......................................................................................................1*7 0ettin# Output )ith DB/S?OUT UT.............................................................................................1*1 +=S<+ Bunction Result "ache......................................................................................................1*Usin# Oracle +ocator 3or Spatial /appin#......................................................................................1*. Insertin# +ocator Data.................................................................................................................1*. <ueries Returnin# Scalar :alues.................................................................................................1*. Selectin# :ertices Usin# SDO?UTI+.0$T:$RTI"$S.................................................................1*1 Usin# a "ustom Bunction............................................................................................................1*1 Schedulin# Back#round or +on# Runnin# Operations....................................................................1** Reusin# rocedures Written 3or /OD? +S<+...............................................................................151

Chapter 1 ,'in" Lar"e O)6ect' in OCI0............................................................................1+$


Workin# )ith +OBs.........................................................................................................................15Insertin# and Updatin# +OBs......................................................................................................15Betchin# +OBs.............................................................................................................................15. Temporar& +OBs.........................................................................................................................15% +OBs and +=S<+ procedures....................................................................................................151 Other +OB /ethods....................................................................................................................154
vii

Workin# )ith BBI+$s......................................................................................................................15*

Chapter 1$ ,'in" 78L 9ith Orac%e and PHP..................................................................... *$


Betchin# Relational Ro)s as 6/+..................................................................................................27Betchin# Ro)s as Bull& Bormed 6/+.............................................................................................27. Usin# the Simple6/+ $'tension in ! .........................................................................................27% Betchin# 6/+T&pe "olumns...........................................................................................................271 Insertin# into 6/+T&pe "olumns....................................................................................................274 Betchin# an 6/+T&pe 3rom a +=S<+ Bunction..............................................................................275 6<uer& 6/+ <uer& +an#ua#e........................................................................................................217 ,ccessin# Data o(er !TT )ith 6/+ DB.......................................................................................212

Chapter 1( PHP Sca%a)i%it: and Hi"h A.ai%a)i%it:.............................................................. 1$


Database Resident "onnection oolin#.........................................................................................21!o) DR" Works.......................................................................................................................21. ! O"I* "onnections and DR" .............................................................................................211 When to use DR" .....................................................................................................................21* Sharin# the Ser(er ool..............................................................................................................215 Usin# DR" in ! .......................................................................................................................227 "on3i#urin# and $nablin# the ool..............................................................................................221 "on3i#urin# ! 3or DR" .........................................................................................................22,pplication Deplo&ment 3or DR" ...............................................................................................22. /onitorin# DR" ............................................................................................................................221 :E RO"$SS and :ES$SSIOA :ie)s........................................................................................224 DB,?" OO+?IABO :ie)...........................................................................................................224 :E" OO+?ST,TS :ie)..............................................................................................................224 :E" OO+?""?ST,TS :ie).......................................................................................................225 !i#h ,(ailabilit& )ith B,A and R,"................................................................................................225 "on3i#urin# B,A $(ents in the Database....................................................................................2-7 "on3i#urin# ! 3or B,A.............................................................................................................2-7 ,pplication Deplo&ment 3or B,A..................................................................................................2-7 R," "onnection +oad Balancin# )ith ! .................................................................................2-1

Chapter 1# !%o)a%i;ation...................................................................................................... $$
$stablishin# the $n(ironment Bet)een Oracle and ! ................................................................2-/anipulatin# Strin#s.......................................................................................................................2-% Determinin# the +ocale o3 the User................................................................................................2-% $ncodin# !T/+ a#es...................................................................................................................2-1 Speci3&in# the a#e $ncodin# 3or !T/+ a#es..........................................................................2-1 Speci3&in# the $ncodin# in the !TT !eader.............................................................................2-4 Speci3&in# the $ncodin# in the !T/+ a#e !eader....................................................................2-4 Speci3&in# the a#e $ncodin# in ! .........................................................................................2-4 Or#aniCin# the "ontent o3 !T/+ a#es 3or Translation..................................................................2-4 Strin#s in ! .............................................................................................................................2-*
viii

Static Biles...................................................................................................................................2-* Data 3rom the Database..............................................................................................................2-* resentin# Data Usin# "on(entions $'pected b& the User............................................................2-* Oracle Aumber Bormats..............................................................................................................2-5 Oracle Date Bormats...................................................................................................................2.7 Oracle +in#uistic Sorts................................................................................................................2.2 Oracle $rror /essa#es...............................................................................................................2.-

Chapter 1- Te'tin" PHP and the OCI0 E&ten'ion.............................................................. (#


Runnin# O"I* Tests........................................................................................................................2.% Runnin# a Sin#le Test..................................................................................................................2.4 Tests that Bail..............................................................................................................................2.4 "reatin# O"I* Tests ......................................................................................................................2.* O"I* Test !elper Scripts.............................................................................................................2.5 "on3i#urin# the Database Bor Testin#............................................................................................2.5

Appendi& A Tracin" OCI0 Interna%'..................................................................................... #$


$nablin# O"I* Debu##in# output...................................................................................................2%-

Appendi& < OCI0 php.ini Para4eter'................................................................................. ## Appendi& C OCI0 =unction Na4e' in PHP ( and PHP #................................................... #/ Appendi& D The O)'o%ete Orac%e E&ten'ion...................................................................... -1
Oracle and O"I* "omparison........................................................................................................211

Appendi& E Re'ource'......................................................................................................... -/
0eneral In3ormation and Borums....................................................................................................214 Oracle Documentation....................................................................................................................214 Selected ! and Oracle Books....................................................................................................21* So3t)are and Source "ode.............................................................................................................215 ! +inks.......................................................................................................................................241

!%o''ar:................................................................................................................................. /$

ix

CHAPTER 1

INTROD,CTION
Th#s boo+ #s des#gned $o br#dge $he ga! be$*een $he man" PHP and $he man" Oracle $e/$s a'a#lable. >$ sho*s ho* $o use $he PHP scr#!$#ng language *#$h $he Oracle da$abase, 5rom #ns$alla$#on $o us#ng $hem e55#c#en$l". The #ns$alla$#on and da$abase d#scuss#on #n $h#s boo+ h#ghl#gh$s $he Oracle Da$abase 10g @/!ress @d#$#on, bu$ e'er"$h#ng co'ered #n $h#s boo+ also a!!l#es $o $he o$her ed#$#ons o5 $he Oracle da$abase, #nclud#ng Oracle Da$abase 11g. The PHP "ou *r#$e 5or Oracle Da$abase 10g @/!ress @d#$#on can be run, *#$hou$ change, aga#ns$ all ed#$#ons o5 $he Oracle da$abase as *ell. The boo+ con$a#ns much un#=ue ma$er#al on PHPGs Oracle O >8 and PDOJO > e/$ens#ons. >$ also #ncor!ora$es se'eral u!da$ed #ns$alla$#on gu#des !re'#ousl" !ubl#shed on $he Oracle Technolog" 7e$*or+ *eb s#$e. The cha!$er on global#1a$#on #s der#'ed 5rom $he Oracle Database Express Edition 2 Day Plus PHP Developer Guide. 3ue Har!er con$r#bu$ed $he cha!$er on 3K. De'elo!er. The cha!$er on PHP 3calab#l#$" and H#gh &'a#lab#l#$" #s der#'ed 5rom $he Oracle *h#$e!a!er, PHP Scalability and High Availability, &!r#l 2008. 0e gra$e5ull" ac+no*ledge all $he Oracle s$a55 $ha$ con$r#bu$ed $o $h#s boo+.

Who Shou%d Read Thi' <oo>?


Th#s boo+ #s a#med a$ PHP !rogrammers *ho are de'elo!#ng a!!l#ca$#ons 5or an Oracle da$abase. >$ br#dges $he ga! be$*een $he man" PHP and $he man" Oracle boo+s a'a#lable. >$ sho*s ho* $o use $he PHP scr#!$#ng language *#$h $he Oracle da$abase, 5rom #ns$alla$#on $o us#ng $hem $oge$her e55#c#en$l". Hou ma" alread" be us#ng ano$her da$abase and ha'e a re=u#remen$ or a !re5erence $o mo'e $o Oracle. Hou ma" be s$ar$#ng ou$ *#$h PHP da$abase de'elo!men$. Hou ma" be unsure ho* $o #ns$all PHP and Oracle. Hou ma" be unclear abou$ bes$ !rac$#ces. Th#s boo+ a#ms $o remo'e an" con5us#on. Th#s boo+ #s no$ a com!le$e PHP s"n$a/ or Oracle 3K. gu#de. >$ does no$ descr#be o'erall a!!l#ca$#on arch#$ec$ure. >$ #s assumed $ha$ "ou alread" ha'e bas#c PHP and 3K. +no*ledge and *an$ bes$ !rac$#ces #n us#ng PHP aga#ns$ an Oracle da$abase. 3#nce $he 5#rs$ release o5 $he Underground PHP and Oracle Manual $here ha'e been man" commerc#all" a'a#lable boo+s s!ec#5#call" on PHP and Oracle !ubl#shed. The" are *or$h*h#le add#$#ons $o "our l#brar". @ach has a d#55eren$ '#e*!o#n$ and sho*s some$h#ng ne* abou$ $he $echnolog#es. OracleLs o*n e/$ens#'e se$ o5 documen$a$#on #s 5reel" a'a#lable onl#ne. 2or ne*comers *e sugges$ read#ng $he Oracle Database Express Edition 2 Day Plus PHP Developer Guide *h#ch *al+s $hrough bu#ld#ng a PHP a!!l#ca$#on aga#ns$ an Oracle da$abase. om!rehens#'e PHP documen$a$#on and resources are also onl#ne. UR.s $o $he mos$ use5ul onl#ne resources and boo+s are l#s$ed #n $he Resources a!!end#/.

Introduction to Orac%e
The Oracle Da$abase #s *ell +no*n 5or #$s scalab#l#$", rel#ab#l#$" and 5ea$ures. >$ #s $he lead#ng da$abase and #s a'a#lable on man" !la$5orms.
1

Introduction

There are some sub$le d#55erences be$*een $he $erm#nolog" used *hen descr#b#ng an Oracle da$abase and a da$abase 5rom o$her so5$*are 'endors. The 5ollo*#ng o'er'#e* o5 $he ma#n Oracle $erms m#gh$ hel! "ou $o unders$and $he Oracle $erm#nolog". hec+ $he Alossar" 5or more descr#!$#ons.

Data)a'e' and In'tance'


&n Oracle da$abase s$ores and re$r#e'es da$a. @ach da$abase cons#s$s o5 one or more da$a 5#les. &n Oracle database server cons#s$s o5 an Oracle database and an Oracle instance. @'er" $#me a ser'er #s s$ar$ed, a shared memor" reg#on called $he system global area :3A&; #s alloca$ed and $he Oracle bac+ground !rocesses are s$ar$ed. The comb#na$#on o5 $he bac+ground !rocesses and 3A& #s called an Oracle instance. On some o!era$#ng s"s$ems, l#+e 0#ndo*s, $here are no se!ara$e bac+ground !rocesses. >ns$ead $hreads run *#$h#n $he Oracle #mage.

Ta)%e'pace'
Tables!aces are $he log#cal un#$s o5 da$a s$orage made u! o5 one or more da$a5#les. Tables!aces are o5$en crea$ed 5or #nd#'#dual a!!l#ca$#ons because $ables!aces can be con'en#en$l" managed. Users are ass#gned a de5aul$ $ables!ace $ha$ holds all $he da$a $he users crea$es. & da$abase #s made u! o5 de5aul$ and D-&? crea$ed $ables!aces.

Sche4a' and ,'er'


& schema #s a collec$#on o5 da$abase ob8ec$s such as $ables and #nde/es. & schema #s o*ned b" a da$abase user and has $he same name as $ha$ user. Man" !eo!le use $he *ords schema and user #n$erchangeabl". Once "ou ha'e #ns$alled PHP and *an$ $o *r#$e scr#!$s $ha$ #n$erac$ *#$h Oracle, "ou need $o connec$ as $he o*ner o5 $he schema $ha$ con$a#ns $he ob8ec$s "ou *an$ $o #n$erac$ *#$h. 2or e/am!le, $o connec$ $o $he HR schema, "ou *ould use $he username hr #n PHPLs connec$#on s$r#ng. &l$hough "ou ma" ha'e more $han one da$abase !er mach#ne, $"!#call" a s#ngle Oracle da$abase con$a#ns mul$#!le schemas. Mul$#!le a!!l#ca$#ons can use $he same da$abase *#$hou$ an" con5l#c$ b" us#ng d#55eren$ schemas. >ns$ead o5 us#ng a CREATE DATABASE command 5or ne* a!!l#ca$#ons, use $he CREATE USER command $o crea$e a ne* schema #n $he da$abase. >n Oracle Da$abase 10g @/!ress @d#$#on :+no*n as MOracle Da$abase N@O; $here #s a *#1ard $o crea$e ne* users #n $he Oracle &!!l#ca$#on @/!ress managemen$ console.

Introduction to PHP
PHP #s a hugel" !o!ular, #n$er!re$ed scr#!$#ng language commonl" used 5or *eb a!!l#ca$#ons. PHP #s o!en source and 5ree, and has a -3D?s$"le l#cense, ma+#ng #$ cor!ora$#on?5r#endl". PHP #s !er5ec$ 5or ra!#dl" de'elo!#ng a!!l#ca$#ons bo$h b#g and small, and #s grea$ 5or crea$#ng 0eb 2.0 a!!l#ca$#ons. >$ !o*ers o'er $*en$" m#ll#on *eb s#$es on $he >n$erne$ and has a huge user commun#$" beh#nd #$. >$ runs on man" !la$5orms. The language #s d"nam#call" $"!ed and eas" $o use. PHP comes *#$h man" e/$ens#ons o55er#ng all +#nds o5 5unc$#onal#$" such as da$abase access. PHP 5 #n$roduced s$rong ob8ec$ or#en$a$ed ca!ab#l#$#es.

Introduction to PHP

PHP #s $"!#call" #ns$alled as an &!ache module, or run b" $he *eb ser'er us#ng 2as$ A>. >$ #ncludes $he PHP O >8 e/$ens#on and #s l#n+ed *#$h $he Oracle l#en$ .#brar#es, enabl#ng Oracle Da$abase access. 0hen a user en$ers $he UR. o5 a PHP scr#!$ hello.php :see s$e! 1 #n 2#gure 1; #n $he#r bro*ser, &!ache #n'o+es PHP $o !rocesses $he 5#le. The PHP code #s e/ecu$ed :2;, *#$h calls $o $he da$abase :P;. 2#nall", $he HTM. ou$!u$ #s re$urned $o $he userGs bro*ser :E;, *h#ch 5orma$s and d#s!la"s $he !age.

igure !" #he $our stages o$ processing a PHP script% The PHP command l#ne #n$er5ace : .>; can also be used $o run PHP scr#!$s 5rom an o!era$#ng s"s$em shell *#ndo*.

Introduction

CHAPTER

!ETTIN! STARTED WITH PHP


Th#s ha!$er g#'es "ou a 'er" br#e5 o'er'#e* o5 $he PHP language. -as#c PHP s"n$a/ #s s#m!le $o learn. >$ has 5am#l#ar loo!s, $es$s and ass#gnmen$ cons$ruc$s.

Creatin" and Editin" PHP Script'


There are a number o5 s!ec#al#1ed PHP ed#$ors a'a#lable, #nclud#ng OracleLs )De'elo!er *h#ch can be con5#gured *#$h a PHP e/$ens#on. Man" de'elo!ers s$#ll !re5er $e/$ ed#$ors, or ed#$ors *#$h modes $ha$ h#ghl#gh$ code s"n$a/ and a#d de'elo!men$. Th#s manual does no$ assume an" !ar$#cular ed#$or or debugger #s be#ng used. PHP scr#!$s o5$en ha'e $he 5#le e/$ens#on %php, bu$ some$#mes %phtml or %inc are also used. The *eb ser'er can be con5#gured $o recogn#1e $he e/$ens#on:s; $ha$ "ou choose.

PHP S:nta& O.er.ie9


PHP scr#!$s are enclosed #n !php and !" $ags. .#nes are $erm#na$ed *#$h a sem#?colon(
!php echo #$ello% &orld'#( !"

-loc+s o5 PHP code and HTM. code ma" be #n$erlea'ed. The PHP code can also e/!l#c#$l" !r#n$ HTM. $ags(
!php echo # h)"#( echo #*ull Re+ult+#( echo # /h)"#( ,output - .no re+ult+ a/a0la1le.( !" ta1le 1order-.2." tr" td" !php echo ,output !" /td" /tr" /ta1le"

The ou$!u$ *hen runn#ng $h#s scr#!$ #s(


h)"*ull Re+ult+ /h)" ta1le 1order-.2." tr" td" no re+ult+ a/a0la1le /td" 5

Getting Started With PHP /tr" /ta1le"

& bro*ser *ould d#s!la" #$ as(

igure 2" PHP script output% PHP s$r#ngs can be enclosed #n s#ngle or double =uo$es(
#A +tr0ng con+tant# .another con+tant.

,ar#able names are !re5#/ed *#$h a dollar s#gn. Th#ngs $ha$ loo+ l#+e 'ar#ables #ns#de a double?=uo$ed s$r#ng *#ll be e/!anded(
.A /alue appear+ here: ,/2.

3$r#ngs and 'ar#ables can also be conca$ena$ed us#ng a !er#od.


#Employee # . ,ename . # 0+ 0n department # . ,dept

,ar#ables do no$ need $"!es declared(


,count - 2( ,ename - #Arn0e#(

&rra"s can ha'e numer#c or assoc#a$#'e #nde/es(


,a2324 - ).2526( ,a73#89#4 - ).2526(

3$r#ngs and 'ar#ables can be d#s!la"ed *#$h an echo or pr0nt s$a$emen$. 2orma$$ed ou$!u$ *#$h pr0ntf:; #s also !oss#ble.
echo #$ello% &orld'#( echo ,/% ,<( pr0nt #$ello% &orld'#( pr0ntf:.There 0+ =d =+.% ,/2% ,/7;(

ode 5lo* can be con$rolled *#$h $es$s and loo!s. PHP also has a s&itch s$a$emen$. The 0fIel+e0fIel+e s$a$emen$s loo+ l#+e(
0f :,+al echo A el+e0f echo A el+e @ echo 6 " >?????; @ #Salary 0+ way too 10g#( :,+al " 6?????; @ #Salary 0+ huge#( #Salary m0ght 1e BC#(

PHP Syntax Overview A

Th#s also sho*s ho* bloc+s o5 code are enclosed #n braces. & $rad#$#onal loo! #s(
for :,0 - ?( ,0 2?( DD,0; @ echo ,0 . . 1r"En.( A

Th#s !r#n$s $he numbers 0 $o D, each on a ne* l#ne. The 'alue o5 ,0 #s #ncremen$ed #n each #$era$#on. The loo! s$o!s *hen $he $es$ cond#$#on e'alua$es $o $rue. Hou can also loo! *#$h wh0le or do wh0le cons$ruc$s. The foreach command #s use5ul $o #$era$e o'er arra"s(
,a) - array:#Aa#% #B1#% #Cc#;( foreach :,a) a+ ,/; @ echo ,/( A

Th#s se$s ,/ $o each elemen$ o5 $he arra" #n $urn. & 5unc$#on ma" be de5#ned(
funct0on myfunc:,p2% ,p7; @ echo ,p2% ,p7( return ,p2 D ,p7( A

2unc$#ons ma" ha'e 'ar#able numbers o5 argumen$s. Th#s 5unc$#on could be called us#ng(
,/) - myfunc:2% );(

2unc$#on calls ma" a!!ear earl#er $han $he 5unc$#on de5#n#$#on. Procedures use $he same $unction +e"*ord bu$ do no$ ha'e a return s$a$emen$. 3ub?5#les can be #ncluded #n PHP scr#!$s *#$h an 0nclude:; or reFu0re:; s$a$emen$.
0nclude:#foo.php#;( reFu0re:#1ar.php#;(

& reFu0re:; *#ll genera$e a 5a$al error #5 $he scr#!$ #s no$ 5ound. The 0ncludeGonce:; and reFu0reGonce:; s$a$emen$s !re'en$ mul$#!le #nclus#ons o5 a 5#le. ommen$s are e#$her s#ngle l#ne(
// a +hort comment

or mul$#?l#ne(
/H A longer comment

H/

Getting Started With PHP

Runnin" PHP Script'


PHP scr#!$s can be loaded #n a bro*ser, or e/ecu$ed a$ a command !rom!$ #n a $erm#nal *#ndo*. -ecause bro*sers #n$er!re$ HTM. $ags and com!ress *h#$e s!ace #nclud#ng ne*?l#nes, scr#!$ ou$!u$ can d#55er be$*een command?l#ne and bro*ser #n'oca$#on o5 $he same scr#!$. Man" as!ec$s o5 PHP are con$rolled b" se$$#ngs #n $he php%ini con5#gura$#on 5#le. The loca$#on o5 $he 5#le #s s"s$em s!ec#5#c. >$s loca$#on, $he l#s$ o5 e/$ens#ons loaded, and $he 'alue o5 all $he #n#$#al#1a$#on se$$#ngs can be 5ound us#ng $he php0nfo:; 5unc$#on(
!php php0nfo:;( !"

,alues can be changed b" ed#$#ng php%ini or us#ng $he 9end ore 5or Oracle console, and res$ar$#ng $he *eb ser'er. 3ome 'alues can also be changed *#$h#n scr#!$s b" us#ng $he 0n0G+et:; 5unc$#on. To connec$ $o Oracle, some Oracle en'#ronmen$ 'ar#ables need $o be se$ be5ore $he *eb ser'er s$ar$s. Th#s #s d#scussed #n $he #ns$alla$#on cha!$ers o5 $h#s boo+.

Runnin" PHP Script' in a <ro9'er


PHP scr#!$s are commonl" run b" load#ng $hem #n a bro*ser( http://localho+t/myphp0nfo.php 0hen a *eb ser'er #s con5#gured $o run PHP 5#les $hrough $he PHP #n$er!re$er, re=ues$#ng $he scr#!$ #n a bro*ser *#ll cause $he PHP code $o be e/ecu$ed and all #$s ou$!u$ $o be s$reamed $o $he bro*ser.

Runnin" Script' 9ith Co44and Line PHP


>5 "our PHP code #s #n a 5#le, and $he PHP e/ecu$able #s #n "our !a$h, run #$ *#$h(
, php myphp0nfo.php

,ar#ous o!$#ons $o $he !h! e/ecu$able con$rol #$s beha'#or. The -h o!$#ons g#'es $he hel! $e/$(
, php -h

ommon o!$#ons *hen 5#rs$ us#ng PHP are --0n0 *h#ch d#s!la"s $he loca$#on o5 $he php%ini 5#le, and -0 *h#ch d#s!la"s $he 'alue o5 $he php%ini se$$#ngs.

De)u""in" PHP Script'


>5 "ou are no$ us#ng a s!ec#al#1ed PHP ed#$or, debugg#ng *#ll be an old?5ash#oned ma$$er o5 us#ng echo $o !r#n$ 'ar#ables and chec+ code 5lo*. The /arGdump:; 5unc$#on #s use5ul 5or debugg#ng because #$ 5orma$s and !r#n$s com!le/ 'ar#ables(
,a73#89#4 - ).2526( /arGdump:,a7;(

The ou$!u$ #s(


array:2; @

!unning PHP Scri"t# 3.89.4-" float:).2526; A

The 5orma$$#ng #s a!!aren$ *hen us#ng command?l#ne PHP. >n a bro*ser, $o !re'en$ *h#$e s!ace and ne* l#nes coalesc#ng, "ou *#ll need $o do(
echo # pre"#( ,a73#89#4 - ).2526( /arGdump:,a7;( echo # /pre"#(

3ome e/am!les #n $h#s manual use /arGdump:; $o s#m!l#5" $he code be#ng demons$ra$ed or $o sho* $he $"!e and con$en$s o5 a 'ar#able.

Getting Started With PHP

1%

CHAPTER $

PHP ORACLE E7TENSIONS


PHP has se'eral e/$ens#ons $ha$ le$ a!!l#ca$#ons use Oracle. There are also da$abase abs$rac$#on l#brar#es *r#$$en #n PHP *h#ch are !o!ular. &l$hough $h#s boo+ concen$ra$es on $he O >8 e/$ens#on 5or PHP, #$ #s *or$h +no*#ng $he al$erna$#'es. Da$abase access #n each e/$ens#on and abs$rac$#on l#brar" #s 5undamen$all" s#m#lar. The d#55erences are #n $he#r su!!or$ 5or ad'anced 5ea$ures and $he !rogramm#ng me$hodolog" !romo$ed. >5 "ou *an$ $o ma+e 5ull use o5 OracleGs 5ea$ures and *an$ h#gh !er5ormance $hen use O >8, *h#ch #s PHPLs ma#n Oracle e/$ens#on. >5 "ou *an$ da$abase #nde!endence, cons#der us#ng $he PHP Da$a Ob8ec$ :PDO; e/$ens#on or $he &DOdb abs$rac$#on l#brar". The PHP *orld can mo'e a$ a 5as$ !ace, so e/am#ne "our re=u#remen$s and $he la$es$ #n5orma$#on a'a#lable be5ore s$ar$#ng a !ro8ec$.

PHP Orac%e E&ten'ion'


The PHP Oracle e/$ens#ons are *r#$$en #n

and l#n+ed #n$o $he PHP b#nar". The e/$ens#ons are(

Oracle O >8 PDO

Hou can also use $he OD- e/$ens#on.

Orac%e E&ten'ion
The e/$ens#on called MOracleO *as #ncluded #n PHP P, E and 5.0. >$ had l#m#$ed 5unc$#onal#$", #s no longer #n PHP, and #s no$ ma#n$a#ned. The e/$ens#on accessed $he da$abase us#ng OracleLs obsole$e MO >CO language &P>. 7e* de'elo!men$ us#ng $h#s e/$ens#on #s no$ recommended.

OCI0 E&ten'ion
O >8 #s $he recommended e/$ens#on $o use. >$ #s #ncluded #n PHP P, E, and 5. >$ #s also #n PHP F, *h#ch #s #n $he earl" s$age o5 de'elo!men$. >$ #s o!en source and ma#n$a#ned b" $he PHP commun#$". Oracle #s a member o5 $he commun#$" loo+#ng a5$er O >8. There ha'e been ma8or and m#nor changes $o $he O >8 e/$ens#on #n 'ar#ous 'ers#ons o5 PHP. >$ #s recommended $o u!grade $he de5aul$ O >8 code #n PHP E Q PHP 5.2 $o $he la$es$ 'ers#on o5 $he e/$ens#on. &n e/am!le scr#!$ $ha$ 5#nds c#$" names 5rom $he locations $able us#ng O >8( Script !" intro%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( 11

PHP Orac&e 'xten#ion# ,+ - oc0Gpar+e:,c% #+elect c0ty from locat0on+#;( oc0Ge<ecute:,+;( wh0le :,re+ - oc0GfetchGarray:,+% BC9GASSBC;; @ echo ,re+3#C9TJ#4 . . 1r".( A !"

0hen #n'o+ed #n a *eb bro*ser, #$ connec$s as $he demons$ra$#on user hr o5 $he Oracle Da$abase N@ da$abase runn#ng on $he local mach#ne. The =uer" #s e/ecu$ed and a *eb !age o5 resul$s #s d#s!la"ed #n $he bro*ser(

igure '" PHP Output in a &eb bro&ser% >n PHP 5, some e/$ens#on 5unc$#on names *ere s$andard#1ed. PHP E 5unc$#ons l#+e BC9Kog0n:; became oc0Gconnect:;, BC98ar+e:; became oc0Gpar+e:; and so on. The old names s$#ll e/#s$ as al#ases, so PHP E scr#!$s do no$ need $o be changed. & $able sho*#ng old and ne* names a!!ears #n &!!end#/ . The name MO >8O #s also $he name 5or OracleLs all >n$er5ace &P> used b" !rograms such as $he PHP O >8 e/$ens#on. &ll un=ual#5#ed re5erences $o O >8 #n $h#s boo+ re5er $o $he PHP e/$ens#on.

PDO E&ten'ion
PHP Da$a Ob8ec$s :PDO; #s a da$a abs$rac$#on e/$ens#on $ha$ !ro'#des PHP 5unc$#ons 5or access#ng da$abases us#ng a common core o5 da$abase #nde!enden$ me$hods. @ach da$abase has #$s o*n dr#'er, *h#ch ma" also su!!or$ 'endor s!ec#5#c 5unc$#onal#$". PDOJO > !ro'#des $he Oracle 5unc$#onal#$" 5or PDO. The PDO e/$ens#on and PDOJO > dr#'er are o!en source and #ncluded #n PHP 5.1 on*ards. &n e/am!le scr#!$ $ha$ 5#nds c#$" names 5rom $he locations $able us#ng PDOJO > #s( Script !" connectpdo%php
!php ,d1h - new 8DB:#oc0:d1name-localho+t/IE#% #hr#% #hrpwd#;( ,+ - ,d1h-"prepare:.+elect c0ty from locat0on+.;( 12

PHP Orac&e 'xten#ion# ,+-"e<ecute:;( wh0le :,r - ,+-"fetch:8DB::*ETC$GASSBC;; @ echo ,r3#C9TJ#4 . . 1r".( A !"

The ou$!u$ #s $he same as $he O >8 e/am!le #n 2#gure P. The Da$a 3ource 7ame :D37; !re5#/ oc0: mus$ be lo*ercase. The 'alue o5 d1name #s $he Oracle connec$#on #den$#5#er 5or "our da$abase.

PHP Data)a'e A)'traction Li)rarie'


.#+e PDO, $he abs$rac$#on l#brar#es allo* s#m!le PHP a!!l#ca$#ons $o *or+ *#$h d#55eren$ brands o5 da$abase. There are $hree ma#n da$abase abs$rac$#on l#brar#es 5or PHP. The" are *r#$$en #n PHP and, *hen con5#gured 5or Oracle, $he" use 5unc$#onal#$" !ro'#ded b" $he O >8 e/$ens#on. The abs$rac$#on l#brar#es are(

&DOdb P@&R DP@&R MD-2

O$her abs$rac$#ons such as reole ha'e ded#ca$ed 5an?base, bu$ $he su!!or$ 5or Oracle 5ea$ures 'ar#es. Hou can 5reel" do*nload and use $he PHP code 5or $hese l#brar#es.

ADOd)
The !o!ular &DOdb l#brar" #s a'a#lable 5rom http://adod1.+ourceforge.net. There #s an o!$#onal e/$ens#on !lug?#n #5 "ou need e/$ra !er5ormance. &n e/am!le scr#!$ $ha$ 5#nds c#$" names 5rom $he locations $able us#ng &DOdb( Script 2" connectadodb%php
!php reFu0reGonce:.adod1.0nc.php.;( ,d1 - ADBLewConnect0on:.oc0M.;( ,d1-"Connect:.localho+t/IE.% .hr.% .hrpwd.;( ,+ - ,d1-"E<ecute:.+elect c0ty from locat0on+.;( wh0le :,r - ,+-"*etchRow:;; @ echo ,r3#C9TJ#4 . . 1r".( A !"

There #s an Advanced Oracle #utorial a$( http://phplen+.com/len+/adod1/doc+-oracle.htm

13

PHP Orac&e 'xten#ion#

PEAR D<
The PHP @/$ens#on and &!!l#ca$#on Re!os#$or" :P@&R; con$a#ns man" use5ul !ac+ages $ha$ e/$end PHPLs 5unc$#onal#$". P@&R D- #s a !ac+age 5or da$abase abs$rac$#on. >$ #s a'a#lable 5rom http://pear.php.net/pacNage/DB. P@&R D- has been su!erseded b" P@&R MD-2 bu$ #s s$#ll *#del" used.

PEAR 8D<
The P@&R MD-2 !ac+age #s a'a#lable 5rom http://pear.php.net/pacNage/ODB7. >$ #s a l#brar" a#m#ng $o comb#ne $he bes$ o5 P@&R D- and $he PHP Me$abase abs$rac$#on !ac+ages. &n e/am!le scr#!$ $ha$ 5#nds c#$" names 5rom $he locations $able us#ng MD-2( Script '" connectpear%php
!php reFu0re:.ODB7.php.;( ,md17 - ODB7::connect:#oc0M://hr:hrpwdP//localho+t/IE#;( ,re+ - ,md17-"Fuery:.+elect c0ty from locat0on+.;( wh0le :,row - ,re+-"fetchRow:ODB7G*ETC$OBDEGASSBC;; @ echo ,row3#c0ty#4 . . /1r".( A !"

!ettin" the OCI0 E&ten'ion


The O >8 e/$ens#on #s #ncluded #n 'ar#ous PHP bundles. There are $hree ma#n d#s$r#bu$#on channels( $he PHP releases, 9end ore 5or Oracle, and $he PHP @/$ens#on ommun#$" .#brar" :P@ .; s#$e *h#ch con$a#ns PHP e/$ens#ons as #nd#'#dual do*nloads. The O >8 e/$ens#on #s a'a#lable #n se'eral 5orms because o5 $he d#55er#ng needs o5 $he commun#$". Man" PHP users #ns$all $he 5ull PHP source and do $he#r o*n cus$om con5#gura$#on. >5 $he" need a s!ec#5#c bug 5#/ $he" use P@ . or PHPLs la$es$ de'elo!men$ source code $o ge$ #$. 0#ndo*s users commonl" #ns$all PHPGs !re?bu#l$ 0#ndo* b#nar#es. &$ $#me o5 *r#$#ng, $he s#$e http://pecl5w0n.php.net/ $ha$ *as use5ul 5or ob$a#n#ng 0#ndo*s PHP 5#/es #s no longer be#ng ma#n$a#ned. & ne* s#$e a$ http://w0ndow+.php.net/ #s under cons$ruc$#on. >5 "ou do no$ *an$ $o com!#le PHP, or $h#s #s "our 5#rs$ $#me *#$h PHP and Oracle, or "ou *an$ a su!!or$ed s$ac+, #ns$all 9end ore 5or Oracle. Table 1 sho*s *here O >8 can be do*nloaded.

#able !" O()* Availability%


<und%e Containin" OCI0 Location and Current Re%ea'e PHP 3ource ode http://www.php.net/download+.php php+,%2%-%tar%b.2 om!#les and runs on man" !la$5orms

14

Getting the O(I 'xten#ion

<und%e Containin" OCI0 Location and Current Re%ea'e PHP 0#ndo*s -#nar#es P@ . 3ource ode http://www.php.net/download+.php php+,%2%-+/in'2%.ip http://pecl.php.net/pacNage/oc0M oci*+!%'%0%tg. Used $o add or u!grade O >8 5or an e/#s$#ng PHP #ns$alla$#on 9end ore 5or Oracle 2.5 http://www.oracle.com/technology/tech/php/Qendcore/ 1end(ore orOracle+v2%,%2+3inux+x*4%tar%g. 1end(ore$orOracle+v%2%,%2+/indo&s+x*4%exe O$her !la$5orms are also a'a#lable

OCI0 and Orac%e In'ta%%ation Option'


To !ro'#de Oracle da$abase access, $he PHP b#nar" #s l#n+ed *#$h Oracle cl#en$ l#brar#es. These l#brar#es !ro'#de underl"#ng connec$#'#$" $o $he da$abase, *h#ch ma" be local or remo$e on "our ne$*or+.

igure 0" PHP lin5s &ith Oracle client libraries% Oracle has cross?'ers#on com!a$#b#l#$". 2or e/am!le, #5 PHP O >8 #s l#n+ed *#$h Oracle Da$abase 10g cl#en$ l#brar#es, $hen PHP a!!l#ca$#ons can connec$ $o Oracle Da$abase 8i , Di, 10g or 11g% >5 O >8 #s l#n+ed *#$h Oracle Da$abase 11g l#brar#es, $hen PHP can connec$ $o Oracle Da$abase DiR2 on*ards% >5 $he da$abase #s #ns$alled on $he same mach#ne as $he *eb ser'er and PHP, $hen PHP can be l#n+ed *#$h Oracle l#brar#es #ncluded #n $he da$abase so5$*are. >5 $he da$abase #s #ns$alled on ano$her mach#ne, $hen l#n+ PHP *#$h $he small, 5ree Oracle >ns$an$ l#en$ l#brar#es.

15

PHP Orac&e 'xten#ion#

2ull O >8 5unc$#onal#$" ma" no$ be a'a#lable unless $he Oracle cl#en$ l#brar#es and da$abase ser'er are $he la$es$ 'ers#on. Table 2 sho*s $he com!a$#b#l#$" o5 $he Oracle cl#en$ l#brar#es *#$h $he curren$ O >8 e/$ens#on and PHP. Older 'ers#ons o5 PHP ha'e d#55eren$ com!a$#b#l#$" re=u#remen$s.

#able 2" O()* and Oracle (ompatibility 6atrix%


So2t9are <und%e PHP Release 3ource ode PHP Release 0#ndo*s -#nar#es P@ . O >8 3ource ode 9end ore 5or Oracle 2.5 PHP @er'ion OCI0 @er'ion Inc%uded Orac%e C%ient Li)rarie' ,'a)%e 9ith OCI0 8i, Di, 10g, 11g 10g7 11g

urren$ release #s 5.2.C O >8 1.2.5 urren$ release #s 5.2.C O >8 1.2.5 -u#lds *#$h PHP E.P.D on*ards >ncludes PHP 5.2.5

.a$es$ release #s DiR2, 10g, 11g O >8 1.P.E O >8 1.2.P )nbuilt Oracle Database !2g client

>5 O >8 #s be#ng used *#$h PHP E, 5.0, 5.1 or 5.2, cons#der re!lac#ng $he de5aul$ O >8 code *#$h $he la$es$ 'ers#on 5rom P@ . $o ge$ #m!ro'ed s$ab#l#$", beha'#or and !er5ormance o!$#m#1a$#ons. Th#s #s #m!or$an$ 5or PHP E and 5.0 because $he#r 'ers#ons o5 O >8 are no$or#ousl" uns$able. >ns$ruc$#ons 5or u!da$#ng O >8 are sho*n la$er #n $h#s boo+.

!ettin" the PDO E&ten'ion


The PDOJO > dr#'er 5or PDO #s #ncluded *#$h PHP source code and, l#+e O >8, #s also a'a#lable on P@ .. The PHP commun#$" has le$ $he PDO !ro8ec$ langu#sh and Oracle recommends us#ng O >8 #ns$ead *hene'er !oss#ble because o5 #$s be$$er 5ea$ure se$, !er5ormance and rel#ab#l#$". Onl" a 5e* m#nor changes ha'e been made $o PDOJO > #n PHP releases s#nce #$s #n$roduc$#on. The 'ers#on o5 PDOJO > on P@ . has no$ been u!da$ed *#$h $hese 5#/es and #s s$#ll a$ 'ers#on 1.0. PDOJO > #s #nde!enden$ o5 O >8 and has no code #n common *#$h #$. The e/$ens#ons can be enabled se!ara$el" or a$ $he same $#me. Table P sho*s $he com!a$#b#l#$" o5 $he PDOJO > dr#'er *#$h PHP and Oracle.

#able '" PDO8O() Availability and (ompatibility%


<und%e Containin" PDOAOCI PHP 3ource ode Location and Current Re%ea'e http://www.php.net/download+.php php+,%2%-%tar%b.2 om!#les and runs on man" !la$5orms PHP 0#ndo*s -#nar#es http://www.php.net/download+.php php+,%2%-+/in'2%.ip
16

Orac%e C%ient Li)rarie' ,'a)%e 9ith PDOAOCI 8i, Di, 10g, 11g

8i, Di, 10g, 11g

Getting the P)O 'xten#ion

<und%e Containin" PDOAOCI P@ . 3ource ode

Location and Current Re%ea'e http://pecl.php.net/pacNage/8DBGBC9 PDO8O()+!%2%tg.

Orac%e C%ient Li)rarie' ,'a)%e 9ith PDOAOCI 8i, Di, 10g 9no !!g support:

9end ore 5or Oracle 2.5 http://www.oracle.com/technology/tech/ )nbuilt Oracle Database !2g php/Qendcore/ client 1end(ore orOracle+v2%,%2+3inux+x*4%tar%g. 1end(ore$orOracle+v%2%,%2+/indo&s+x*4%exe O$her !la$5orms are also a'a#lable

1end Core 2or Orac%e


9end ore 5or Oracle :9 O; #s a !re?bu#l$ release o5 PHP 5rom 9end $ha$ comes enabled *#$h $he O >8 and PDOJO > e/$ens#ons. >$ #s a'a#lable 5or se'eral !la$5orms, has a s#m!le #ns$aller, and #ncludes a con'en#en$ bro*ser?based managemen$ console 5or con5#gura$#on and ge$$#ng u!da$es. >$ comes *#$h Oracle >ns$an$ l#en$ 10g and #ncludes an o!$#onal &!ache *eb ser'er. 9 O can connec$ $o local and remo$e Oracle da$abases. &l$hough 9 O 2.5 #ncludes PHP 5.2.5, 9end dec#ded $o use a $es$ed O >8 e/$ens#on 5rom an older PHP release, so no$ all recen$ bug 5#/es are #ncluded. 9 O #s 5ree $o do*nload and use. & su!!or$ !ac+age #s a'a#lable 5rom 9end.

The PHP Re%ea'e C:c%e


PHPLs source code #s under con$#nual de'elo!men$ #n a source code con$rol s"s$em '#e*able a$ http://c/+.php.net/. :M#gra$#on 5rom ,3 $o 3,7 #s !lanned;. Th#s #s $he onl" !lace bug 5#/es are merged. The code #s o!en source and an"one can read $he code #n ,3 or see+ a!!ro'al $o con$r#bu$e. The code #n ,3 #s used $o crea$e $he 'ar#ous PHP d#s$r#bu$#ons(

T*o?hourl" sna!?sho$s are crea$ed con$a#n#ng a com!le$e se$ o5 all PHPLs source #n ,3 a$ $he $#me $he sna!sho$ *as crea$ed. Hou can u!da$e "our PHP en'#ronmen$ b" ge$$#ng $h#s source code and recom!#l#ng, or b" do*nload#ng $he 0#ndo*s b#nar#es. The sna!sho$s ma" be rela$#'el" uns$able because $he code #s #n 5lu/. The sna!sho$s are loca$ed a$ http://+nap+.php.net/. &5$er se'eral mon$hs o5 de'elo!men$ $he PHP release manager *#ll release a ne* s$able 'ers#on o5 PHP. >$ uses $he mos$ curren$ ,3 code a$ $he $#me o5 release. P@ . O >8 source code sna!sho$s are $a+en 5rom ,3 a$ #n5re=uen$ #n$er'als. Recen$l", sna!sho$s ha'e been made concurren$l" a$ $he $#me o5 a PHP release :*hen O >8 has changed;. 9end ore 5or Oracle also $a+es sna!sho$s o5 PHP 5rom ,3. ,ar#ous o!era$#ng s"s$ems bundle $he 'ers#on o5 PHP curren$ a$ $he $#me $he O3 #s released and !ro'#de cr#$#cal !a$ch u!da$es.
17

PHP Orac&e 'xten#ion#

The schedules o5 PHP releases, $he P@ . source sna!sho$s, and 9end ore 5or Oracle are no$ 5ull" s"nchron#1ed. &s a resul$ o5 a recen$ PHP 'ers#on number#ng o'erhaul, $he O >8 e/$ens#on #ncluded #n $he PHP source code #s no* labeled *#$h $he same 'ers#on number as $he e=u#'alen$ P@ . release. Table E sho*s $he ma8or 5ea$ures #n each re'#s#on o5 P@ . O >8.

#able 0" 6a;or Revisions o$ O()*%


PECL OCI0 @er'ion O >8 1.0 O >8 1.1 O >8 1.2 8ain =eature' 2#rs$ P@ . release. -ased on PHP E.P O >8 code. -e$a releases $ha$ became O >8 1.2. & ma8or re5ac$or#ng o5 $he e/$ens#on 5or PHP 5.1. >$ grea$l" #m!ro'ed s$ab#l#$", added con$rol o'er !ers#s$en$ connec$#ons, and #n$roduced !er5ormance 5ea$ures such as $he ab#l#$" $o do s$a$emen$ cach#ng and a ne* arra" b#nd 5unc$#on. &'a#lable #n PHP 5.1 Q 5.2. Re5ac$ored connec$#on managemen$ g#'es be$$er handl#ng o5 res$ar$ed da$abases and adds su!!or$ 5or OracleGs e/$ernal au$hen$#ca$#on. &lso some o5 Oracle recen$ scalab#l#$" and h#gh a'a#lab#l#$" 5ea$ures can be no* be used. These 5ea$ures are d#scussed #n a la$er cha!$er. O >8 1.P *#ll be #ncluded #n PHP 5.P.

O >8 1.P

CHAPTER (

INSTALLIN! ORACLE DATA<ASE 1*G E7PRESS EDITION


Th#s ha!$er con$a#ns an o'er'#e* o5, and #ns$alla$#on #ns$ruc$#ons 5or, Oracle Da$abase 10g @/!ress @d#$#on :Oracle Da$abase N@;. The #ns$alla$#on #ns$ruc$#ons are g#'en 5or .#nu/, 0#ndo*s, Deb#an, Ubun$u and 6ubun$u.

Orac%e Data)a'e Edition'


There are a number o5 ed#$#ons o5 $he Oracle da$abase, each *#$h d#55eren$ 5ea$ures, l#cens#ng o!$#ons and cos$s. The ed#$#ons are(

@/!ress @d#$#on 3$andard @d#$#on One 3$andard @d#$#on @n$er!r#se @d#$#on

&ll $he ed#$#ons are bu#l$ us#ng $he same code base. Tha$ #s, $he" all ha'e $he same source code, bu$ d#55eren$ 5ea$ures are #m!lemen$ed #n each ed#$#on. @n$er!r#se @d#$#on has all $he bells and *h#s$les, *hereas $he @/!ress @d#$#on has a l#m#$ed 5ea$ure se$, bu$ s$#ll has all $he rel#ab#l#$" and !er5ormance o5 $he @n$er!r#se @d#$#on. Hou could s$ar$ o55 *#$h $he @/!ress @d#$#on, and, as needed, mo'e u! $o ano$her ed#$#on as "our scalab#l#$" and su!!or$ re=u#remen$s change. Hou could do $h#s *#$hou$ chang#ng an" o5 "our underl"#ng $able s$ruc$ure or code. )us$ change $he Oracle so5$*are and "ouLre a*a". There #s a com!rehens#'e l#s$ o5 $he 5ea$ures 5or each Oracle ed#$#on a$ http://www.oracle.com/data1a+e/productGed0t0on+.html. Th#s boo+ d#scusses *or+#ng *#$h Oracle Da$abase N@. Th#s #s $he 5ree ed#$#on. 2ree $o do*nload. 2ree $o de'elo! aga#ns$. 2ree $o d#s$r#bu$e *#$h "our a!!l#ca$#ons. Hes, $ha$ #s 5ree, 5ree, 5reeR

Orac%e Data)a'e 7E
Oracle Da$abase N@ #s a'a#lable on P2?b#$ 0#ndo*s and .#nu/ !la$5orms. Oracle Da$abase N@ #s a good cho#ce 5or de'elo!men$ o5 PHP a!!l#ca$#ons $ha$ re=u#re a 5ree, small 5oo$!r#n$ da$abase. Oracle Da$abase N@ #s a'a#lable on $he Oracle Technolog" 7e$*or+ :http://otn.oracle.com/<e; 5or $he 5ollo*#ng o!era$#ng s"s$ems(

0#ndo*s 2000 3er'#ce Pac+ E or la$er 0#ndo*s 3er'er 200P

1$

In#ta&&ing Orac&e )ata*a#e 1%g 'x"re## 'dition


0#ndo*s NP Pro5ess#onal 3er'#ce Pac+ 1 or la$er Oracle @n$er!r#se .#nu/ E and 5 Red Ha$ @n$er!r#se .#nu/ RH@. P, E, and 5 3use 3.@3?D 2edora ore E Red 2lag D 3er'er 5.0IM>R& .@ .>7UN ,E.0IHaanso5$ .#nu/ 200F 3er'er :&s#anu/ 2.0 >ns#de; Deb#an P.1

The 5ollo*#ng l#brar#es are re=u#red 5or $he .#nu/?based o!era$#ng s"s$ems(

glibc release 2.P.2 libaio release 0.P.DF

There are some l#m#$a$#ons *#$h Oracle Da$abase N@(


EA- o5 da$a 3#ngle da$abase #ns$ance 3#ngle PU used, e'en #5 mul$#!le PUs e/#s$ 1A- R&M used, e'en #5 more R&M #s #ns$alled

Oracle Da$abase N@ has a bro*ser?based managemen$ #n$er5ace, Oracle &!!l#ca$#on @/!ress. 3u!!or$ 5or Oracle Da$abase N@ #s $hrough an Oracle Technolog" 7e$*or+ :http://otn.oracle.com/; d#scuss#on 5orum, *h#ch #s !o!ula$ed b" !eers and !roduc$ e/!er$s. Hou canno$ bu" su!!or$ 5rom Oracle 5or Oracle Da$abase N@. >5 "ou need a 5ull" su!!or$ed 'ers#on 5or $he Oracle da$abase, "ou should cons#der Oracle 3$andard @d#$#on or @n$er!r#se @d#$#on. Hou can do*nload all $he ed#$#ons o5 $he Oracle Da$abase 5rom $he Oracle Technolog" 7e$*or+, and use $hese 5or a!!l#ca$#on de'elo!men$ and $es$#ng, bu$ *hen "ou go !roduc$#on, "ou *#ll need $o !a" Oracle 5or $he l#cense cos$s.

In'ta%%in" Orac%e Data)a'e 7E on Linu&


>5 "ou do no$ ha'e a 'ers#on o5 libaio o'er release 0.P.DF, "ou need $o #ns$all $h#s l#brar" be5ore "ou can #ns$all Oracle Da$abase N@. To #ns$all Oracle Da$abase N@( 1. 2. Do*nload $he Oracle Da$abase N@ 5rom http://otn.oracle.com/<e. .og #n or su as root(
R +u 8a++word:

P.

>ns$all $he RPM(


R rpm -0/h oracle-<e-un0/-2?.7.?.2-2.?.0)MS.rpm

2%

In#ta&&ing Orac&e )ata*a#e +' on ,inux

Oracle Da$abase N@ #ns$alls. E. on5#gure $he da$abase


R /etc/0n0t.d/oracle-<e conf0gure

5. F. C.

&cce!$ $he de5aul$ !or$s o5 8080 5or &!!l#ca$#on @/!ress, and 1521 5or $he Da$abase .#s$ener. @n$er and con5#rm $he !ass*ord 5or $he de5aul$ users. @n$er Y or N 5or *he$her "ou *an$ $he da$abase $o s$ar$ au$oma$#call" on reboo$. The da$abase and da$abase l#s$ener are con5#gured and s$ar$ed.

>5 "ou use $he Oracle Unbrea+able .#nu/ 7e$*or+ and ha'e $he Oracle 3o5$*are channel enabled, "ou can #ns$all Oracle Da$abaseN@ *#$h(
R up7date oracle-<e

&5$er $h#s do*nload com!le$es, 5ollo* $he con5#gura$#on s$e! E on*ards.

In'ta%%in" Orac%e Data)a'e 7E on De)ianB ,)untuB and Cu)untu


There #s an &d'anced Pac+age Tool :apt+get; re!os#$or" a'a#lable on $he Oracle O!en 3ource *eb s#$e 5or Oracle Da$abase N@. To #nclude $h#s re!os#$or", add $he 5ollo*#ng $o $he 5#le <etc<apt<sources%list(
de1 http://o++.oracle.com/de10an un+ta1le ma0n non-free

libaio and bc are #ncluded #n $he re!os#$or", and *#ll be #ns$alled 5rom $he re!os#$or" #5 "ou do no$ alread" ha'e $hem #ns$alled. >5 "ou do*nload Oracle Da$abase N@ 5rom $he Oracle Technolog" 7e$*or+ :http://otn.oracle.com/<e;, "ou need $o ma+e sure $ha$ "ou ha'e alread" #ns$alled $he libaio and bc !ac+ages. >5 "ou are us#ng Ubun$u or 6ubun$u, $he bc !ac+age #s #ns$alled b" de5aul$ on $he des+$o! 'ers#on, bu$ no$ on $he ser'er 'ers#on. To #ns$all Oracle Da$abase N@ on Deb#an, Ubun$u and 6ubun$u, 5ollo* $hese s$e!s( 1. .og #n or su as root
R +u 8a++word:

2.

>ns$all Oracle Da$abase N@


R apt-get update R apt-get 0n+tall oracle-<e

>5 "ou ha'e no$ added $he apt+get re!os#$or", "ou can do*nload Oracle Da$abase N@ 5rom http://otn.oracle.com/<e, and run $he 5ollo*#ng command $o beg#n $he #ns$all(
R dpNg -0 download+/oracle-<e-un0/er+alG2?.7.?.2-2.?G0)MS.de1

Oracle Da$abase N@ #ns$alls. P. on5#gure $he da$abase


21

In#ta&&ing Orac&e )ata*a#e 1%g 'x"re## 'dition R /etc/0n0t.d/oracle-<e conf0gure

E. 5. F.

&cce!$ $he de5aul$ !or$s o5 8080 5or &!!l#ca$#on @/!ress, and 1521 5or $he Da$abase .#s$ener. @n$er and con5#rm $he !ass*ord 5or $he de5aul$ users. @n$er Y or N 5or *he$her "ou *an$ $he da$abase $o s$ar$ au$oma$#call" on reboo$. The da$abase and da$abase l#s$ener are con5#gured and s$ar$ed.

In'ta%%in" Orac%e Data)a'e 7E on Windo9'


To #ns$all Oracle Da$abase N@ on 0#ndo*s, 5ollo* $hese s$e!s( 1. 2. P. E. 5. .og on $o 0#ndo*s as a user *#$h &dm#n#s$ra$#'e !r#'#leges. >5 an BRACKEG$BOE en'#ronmen$ 'ar#able has been se$, dele$e #$ us#ng $he Control Panel > Sy te! d#alog. Do*nload $he Oracle Da$abase N@ 5rom http://otn.oracle.com/<e. Double cl#c+ on $he Oracle=E>niv%exe 5#le. >n $he Oracle Da$abase N@ ? >ns$all 0#1ard *elcome *#ndo*, cl#c+ Ne"t.

igure ," Oracle Database =E install &elcome dialog% F. C. >n $he .#cense &greemen$ *#ndo*, selec$ I a##e$t and cl#c+ Ne"t. >n $he hoose Des$#na$#on .oca$#on *#ndo*, e#$her acce!$ $he de5aul$ or cl#c+ -ro*se $o selec$ a d#55eren$ #ns$alla$#on d#rec$or". l#c+ Ne"t.

22

In#ta&&ing Orac&e )ata*a#e +' on Window#

igure 4" Oracle Database =E install location dialog% 8. D. Oracle Da$abase N@ re=u#res a number o5 !or$s and selec$s a number o5 de5aul$ !or$s. >5 $hese !or$s are alread" be#ng used, "ou are !rom!$ed $o en$er ano$her !or$ number. >n $he 3!ec#5" Da$abase Pass*ords *#ndo*, en$er and con5#rm $he !ass*ord $o use 5or $he sys and system da$abase accoun$s. l#c+ Ne"t.

igure -" Oracle Database =E database pass&ord dialog% 10. >n $he 3ummar" *#ndo*, re'#e* $he #ns$alla$#on se$$#ngs. l#c+ In tall.

23

In#ta&&ing Orac&e )ata*a#e 1%g 'x"re## 'dition

igure *" Oracle Database =E install summary dialog% 11. >n $he >ns$all3h#eld 0#1ard om!le$e *#ndo*, cl#c+ %a&n#' t'e (ata)a e 'o!e$a*e $o d#s!la" $he Da$abase Home Page. l#c+ +,n, '.

Te'tin" the Orac%e Data)a'e 7E In'ta%%ation


To $es$ $he #ns$alla$#on o5 Oracle Da$abase N@( 1. >5 "ou do no$ alread" ha'e $he Da$abase home!age d#s!la"ed #n "our *eb bro*ser, o!en a *eb bro*ser and en$er(
http://27T.?.?.2:M?M?/ape<

2.

The Da$abase home!age #s d#s!la"ed.

igure ?" Oracle Database =E home page login screen%

24

-e#ting the Orac&e )ata*a#e +' In#ta&&ation

P.

.og #n as user system *#$h $he !ass*ord "ou en$ered dur#ng $he #ns$alla$#on. Hou should no* be logged #n$o $he Oracle Da$abase home!age.

igure !2" Oracle Database =E home page%

Con2i"urin" Orac%e Data)a'e 7E


There are a number o5 en'#ronmen$ se$$#ngs and con5#gura$#on o!$#ons "ou can se$ 5or Oracle Da$abase N@. The more commonl" used se$$#ngs are d#scussed here.

Settin" the Orac%e Data)a'e 7E En.iron4ent @aria)%e' on Linu&


On .#nu/ !la$5orms a scr#!$ #s !ro'#ded $o se$ $he Oracle en'#ronmen$ 'ar#ables a5$er "ou log #n. The scr#!$ 5or -ourne, -ash and 6orn shells(
/u+r/l01/oracle/<e/app/oracle/product/2?.7.?/+er/er/10n/oracleGen/.+h

2or and $csh shells, use oracle8env%csh. Run $he a!!ro!r#a$e scr#!$ 5or "our shell $o se$ "our Oracle Da$abase N@ en'#ronmen$ 'ar#ables. Hou can also add $h#s scr#!$ $o "our log#n !ro5#le $o ha'e $he en'#ronmen$ 'ar#ables se$ u! au$oma$#call" *hen "ou log #n. To add $he scr#!$ $o "our -ourne, -ash or 6orn shell, add $he 5ollo*#ng l#nes $o "our %bash8pro$ile or %bashrc 5#le(
. /u+r/l01/oracle/<e/app/oracle/product/2?.7.?/+er/er/10n/oracleGen/.+h

:7o$e $he s!ace a5$er $he !er#od;. To add $he scr#!$ $o "our log#n !ro5#le 5or 5ollo*#ng l#nes $o "our %login or %cshrc 5#le(

and $csh shells, add $he

+ource /u+r/l01/oracle/<e/app/oracle/product/2?.7.?/+er/er/10n/oracleGen/.c+h

25

In#ta&&ing Orac&e )ata*a#e 1%g 'x"re## 'dition

Ena)%in" Data)a'e Startup and Shutdo9n 2ro4 8enu' on Linu&


Hou ma" no$ be able $o s$ar$ and s$o! $he da$abase us#ng $he menu on .#nu/ !la$5orms. Th#s #s because "our user #s no$ a member o5 $he o!era$#ng s"s$em dba grou!. To enable $h#s 5unc$#onal#$", add $he user name $o $he dba grou! us#ng $he 3"s$em 3e$$#ngs.

Startin" and Stoppin" the Li'tener and Data)a'e


The da$abase l#s$ener #s an Oracle 7e$ !rogram $ha$ l#s$ens 5or and res!onds $o re=ues$s $o $he da$abase. The da$abase l#s$ener mus$ be runn#ng $o handle $hese re=ues$s. The da$abase #s ano$her !rocess $ha$ runs #n memor", and needs $o be s$ar$ed be5ore Oracle 7e$ can handle connec$#on re=ues$s $o #$. &5$er #ns$all#ng Oracle Da$abase N@, $he l#s$ener and da$abase should alread" be runn#ng, and "ou ma" ha'e re=ues$ed dur#ng $he #ns$alla$#on $ha$ $he l#s$ener and da$abase should be s$ar$ed *hen $he o!era$#ng s"s$em s$ar$s u!. >5 "ou need $o manuall" s$ar$ or s$o! $he da$abase l#s$ener, $he o!$#ons and commands 5or $h#s are l#s$ed belo*. To s$ar$ $he da$abase, "ou mus$ log #n as a user *ho #s a member o5 $he o!era$#ng s"s$em dba user grou!. Th#s a!!l#es $o all $he me$hods o5 s$ar$#ng and s$o!!#ng $he da$abase.

Starting and Stopping the Listener and Database on Linux


To s$ar$ u! $he l#s$ener and da$abase on .#nu/ !la$5orms us#ng $he des+$o!, do one o5 $he 5ollo*#ng(

On .#nu/ *#$h Anome( 3elec$ -$$l,#at,on > Ora#le (ata)a e 10* E"$re On .#nu/ *#$h 6D@( 3elec$ / Men& > Ora#le (ata)a e 10* E"$re

E.,t,on > Start (ata)a e.

E.,t,on > Start (ata)a e.

To shu$ do*n $he da$abase on .#nu/ !la$5orms us#ng $he des+$o!, do one o5 $he 5ollo*#ng(

On .#nu/ *#$h Anome( 3elec$ -$$l,#at,on > Ora#le (ata)a e 10* E"$re On .#nu/ *#$h 6D@( 3elec$ / Men& > Ora#le (ata)a e 10* E"$re

E.,t,on > Sto$ (ata)a e.

E.,t,on > Sto$ (ata)a e.

To s$ar$ $he l#s$ener and da$abase on .#nu/ !la$5orms us#ng $he command l#ne, run $he 5ollo*#ng command #n "our shell(
R /etc/0n0t.d/oracle-<e re+tart

To s$o! $he l#s$ener and da$abase on .#nu/ !la$5orms us#ng $he command l#ne, run $he 5ollo*#ng command #n "our shell(
R /etc/0n0t.d/oracle-<e +top

Hou can also use $he 3er'#ces d#alog 5rom $he Des+$o! $o s$ar$ and s$o! $he l#s$ener and da$abase. To s$ar$ $he l#s$ener and da$abase 5rom $he Des+$o! 3er'#ces d#alog, selec$ -$$l,#at,on S Sy te! Sett,n* S Ser0er Sett,n* S Ser0,#e . 3elec$ ora#le1"e 5rom $he l#s$ o5 ser'#ces and selec$ Start. To s$o! $he l#s$ener and da$abase 5rom $he Des+$o! 3er'#ces d#alog, selec$ -$$l,#at,on S Sy te! Sett,n* S Ser0er Sett,n* S Ser0,#e . 3elec$ ora#le1"e 5rom $he l#s$ o5 ser'#ces and selec$ Sto$.

26

(on.iguring Orac&e )ata*a#e +'

Starting and Stopping the Listener and Database on Windows


To s$ar$ $he l#s$ener and da$abase on 0#ndo*s !la$5orms, selec$ Start S Ora#le (ata)a e 10* E"$re E.,t,on S Start (ata)a e. & 0#ndo* #s d#s!la"ed sho*#ng $he s$a$us o5 $he l#s$ener and da$abase s$ar$u! !rocess.

igure !!" Start Database dialog% T"!e e",t and !ress @n$er $o close $he 0#ndo*. The l#s$ener and da$abase are no* s$ar$ed. To s$o! $he l#s$ener and da$abase on 0#ndo*s !la$5orms, selec$ Start S Ora#le (ata)a e 10* E"$re E.,t,on S Sto$ (ata)a e. & 0#ndo* #s d#s!la"ed sho*#ng $he s$a$us o5 $he l#s$ener and da$abase shu$do*n !rocess.

igure !2" Stop Database dialog% T"!e e",t and !ress @n$er $o close $he 0#ndo*. The l#s$ener and da$abase are no* s$o!!ed. Hou can also s$ar$ and s$o! $he l#s$ener se!ara$el" on 0#ndo*s !la$5orms us#ng $he 3er'#ces d#alog. To s$ar$ $he l#s$ener on 0#ndo*s !la$5orms, o!en $he 3er'#ces d#alog us#ng Start > Sett,n* > Control Panel > -.!,n, trat,0e Tool > Ser0,#e , and selec$ $he Ora#le2ETNS%, tener ser'#ce. R#gh$ cl#c+ on $he %, tener ser'#ce, and selec$ Start.

27

In#ta&&ing Orac&e )ata*a#e 1%g 'x"re## 'dition

To s$o! $he l#s$ener on 0#ndo*s !la$5orms, o!en $he 3er'#ces d#alog us#ng Start > Sett,n* > Control Panel > -.!,n, trat,0e Tool > Ser0,#e , and selec$ $he Ora#le2ETNS%, tener ser'#ce. R#gh$ cl#c+ on $he %, tener ser'#ce, and selec$ Sto$. Hou can also s$ar$ and s$o! $he da$abase se!ara$el" on 0#ndo*s !la$5orms us#ng $he 3er'#ces d#alog. To s$ar$ $he da$abase us#ng $he 3er'#ces d#alog on 0#ndo*s !la$5orms, o!en $he 3er'#ces d#alog us#ng Start > Sett,n* > Control Panel > -.!,n, trat,0e Tool > Ser0,#e , and selec$ $he Ora#leSer0,#e2E ser'#ce. R#gh$ cl#c+ on $he da$abase ser'#ce, and selec$ Start. To s$o! $he da$abase us#ng $he 3er'#ces d#alog on 0#ndo*s !la$5orms, o!en $he 3er'#ces d#alog us#ng Start > Sett,n* > Control Panel > -.!,n, trat,0e Tool > Ser0,#e , and selec$ $he Ora#leSer0,#e2E ser'#ce. R#gh$ cl#c+ on $he da$abase ser'#ce, and selec$ Sto$.

Starting and Stopping the Listener and Database Using SQL*Plus


Hou can also use $he command l#ne shell and 3K.TPlus command l#ne $o s$ar$ and s$o! $he da$abase. Ma+e sure $ha$ "ou are logged #n as a !r#'#leged user and ha'e "our o!era$#ng s"s$em en'#ronmen$ se$ u! correc$l" as d#scussed #n an earl#er sec$#on o5 $h#s cha!$er. On 0#ndo*s, $o con$rol $he l#s$ener, use $he 3er'#ces d#alog as d#scussed abo'e. To s$ar$ u! $he l#s$ener on .#nu/, o!en a $erm#nal *#ndo* and run $he 5ollo*#ng command(
R l+nrctl +tart

Oracle 7e$ s$ar$s $he l#s$ener and #$ #s read" $o $a+e da$abase re=ues$s. >5 "ou *an$ $o shu$ do*n $he l#s$ener manuall", "ou use $he s#m#lar command 5rom $he o!era$#ng s"s$em command !rom!$(
R l+nrctl +top

&5$er s$ar$#ng $he l#s$ener, "ou also need $o s$ar$ $he da$abase us#ng 3K.TPlus. 2or $h#s, "ou mus$ log #n as a da$abase user *#$h $he sysdba role. Th#s #s $he sys user #n de5aul$ #ns$alla$#ons, or "ou can use o!era$#ng s"s$em au$hen$#ca$#on #5 "ou are on $he local mach#ne #n $he o!era$#ng s"s$em dba grou!. To s$ar$ u! a da$abase us#ng 3K.TPlus, en$er $he 5ollo*#ng a$ $he command l#ne !rom!$(
R +Flplu+ /nolog

The 3K.TPlus command l#ne s$ar$s. Hou can also s$ar$ 3K.TPlus 5rom $he -$$l,#at,on S Ora#le (ata)a e 10* E"$re E.,t,on S R&n S3% Co!!an. %,ne on .#nu/, or Start S Pro*ra! S Ora#le (ata)a e 10* E"$re E.,t,on S R&n S3% Co!!an. %,ne on 0#ndo*s. &$ $he 3K.TPlus command l#ne !rom!$, en$er $he 5ollo*#ng commands $o connec$ $o $he da$abase and s$ar$ #$ u!(
SUK" connect / as sysdba SUK" startup

The da$abase #s s$ar$ed. >5 "ou s$ar$ $he da$abase be5ore s$ar$#ng $he Oracle 7e$ l#s$ener, #$ can $a+e a shor$ *h#le be5ore $he da$abase reg#s$ers *#$h $he l#s$ener. Un$#l #$ $h#s ha!!ens, connec$#ons $o $he da$abase *#ll 5a#l. To shu$ do*n $he da$abase, "ou need $o log #n as sysdba, and #ssue $he S$UTDB&L 9OOED9ATE command. .og #n$o 3K.TPlus as be5ore and #ssue $he 5ollo*#ng command(

(on.iguring Orac&e )ata*a#e +' SUK" connect / as sysdba SUK" shutdown immediate

The 3K.TPlus UserLs Au#de and Re5erence g#'es "ou $he 5ull s"n$a/ 5or s$ar$#ng u! and shu$$#ng do*n $he da$abase #5 "ou need more hel!.

Ena)%in" Re4ote C%ient Connection


The Oracle Da$abase N@ home !age #s onl" a'a#lable 5rom $he local mach#ne, no$ remo$el". >5 "ou *an$ $o enable access 5rom a remo$e cl#en$, "ou should be a*are $ha$ HTTP3 canno$ be used :onl" HTTP;, so "our log#n creden$#als are sen$ #n clear $e/$, and are no$ encr"!$ed, so #5 "ou donL$ need $o se$ $h#s u!, #$ #s more secure $o lea'e #$ as $he de5aul$ se$u!. To enable connec$#on $o $he Oracle Da$abase N@ home !age 5rom remo$e mach#nes, 5ollo* $hese s$e!s( 1. 2. P. E. 5. O!en a *eb bro*ser and load $he Oracle Da$abase N@ home !age(
http://27T.?.?.2:M?M?/ape<

.og #n as $he system user. 3elec$ -.!,n, trat,on 5rom $he home !age. 3elec$ Mana*e HTTP -##e 5rom $he Ta 4 o!$#on. hec+ $he -0a,la)le 5ro! lo#al er0er an. re!ote #l,ent rad#o bu$$on. l#c+ -$$ly C'an*e .

Hou can also use 3K.TPlus command l#ne $o enable access 5rom remo$e cl#en$s. To use 3K.TPlus command l#ne $o change $h#s se$$#ng, log #n$o 3K.TPlus as system, and run $he 5ollo*#ng command(
SUK" EXEC DBMS_XDB.SETLISTE E!L"C#L#CCESS$%#LSE&'

2$

In#ta&&ing Orac&e )ata*a#e 1%g 'x"re## 'dition

3%

CHAPTER #

,SIN! ORACLE DATA<ASE


Th#s ha!$er con$a#ns an o'er'#e* o5 $he Oracle &!!l#ca$#on @/!ress, 3K.TPlus and Oracle 3K. De'elo!er a!!l#ca$#ons $ha$ "ou can use $o !er5orm da$abase de'elo!men$ and adm#n#s$ra$#on.

Orac%e App%ication E&pre''


Oracle &!!l#ca$#on @/!ress #s a bro*ser?based a!!l#ca$#on bu#lder 5or $he Oracle da$abase. >$ #s #ns$alled *#$h Oracle Da$abase N@ and #s also a'a#lable 5or do*nload 5rom Oracle Technolog" 7e$*or+ :http://otn.oracle.com; as a s$andalone !roduc$ 5or o$her 'ers#ons and ed#$#ons o5 $he da$abase. >$ also con$a#ns a da$abase de'elo!men$ $ool, no$ co'ered #n $h#s boo+. The release o5 Oracle &!!l#ca$#on @/!ress #ns$alled *#$h Oracle Da$abase N@ has an add#$#onal module 5or !er5orm#ng da$abase adm#n#s$ra$#on, mon#$or#ng and ma#n$enance.

Lo""in" In To Orac%e App%ication E&pre''


To s$ar$ and log #n $o Oracle &!!l#ca$#on @/!ress( 1. 2. O!en a *eb bro*ser and en$er $he UR.(
http://27T.?.?.2:M?M?/ape<

The Oracle &!!l#ca$#on @/!ress log#n screen #s d#s!la"ed.

igure !'" Oracle Application Express login screen% P. .og #n as system. The !ass*ord "ou en$er here #s $he !ass*ord "ou en$ered *hen "ou #ns$alled Oracle. There #s no de5aul$ system !ass*ord se$ b" $he Oracle >ns$aller.

31

/#ing Orac&e )ata*a#e

igure !0" Oracle Application Express inter$ace%

,n%oc>in" the HR ,'er


Man" o5 $he e/am!les #n $h#s boo+, and o$her Oracle boo+s use $he hr user, *h#ch #s au$oma$#call" #ns$alled and con5#gured dur#ng an Oracle da$abase #ns$alla$#on. Hou ma" *an$ $o unloc+ $h#s user no* so $ha$ man" o5 $he e/am!les #n $h#s boo+ *or+ correc$l". To unloc+ $he hr user( 1. 2. .og #n $o Oracle &!!l#ca$#on @/!ress as sho*n #n $he !re'#ous sec$#on. 3elec$ -.!,n, trat,on S (ata)a e U er S Mana*e U er .

igure !," 6anage Database >sers menu%

32

Orac&e 0""&ication 'x"re##

P.

3elec$ $he HR #con.

igure !4" 6anage Database >sers screen% E. To unloc+ $he hr user, en$er a !ass*ord #n $he Pa 6or. and Con5,r! Pa 6or. 5#elds. The e/am!les #n la$er cha!$ers use $he !ass*ord hrp&d. hange &ccoun$ 3$a$us $o Unlo#4e., and cl#c+ $he -lter U er bu$$on.

igure !-" >ser screen% The hr accoun$ #s no* unloc+ed.

Creatin" Data)a'e O)6ect'


The Oracle &!!l#ca$#on @/!ress Ob8ec$ -ro*ser can be used $o crea$e or ed#$ $he 5ollo*#ng ob8ec$s(

Table ,#e* >nde/


33

/#ing Orac&e )ata*a#e


3e=uence ollec$#on T"!e Pac+age Procedure 2unc$#on Tr#gger Da$abase .#n+ Ma$er#al#1ed ,#e* 3"non"m

Oracle &!!l#ca$#on @/!ress uses *#1ards $o gu#de "ou $hrough crea$#ng $hese da$abase ob8ec$s. The 5ollo*#ng e/am!le co'ers crea$#ng a $able, bu$ "ou *#ll see $ha$ $he #n$er5ace #s *#1ard?based and crea$#ng and ed#$#ng d#55eren$ ob8ec$s can all be !er5ormed $hrough $he Ob8ec$ -ro*ser. To crea$e a ne* $able( 1. On $he Da$abase home !age, cl#c+ $he O)7e#t 8ro6 er #con. The Ob8ec$ -ro*ser !age #s d#s!la"ed.

igure !*" Oracle Application Express create ob;ect screen%

34

Orac&e 0""&ication 'x"re##

2.

l#c+ Create.

igure !?" Oracle Application Express create table ob;ect screen% P. 2rom $he l#s$ o5 ob8ec$ $"!es, selec$ Ta)le.

igure 22" Oracle Application Express table de$inition screen% E. 5. F. @n$er a $able name #n $he Table 7ame 5#eld, and de$a#ls 5or each column. l#c+ Ne"t. De5#ne $he !r#mar" +e" 5or $h#s $able :o!$#onal;. &dd 5ore#gn +e"s :o!$#onal;.

35

/#ing Orac&e )ata*a#e

C. 8.

&dd a cons$ra#n$ :o!$#onal;. l#c+ +,n, '. To '#e* $he 3K. used $o crea$e $he $able, cl#c+ S3% Synta".

igure 2!" Oracle Application Express con$irm create table screen% The 3K. $ha$ #s genera$ed $o crea$e $he $able #s(
CREATE ta1le .OJTABKE. : .*9RSTGLAOE. C$AR:2??; LBT LUKK% .SECBLDGLAOE. C$AR:2??; LBT LUKK% .SAKARJ. LUOBER% .B9RT$DATE. DATE ; /

Hou could also run $h#s command #n 3K.TPlus command l#ne $o crea$e $he $able. D. l#c+ Create. The $able #s crea$ed and a descr#!$#on o5 $he $able #s d#s!la"ed.

igure 22" Oracle Application Express table created con$irmation screen%

36

Orac&e 0""&ication 'x"re##

Wor>in" 9ith S3L Script'


The 3K. onsole enables "ou $o(

0r#$e 3K. and P.I3K. .oad and sa'e 3K. scr#!$s Ara!h#call" bu#ld 3K.

The 5ollo*#ng e/am!le gu#des "ou $hrough crea$#ng a 3K. scr#!$. Th#s e/am!le uses $he Kuer" -u#lder $o gra!h#call" crea$e a 3K. scr#!$ $o =uer" da$a. To access $he $ables used, log #n as $he hr user. To access Kuer" -u#lder( 1. 2. On $he (ata)a e home !age, cl#c+ $he S3% #con. l#c+ $he 3&ery 8&,l.er #con.

igure 2'" Oracle Application Express S@3 options screen% P. 3elec$ ob8ec$s 5rom $he Ob8ec$ 3elec$#on !ane. 0hen "ou cl#c+ on $he ob8ec$ name, #$ #s d#s!la"ed #n $he Des#gn !ane.

37

/#ing Orac&e )ata*a#e

igure 20" Oracle Application Express S@3 Auery builder screen% E. 5. F. C. 3elec$ columns 5rom $he ob8ec$s #n $he Des#gn !ane $o selec$ *h#ch columns $o #nclude #n $he =uer" resul$s. @s$abl#sh rela$#onsh#!s be$*een ob8ec$s b" cl#c+#ng on $he r#gh$?hand column o5 each $able :o!$#onal;. rea$e =uer" cond#$#ons :o!$#onal;. l#c+ R&n $o e/ecu$e $he =uer" and '#e* $he resul$s.

igure 2," Oracle Application Express S@3 Auery results screen% 8. Hou can sa'e "our =uer" us#ng $he Sa0e bu$$on.

Creatin" a PL5S3L Procedure


To en$er and run P.I3K. code #n $he 3K. ommands !age( 1. 2.
3

On $he Da$abase home !age, cl#c+ $he S3% #con $o d#s!la" $he 3K. !age. l#c+ $he S3% Co!!an. #con $o d#s!la" $he 3K. ommands !age.

Orac&e 0""&ication 'x"re##

P.

On $he 3K. ommands !age, en$er some P.I3K. code. Here #s some P.I3K. code $o $r" #5 "ou arenL$ 5am#l#ar *#$h P.I3K.. Th#s !rocedure, empG+tat, a'erages $he salar" 5or de!ar$men$s #n $he hr schema, and #s enca!sula$ed #n a P.I3K. !ac+age called empG+al. Hou need $o en$er each bloc+ se!ara$el", and cl#c+ R&n.
create or replace pacNage empG+al a+ procedure empG+tat( end empG+al( /

igure 24" Oracle Application Express P3<S@3 and S@3 command screen% l#c+ R&n $o e/ecu$e $he P.I3K.. Then en$er $he 5ollo*#ng code(
create or replace pacNage 1ody empG+al a+ 8rocedure empG+tat 0+ TJ8E EmpStatTyp 0+ record :DeptGname /archar7:7?;% DeptGa/g num1er;( EmpStatVar EmpStatTyp( BEW9L DBOSGBUT8UT.8UTGK9LE:#Department A/g Salary#;( DBOSGBUT8UT.8UTGK9LE:#-------------------------#;( *or EmpStatVar 0n :+elect round:a/g:e.+alary;%7; a%d.departmentGname 1 from department+ d% employee+ e where d.departmentG0d-e.departmentG0d group 1y d.departmentGname; KBB8 DBOSGBUT8UT.8UTGK9LE:R8AD:EmpStatVar.1%2S%# #;XX TBGC$AR:EmpStatVar.a%#>>>%>>>%>>>.>>#;;( ELD KBB8( ELD( 3$

/#ing Orac&e )ata*a#e

end empG+al(

l#c+ R&n. The P.I3K. code #s e/ecu$ed. E. Hou can e/ecu$e $he s$ored !rocedure b" en$er#ng $he 5ollo*#ng P.I3K. code and cl#c+#ng R&n.
1eg0n empG+al.empG+tat( end(

5.

>5 "ou *an$ $o sa'e $he P.I3K. code 5or 5u$ure use, cl#c+ Sa0e.

Creatin" a Data)a'e ,'er


The &dm#n#s$ra$#on console #s used $o manage da$abase(

3$orage Memor" Users &c$#'#$" o5 sess#ons and o!era$#ons

The #ns$alla$#on !rocess crea$es an accoun$ named system. Th#s accoun$ #s cons#dered an adm#n#s$ra$or accoun$ because #$ has D-& !r#'#leges :SBSDCA;. To !er5orm da$abase adm#n#s$ra$#on such as crea$#ng ne* users, log #n$o Oracle &!!l#ca$#on @/!ress as $he system user. To crea$e a da$abase user( 1. On $he Da$abase home !age, cl#c+ $he -.!,n, trat,on #con. l#c+ $he (ata)a e U er #con.

igure 2-" Oracle Application Express Administration screen%

4%

Orac&e 0""&ication 'x"re##

igure 2*" Oracle Application Express 6anage Database >sers screen% 2. On $he Manage Da$abase Users !age, cl#c+ Create. The rea$e Da$abase User !age #s d#s!la"ed.

igure 2?" Oracle Application Express (reate Database >ser screen% P. E. 5. F. @n$er user #n5orma$#on #n$o $e/$ 5#elds( >n $he Username 5#eld, en$er a ne* username. >n $he Pass*ord and on5#rm Pass*ord 5#elds, en$er a !ass*ord. Aran$ all crea$e ob8ec$ s"s$em !r#'#leges b" cl#c+#ng hec+ &ll a$ $he lo*er r#gh$?hand corner o5 $he User Pr#'#leges bo/.
41

/#ing Orac&e )ata*a#e

C. 8.

The D-& role #s b" de5aul$ no$ selec$ed. The D-& !r#'#lege, g#'es $he user $he ab#l#$" $o crea$e schema ob8ec$s #n o$her usersG schemas, and $o crea$e o$her users. l#c+ Create. The Manage Da$abase Users !age d#s!la"s a con5#rma$#on $ha$ $he user *as crea$ed.

igure '2" Oracle Application Express 6anage Database >sers screen%

8onitorin" Data)a'e Se''ion'


Hou can use $he Oracle Da$abase N@ gra!h#cal user #n$er5ace $o mon#$or $he curren$ da$abase sess#ons. Th#s enables "ou $o de$erm#ne $he users *ho are curren$l" logged #n $o $he da$abase and *ha$ a!!l#ca$#ons $he" are runn#ng. Hou can also use $he Oracle Da$abase N@ gra!h#cal user #n$er5ace $o +#ll a sess#onU$o cause #$ $o be d#sconnec$ed and #$s resources $o be rel#n=u#shed. 0hen "ou '#e* sess#ons, "ou can '#e*(

&ll sess#ons &c$#'e sess#ons onl" 3ess#ons $ha$ ma$ch a search s$r#ng

Hou should be logged #n as $he system user $o !er5orm an" da$abase adm#n#s$ra$#on. To '#e* $he curren$ sess#ons(

42

Orac&e 0""&ication 'x"re##

1.

On $he Da$abase home !age, cl#c+ $he -.!,n, trat,on #con. l#c+ Mon,tor.

igure '!" Oracle Application Express Administration screen% 2. On $he Da$abase Mon#$or !age, cl#c+ Se ,on .

igure '2" Oracle Application Express Administration 6onitor screen%

43

/#ing Orac&e )ata*a#e

P.

The 3ess#ons !age #s d#s!la"ed and sho*s $he curren$ ac$#'e sess#ons.

igure ''" Oracle Application Express Sessions screen% E. :O!$#onal; >n $he 3$a$us l#s$, selec$ -ll, and cl#c+ Go. The !age d#s!la"s all sess#ons, #nclud#ng #dle sess#ons. :&n e/am!le o5 an #dle sess#on #s a 3K.TPlus command l#ne sess#on $ha$ #s no$ curren$l" runn#ng a command.; :O!$#onal; 7arro* do*n $he sess#ons l#s$ b" en$er#ng search $e/$ #n$o $he Sear#' 5#eld and cl#c+#ng Go. & sess#on #s sho*n #5 an" o5 $he 5ollo*#ng 5#elds con$a#n $he search $e/$( 3>D, Da$abase User, Mach#ne, O3 User, l#en$ >n5orma$#on, l#en$ >den$#5#er, and Module. :O!$#onal; l#c+ an" o5 $he h"!erl#n+s abo'e $he 3earch 5#eld $o '#e* $he 5ollo*#ng #n5orma$#on 5or all sess#ons( .oc+s, 0a#$s, >n!u$IOu$!u$ :>IO;, runn#ng 3K. s$a$emen$s, and o!en cursors. :O!$#onal; Under $he 3>D column, cl#c+ a sess#on >D $o '#e* $he 3ess#on De$a#ls !age 5or $ha$ sess#on. The 3ess#on De$a#ls !age enables "ou $o +#ll $he sess#on.

5.

F. C.

Data)a'e <ac>up and Reco.er:


-ac+#ng u! and res$or#ng Oracle Da$abase N@ #s based on !ro$ec$#ng $he !h"s#cal 5#les $ha$ ma+e u! $he da$abase( $he da$a5#les, $he con$rol 5#le, $he ser'er !arame$er 5#le :3P2>.@;, and, #5 #n &R H>,@.OA mode, $he redo log 5#les. >n Oracle Da$abase N@, da$abase bac+u! and reco'er" #s handled b" Reco'er" Manager :RM&7;. Oracle Da$abase N@ #ncludes bac+u! and res$ore scr#!$s $ha$ "ou access us#ng menu on "our des+$o!. These scr#!$s !er5orm a 5ull bac+u! and res$ore o5 $he en$#re da$abase, and s$ore bac+u! 5#les #n $he $lash recovery area. Oracle Da$abase N@ #m!lemen$s a bac+u! re$en$#on !ol#c" $ha$ d#c$a$es $ha$ $*o com!le$e bac+u!s o5 $he da$abase mus$ be re$a#ned. >n &R H>,@.OA mode, all arch#'ed logs re=u#red 5or med#a reco'er" 5rom e#$her bac+u! are also re$a#ned. The da$abase au$oma$#call" manages bac+u!s and arch#'ed logs #n $he 5lash reco'er" area, and dele$es obsole$e bac+u!s and arch#'ed logs a$ $he end o5 each bac+u! 8ob. The bac+u! scr#!$ !er5orms onl#ne bac+u!s o5 da$abases #n &R H>,@.OA mode and o55l#ne bac+u!s o5 da$abases #n 7O&R H>,@.OA mode. Onl#ne bac+u!s are bac+u!s $ha$ can run *h#le $he da$abase #s runn#ng. O55l#ne bac+u!s are bac+u!s $ha$ run *hen $he da$abase #s shu$ do*n. The res$ore scr#!$ res$ores $he da$abase d#55eren$l" de!end#ng on *he$her log arch#'#ng #s on or o55.

44

Orac&e 0""&ication 'x"re##

.og arch#'#ng on :&R H>,@.OA mode; res$ores $he bac+ed u! da$abase 5#les, and $hen uses $he onl#ne and arch#'ed redo log 5#les $o reco'er $he da$abase $o $he s$a$e #$ *as #n be5ore $he so5$*are or med#a 5a#lure occurred. &ll comm#$$ed $ransac$#ons $ha$ $oo+ !lace a5$er $he las$ bac+u! are reco'ered, and an" uncomm#$$ed $ransac$#ons $ha$ *ere under *a" *hen $he 5a#lure $oo+ !lace are rolled bac+ :us#ng undo da$a 5rom $he res$ored undo $ables!ace;. .og arch#'#ng o55 :7O&R H>,@.OA mode; res$ores $he da$abase $o #$s s$a$e a$ $he las$ bac+u!. &n" $ransac$#ons $ha$ $oo+ !lace a5$er $he las$ bac+u! are los$.

Backing Up The Database


To bac+ u! $he da$abase( 1. 2. .og #n $o $he Oracle Da$abase N@ hos$ com!u$er as a user *ho #s a member o5 $he dba user grou!. Do one o5 $he 5ollo*#ng(

On .#nu/ *#$h Anome, selec$ -$$l,#at,on S Ora#le (ata)a e 10* E"$re (ata)a e. On .#nu/ *#$h 6D@, selec$ / Men& S Ora#le (ata)a e 10* E"$re On 0#ndo*s, selec$ Start S Pro*ra! S Ora#le (ata)a e 10* E"$re (ata)a e.

E.,t,on S 8a#4&$

E.,t,on S 8a#4&$ (ata)a e. E.,t,on S 8a#4&$

P.

& console *#ndo* o!ens so $ha$ "ou can #n$erac$ *#$h $he bac+u! scr#!$. >5 runn#ng #n &R H>,@.OA mode, $he scr#!$ d#s!la"s $he 5ollo*#ng ou$!u$(

igure '0" Oracle Application Express bac5up dialog% >5 !rom!$ed, ans*er y $o con5#rm $he da$abase shu$do*n and beg#n $he bac+u!. &5$er $he bac+u! #s com!le$e, $he scr#!$ d#s!la"s $he 5ollo*#ng ou$!u$(

45

/#ing Orac&e )ata*a#e

igure '," Oracle Application Express bac5up success$ul message% E. Press an" +e" $o close $he -ac+u! Da$abase *#ndo*.

.ogs con$a#n#ng $he ou$!u$ 5rom $he bac+u!s are s$ored #n $he 5ollo*#ng loca$#ons(
,BRACKEG$BOE/o<eG1acNupGcurrent.log ,BRACKEG$BOE/o<eG1acNupGpre/0ou+.log

estoring the Database


To res$ore a da$abase 5rom a bac+u!( 1. 2. .og #n $o $he Oracle Da$abase N@ hos$ com!u$er as a user *ho #s a member o5 $he dba user grou!. Do one o5 $he 5ollo*#ng(

On .#nu/ *#$h Anome, selec$ -$$l,#at,on S Ora#le (ata)a e 10* E"$re (ata)a e. On .#nu/ *#$h 6D@, selec$ / Men& S Ora#le (ata)a e 10* E"$re On 0#ndo*s, selec$ Start S Pro*ra! S Ora#le (ata)a e 10* E"$re (ata)a e.

E.,t,on S Re tore

E.,t,on S Re tore (ata)a e. E.,t,on S Re tore

P.

& console *#ndo* o!ens so $ha$ "ou can #n$erac$ *#$h $he res$ore scr#!$. The scr#!$ d#s!la"s $he 5ollo*#ng ou$!u$(

46

Orac&e 0""&ication 'x"re##

igure '4" Oracle Application Express restore dialog% E. &ns*er y $o con5#rm $he da$abase shu$do*n and beg#n $he res$ore. &5$er $he res$ore #s com!le$e, $he scr#!$ d#s!la"s $he 5ollo*#ng ou$!u$(

igure '-" Oracle Application Express restore success$ul message% 5. Press an" +e" $o close $he Res$ore Da$abase *#ndo*.

& log con$a#n#ng $he ou$!u$ 5rom $he res$ore #s s$ored #n(
,BRACKEG$BOE/o<eGre+tore.log

Orac%e S3LDP%u'
3K.TPlus #s OracleGs $rad#$#onal command l#ne $ool. >$ #s a'a#lable *hene'er $he da$abase #s #ns$alled. >$s command se$ #s l#m#$ed bu$ #$ allo*s ad?hoc =uer#es, scr#!$#ng and 5undamen$al da$abase adm#n#s$ra$#on. Man" boo+s, #nclud#ng $h#s one, use 3K.TPlus $o sho* 3K. e/am!les. 2or eas" de'elo!men$, "ou ma" !re5er $o use 3K. De'elo!er, *h#ch #s descr#bed #n $he ne/$ sec$#on.

47

/#ing Orac&e )ata*a#e

Startin" S3LDP%u'
Oracle Da$abase N@ se$s u! a menu o!$#on $o run 3K.TPlus. Ho*e'er, #n general, #5 "ou *an$ $o run 3K.TPlus 5rom a $erm#nal *#ndo*, $he sAlplus e/ecu$able mus$ be #n "our 8AT$ and se'eral en'#ronmen$ 'ar#ables need $o be se$ e/!l#c#$l". These are !re?se$ #n $he reg#s$r" on 0#ndo*s. On .#nu/, 5or Oracle Da$abase N@, se$ $he en'#ronmen$ *#$h(
, . /u+r/l01/oracle/<e/app/oracle/product/2?.7.?/+er/er/10n/oracleGen/.+h

7o$e $he s!ace a5$er $he !er#od. On o$her ed#$#ons o5 $he Oracle da$abase, $he <usr<local<bin<oraenv or <usr<local<bin<coraenv :5or users o5 ? shell; scr#!$s se$ $he en'#ronmen$. >n $he -ash shell, use(
, . /u+r/local/10n/oraen/ BRACKEGS9D - 34 !

Hou *#ll be !rom!$ed 5or $he s"s$em #den$#5#er :B3>DB; o5 $he da$abase on $h#s mach#ne $ha$ "ou #n$end $o connec$ $o. The a'a#lable 3>Ds can be seen #n <etc<oratab. T"!e $he des#red 3>D and !ress en$er. >5 "ou are runn#ng 3K.TPlus on a mach#ne remo$e 5rom $he da$abase ser'er, "ou need $o manuall" se$ $he en'#ronmen$. Once $he en'#ronmen$ #s se$, 3K.TPlus can be s$ar$ed *#$h $he +Flplu+ command(
, +Flplu+ SUKH8lu+: Relea+e 2?.7.?.2.? - 8roduct0on on Thu Lo/ S 22:2T:?> 7??M Copyr0ght :c; 2>M7% 7??6% Bracle. All r0ght+ re+er/ed. Enter u+er-name: hr Enter pa++word: Connected to: Bracle Data1a+e 2?g E<pre++ Ed0t0on Relea+e 2?.7.?.2.? - 8roduct0on SUK"

The !ass*ord "ou en$er #s no$ d#s!la"ed. The !rom!$ SUK" #s sho*n. T"!e $EK8 9LDEI $o 5#nd a l#s$ o5 commands. T"!e EI9T $o =u#$. >n de'elo!men$, #$ #s common $o !u$ $he username, !ass*ord and da$abase $ha$ "ou *an$ $o connec$ $o all on $he command l#ne, bu$ be*are $ha$ en$er#ng $he !ass*ord l#+e $h#s #s a secur#$" r#s+(
+Flplu+ hr/hrpwdPlocalho+t/IE

& be$$er !rac$#ce #s $o run(


+Flplu+ hrPlocalho+t/IE

Th#s *#ll !rom!$ 5or $he !ass*ord. &no$her *a" #s $o s$ar$ 3K.TPlus *#$hou$ a$$em!$#ng $o log on, and $hen use $he CBLLECT command(
, +Flplu+ /nolog . . . SUK" connect hr/hrpwdPlocalho+t/IE Connected. 4

Orac&e S1,2P&u# SUK"

To connec$ as a !r#'#leged user 5or da$abase adm#n#s$ra$#on, 5#rs$ log#n $o $he o!era$#ng s"s$em as a user #n $he dba grou! and $hen use(
, +Flplu+ / a+ +y+d1a

or
+Flplu+ /nolog SUK" connect / a+ +y+d1a

E&ecutin" S3L and PL5S3L State4ent' in S3LDP%u'


3K. s$a$emen$s such as =uer#es mus$ be $erm#na$ed *#$h a sem#?colon :<;(
SUK" +elect H from locat0on+(

or *#$h a s#ngle slash :/;(


SUK" +elect H from locat0on+ 7 /

Th#s las$ e/am!le also sho*s 3K.TPlus !rom!$#ng 5or a second l#ne o5 #n!u$. ode #n OracleGs !rocedural language, P.I3K., mus$ end *#$h a slash #n add#$#on $o $he P.I3K. codeGs sem#?colon(
SUK" 1eg0n 7 myproc:;( ) end( 5 /

The $erm#na$#ng sem#?colon or slash #s no$ !ar$ o5 $he s$a$emen$. Tools o$her $hen 3K.TPlus *#ll use d#55eren$ me$hods $o #nd#ca$e Mend o5 s$a$emen$O. >5 a blan+ l#ne :#n 3K.; or a s#ngle !er#od :#n 3K. or P.I3K.; #s en$ered, 3K.TPlus re$urns $o $he ma#n !rom!$ and does no$ e/ecu$e $he s$a$emen$(
SUK" +elect H from locat0on+ 7 SUK"

Contro%%in" 3uer: Output in S3LDP%u'


3K.TPlus has 'ar#ous *a"s $o con$rol ou$!u$ d#s!la". The SET command con$rols some 5orma$$#ng. 2or e/am!le, $o change $he !age s#1e :ho* o5$en $able column names re!ea$; and $he l#ne s#1e :*here l#nes *ra!;(
SUK" +et page+0Qe M? SUK" +et l0ne+0Qe 2)7

>5 "ou are 5e$ch#ng da$a 5rom .O7A, .O- or :*#$h 3K.TPlus 11g; 5rom -.O- columns, #ncrease $he ma/#mum number o5 charac$ers $ha$ *#ll d#s!la" :$he de5aul$ #s 8us$ 80;(
4$

/#ing Orac&e )ata*a#e SUK" +et long 2???

7o$e $hese are local commands $o 3K.TPlus and do no$ need a sem#?colon. The column *#d$h o5 =uer#es can be changed *#$h $he CBKUOL command, here se$$#ng $he CBULTRJGLAOE ou$!u$ *#d$h $o 7? charac$ers, and $he REW9BLG9D column $o a numer#c 5orma$ *#$h a dec#mal !lace(
SUK" +elect H from countr0e+ where countryG0d - #*R#( CB CBULTRJGLAOE REW9BLG9D -- ---------------------------------------- ---------*R *rance 2 SUK" co(umn country_name )ormat a*+ SUK" co(umn re,ion_id )ormat --.+ SUK" +elect H from countr0e+ where countryG0d - #*R#( CB CBULTRJGLAOE REW9BLG9D -- -------------------- ---------*R *rance 2.?

Ou$!u$ can be s!ooled $o a 5#le *#$h $he S8BBK command(


SUK" +pool /tmp/myf0le.log

Runnin" Script' in S3LDP%u'


>5 mul$#!le 3K. s$a$emen$s are s$ored #n a scr#!$ myscript%sAl, $he" can be e/ecu$ed *#$h $he P command e#$her 5rom $he $erm#nal !rom!$(
, +Flplu+ hrPlocalho+t/IE Pmy+cr0pt.+Fl

or 5rom $he 3K.TPlus !rom!$(


SUK" Pmy+cr0pt.+Fl

-ecause 3K.TPlus doesnG$ ha'e a 5ull h#s$or" command, *r#$#ng s$a$emen$s #n scr#!$s us#ng an e/$ernal ed#$or and runn#ng $hem $h#s *a" #s recommended.

In2or4ation On Ta)%e' in S3LDP%u'


Kuer#es 5rom #nbu#l$ '#e*s l#+e USERGTABKES and USERG9LDEIES *#ll sho* #n5orma$#on abou$ $he ob8ec$s "ou o*n. & $rad#$#onal =uer" 5rom $he CAT '#e* g#'es a shor$ summar"(
SUK" +elect H from cat( TABKEGLAOE -----------------------------CBULTR9ES DE8ARTOELTS DE8ARTOELTSGSEU EO8KBJEES 5% TABKEGTJ8E ----------TABKE TABKE SEUUELCE TABKE

Orac&e S1,2P&u# EO8KBJEESGSEU YBBS YBBG$9STBRJ KBCAT9BLS KBCAT9BLSGSEU REW9BLS 2? row+ +elected. SEUUELCE TABKE TABKE TABKE SEUUELCE TABKE

To 5#nd ou$ abou$ $he columns o5 a $able, =uer" $he USERGTABGCBKUOLS '#e*, or s#m!l" use $he DESCR9BE command $o g#'e an o'er'#e*(
SUK" de+cr01e countr0e+ Lame Lull! ----------------------------------------- -------CBULTRJG9D LBT LUKK CBULTRJGLAOE REW9BLG9D Type ---------------------------C$AR:7; VARC$AR7:5?; LUOBER

Acce''in" the De4on'tration Ta)%e' in S3LDP%u'


Th#s boo+ uses $he Human Resource sam!le $ables, loca$ed #n $he hr schema. To unloc+ $h#s accoun$ and se$ $he !ass*ord a5$er $he da$abase has been #ns$alled, connec$ as a !r#'#leged user and e/ecu$e an AKTER USER command(
SUK" connect +y+tem/+y+tempwd SUK" alter u+er hr 0dent0f0ed 1y hrpwd account unlocN(

Th#s se$s $he !ass*ord $o hrpwd.

Orac%e S3L De.e%oper


>n add#$#on $o Oracle &!!l#ca$#on @/!ress or 3K.TPlus, "ou can also use Oracle 3K. De'elo!er 5or da$abase de'elo!men$ and ma#n$enance. Oracle 3K. De'elo!er #s a 5ree, $h#c+?cl#en$ gra!h#cal $ool 5or da$abase de'elo!men$. Hou can use 3K. De'elo!er $o e/ecu$e 3K. s$a$emen$s, e/ecu$e and debug P.I3K. s$a$emen$s, and $o run a 5e* 3K.TPlus commands :l#+e DESCR9BE;. 3K. De'elo!er #ncludes some !rebu#l$ re!or$s, and "ou can crea$e and sa'e "our o*n re!or$s. 3K. De'elo!er can connec$ $o Oracle da$abases 5rom 'ers#on D.2.0.1 on*ards, and #s a'a#lable on .#nu/, 0#ndo*s and Mac O3 N. Hou can do*nload 3K. De'elo!er 5rom $he Oracle Technolog" 7e$*or+ a$ http://www.oracle.com/technology/product+/data1a+e/+FlGde/eloper. Hou can also do*nload !a$ches, e/$ens#ons, documen$a$#on, and o$her resources 5rom $h#s s#$e. There #s also a d#scuss#on 5orum 5or "ou $o as+ =ues$#ons o5 o$her users, and g#'e 5eedbac+ $o $he 3K. De'elo!er !roduc$ $eam.

Creatin" a Data)a'e Connection


0hen "ou s$ar$ 3K. De'elo!er 5or $he 5#rs$ $#me, $here are no da$abase connec$#ons con5#gured, so $he 5#rs$ $h#ng "ou need $o do #s crea$e one. The de5aul$ 3K. De'elo!er screen #s sho*n #n 2#gure P8.
51

/#ing Orac&e )ata*a#e

igure '*" Oracle S@3 Developer login screen% To crea$e a da$abase connec$#on $o $he local Oracle Da$abase N@ da$abase( 1. 3elec$ Conne#t,on #n $he le5$ !ane, r#gh$ cl#c+ and selec$ Ne6 Conne#t,on. @n$er $he log#n creden$#als 5or $he Oracle Da$abase N@ *#$h $he username hr, and !ass*ord "ou crea$ed 5or $he hr user, $he hos$name localho+t, and $he 3>D IE.

igure '?" Oracle S@3 Developer (onnection screen% 2. l#c+ $he Te t bu$$on $o $es$ $he connec$#on. & message #s d#s!la"ed a$ $he bo$$om le5$ s#de o5 $he d#alog $o $ell "ou *he$her $he $es$ connec$#on succeeded. l#c+ $he Conne#t bu$$on $o sa'e $he connec$#on and connec$ $o #$.

52

Orac&e S1, )eve&o"er

igure 02" Oracle S@3 Developer main screen% 0hen "ou ha'e connec$ed $o a da$abase, "ou can bro*se $hrough $he da$abase ob8ec$s d#s!la"ed #n $he le5$ !ane, and $he r#gh$ !ane sho*s $he con$en$s o5 $he ob8ec$. >n 2#gure E0, $he employees $able #s d#s!la"ed.

!diting Data
Hou can '#e* and ed#$ da$a us#ng $he Da$a $ab 5or a $able de5#n#$#on. 3elec$ $he Da$a $ab 5or $he employees $able $o d#s!la" $he records a'a#lable. Hou can add ro*s, u!da$e da$a and dele$e ro*s us#ng $he da$a gr#d. >5 "ou ma+e an" changes, $he records are mar+ed *#$h an as$er#s+. Throughou$ 3K. De'elo!er, $here are con$e/$ sens#$#'e menus. 2#gure E1 sho*s $he cho#ce o5 con$e/$ menus a'a#lable #n $he da$a gr#d.

igure 0!" Oracle S@3 Developer Data Grid%

53

/#ing Orac&e )ata*a#e

Creatin" a Ta)%e
Hou can crea$e da$abase ob8ec$s such as $ables, '#e*s, #nde/es, and P.I3K. !rocedures us#ng 3K. De'elo!er. To crea$e a da$abase ob8ec$, r#gh$ cl#c+ on $he da$abase ob8ec$ $"!e "ou *an$ $o crea$e, and 5ollo* $he d#alogs. Hou can use $h#s me$hod $o crea$e an" o5 $he da$abase ob8ec$s d#s!la"ed #n $he le5$ !ane. To crea$e a ne* $able( 1. 3elec$ Ta)le #n $he le5$ !ane, r#gh$ cl#c+ and selec$ Create Ta)le. The rea$e Table d#alog #s d#s!la"ed. @n$er $he column names, $"!es and o$her !arame$ers as re=u#red.

igure 02" Oracle S@3 Developer (reate #able screen% l#c+ $he -.0an#e. chec+ bo/ $o see more ad'anced o!$#ons l#+e cons$ra#n$s, #nde/es, 5ore#gn +e"s, and !ar$#$#ons.

igure 0'" Oracle S@3 Developer Advanced (reate #able screen% 2. l#c+ O/ $o crea$e $he $able. The ne* $able mytable #s no* l#s$ed #n $he le5$ !ane.

54

Orac&e S1, )eve&o"er

igure 00" Oracle S@3 Developer screen sho&ing the ne& table7 mytable% l#c+ on $he $abs d#s!la"ed #n $he r#gh$ !ane $o see $he o!$#ons a'a#lable on $he $able, such as $he Da$a $ab, *h#ch enables "ou $o add, dele$e, mod#5", sor$, and 5#l$er ro*s #n $he $able.

E&ecutin" a S3L 3uer:


The 3K. 0or+shee$ com!onen$ #ncluded #n 3K. De'elo!er can be used $o e/ecu$e 3K. and P.I3K. s$a$emen$s. 3ome 3K.TPlus commands can also be e/ecu$ed. To e/ecu$e a 3K. s$a$emen$ #n 3K. De'elo!er( 1. 3elec$ $he (ata)a e Conne#t,on $ab #n $he r#gh$ hand !ane. Th#s #s $he connec$#on crea$ed earl#er #n (reating a Database (onnection. The 3K. 0or+shee$ com!onen$ #s d#s!la"ed, and sho*s an area $o en$er s$a$emen$s, and a se$ o5 $abs belo* $ha$ 5or 5ur$her o!$#ons. >5 $he $ab #s no$ a'a#lable, selec$ $he menu Tool SS3% 9or4 'eet. Hou are !rom!$ed 5or $he da$abase connec$#on name.

55

/#ing Orac&e )ata*a#e

igure 0," Oracle S@3 Developer screen sho&ing S@3 /or5sheet% 2. @n$er $he 5ollo*#ng $*o s$a$emen$s #n $he 3K. 0or+shee$(
DESCR9BE $R.EO8KBJEES SEKECT H *RBO $R.EO8KBJEES(

l#c+ $he R&n S#r,$t #con :$he second 5rom $he le5$ #n $he r#gh$ hand !ane;, or !ress +5. -o$h $he l#nes o5 $h#s scr#!$ are run and $he ou$!u$ #s d#s!la"ed #n $abs belo*. Hou can '#e* $he ou$!u$ o5 $he SEKECT s$a$emen$ #n $he Re &lt $ab, us#ng +:, and $he ou$!u$ o5 $he *hole scr#!$ :#nclud#ng $he DESCR9BE and SEKECT s$a$emen$s; #n $he S#r,$t O&t$&t $ab, b" us#ng +5. >5 "ou ha'e used OracleLs command l#ne $ool 3K.TPlus, $he ou$!u$ #n $he 3cr#!$ Ou$!u$ *#ndo* #s l#+el" $o be 5am#l#ar $o "ou.

igure 04" Oracle S@3 Developer screen sho&ing S@3 /or5sheet &ith output%

56

Orac&e S1, )eve&o"er

P.

>5 "ou *an$ $o e/ecu$e a s#ngle l#ne o5 $he $*o?l#ne s$a$emen$, selec$ $he l#ne "ou *an$ $o e/ecu$e and cl#c+ on $he E"e#&te State!ent #con :$he 5#rs$ 5rom $he le5$ #n $he r#gh$ hand !ane;, or !ress +:. >n $h#s case, $he SEKECT s$a$emen$ :second l#ne; #s selec$ed, and $he resul$s are sho*n #n $he Resul$s $ab.

igure 0-" Oracle S@3 Developer screen sho&ing S@3 /or5sheet &ith output% Hou should also $a+e a momen$ $o cl#c+ on $he Sn,$$et on $he $o! r#gh$ hand o5 $he #n$er5ace. 3n#!!e$s are a hand" se$ o5 3K. and P.I3K. code sn#!!e$s $ha$ "ou can drag #n$o $he 3K. 0or+shee$ com!onen$.

igure 0*" Oracle S@3 Developer screen sho&ing code snippets%

Editin"B Co4pi%in" and Runnin" PL5S3L


Hou can use 3K. De'elo!er $o bro*se, crea$e, ed#$, com!#le, run, and debug P.I3K.. 1. R#gh$ cl#c+ $he Pro#e.&re node and selec$ Ne6 Pro#e.&re 5rom $he con$e/$ menu. &dd $he name
57

/#ing Orac&e )ata*a#e ADDGDE8T and cl#c+ O/.

igure 0?" S@3 Developer ne& procedure dialog% 2. The P.I3K. @d#$or #s o!ened. Re!lace or add $he $e/$ so $ha$ "our code #s as 5ollo*s(
CREATE BR RE8KACE 8RBCEDURE ADDGDE8T :LAOE 9L DE8ARTOELTS.DE8ARTOELTGLAOE=TJ8E% KBC 9L DE8ARTOELTS.KBCAT9BLG9D=TJ8E; 9S BEW9L 9LSERT 9LTB DE8ARTOELTS:DE8ARTOELTG9D% DE8ARTOELTGLAOE% KBCAT9BLG9D; VAKUES:DE8ARTOELTSGSEU.LEITVAK% LAOE% KBC;( ELD ADDGDE8T(

P.

om!#le $he code b" selec$#ng $he com!#le #con #n $he $oolbar. >5 $here are errors, $he" *#ll a!!ear #n $he Message?.og *#ndo* belo*

Orac&e S1, )eve&o"er

igure ,2" S@3 Developer P3<S@3 (ode Editor% E. 5. Run $he !rocedure. The green arro* #con runs $he code. 3K. De'elo!er !ro'#des d#alog *#$h an anon"mous bloc+ $o hel! $es$ and run "our code. 2#nd and re!lace $he LUKK 'alues *#$h 'alues o5 "our o*n, 5or e/am!le, Tra0n0ng and 2M??. Kuer" $he De!ar$men$s $able $o see $he resul$s added.

igure ,!" S@3 Developer Run P3<S@3 dialog%

Runnin" Report'
There are a number o5 re!or$s #ncluded *#$h 3K. De'elo!er $ha$ ma" be use5ul $o "ou, 5or e/am!le, ge$$#ng l#s$s o5 all users #n a da$abase, all $he $ables o*ned b" a user, or all $he '#e*s a'a#lable #n $he da$a d#c$#onar".

5$

/#ing Orac&e )ata*a#e

Hou can also crea$e "our o*n re!or$s :us#ng 3K. and P.I3K.;, and #nclude $hem #n 3K. De'elo!er. To d#s!la" $he 'ers#on numbers o5 $he da$abase com!onen$s us#ng one o5 $he su!!l#ed re!or$s( 1. 3elec$ $he Re$ort $ab #n $he le5$ hand !ane. 7a'#ga$e do*n $o Re$ort S (ata (,#t,onary Re$ort S -)o&t Yo&r (ata)a e S Ver ,on 8anner. The re!or$ #s run and $he resul$s are d#s!la"ed #n $he r#gh$ hand !ane.

igure ,2" Oracle S@3 Developer screen sho&ing output $rom a report% 2. l#c+ $he R&n Re$ort ,n S3% 9or4 'eet #con :ne/$ $o Re5resh;. The source code #s *r#$$en $o $he 3K. 0or+shee$. Hou can also e/!or$ $he su!!l#ed re!or$s $o ed#$ and add bac+ as user de5#ned re!or$s.

igure ,'" Oracle S@3 Developer screen sho&ing the source code o$ a report%

6%

Orac&e S1, )eve&o"er

Creatin" Report'
To crea$e "our o*n re!or$s, selec$ U er (e5,ne. Re$ort , and r#gh$ cl#c+. >$ #s good !rac$#ce $o crea$e 5older 5or "our re!or$s, $o ca$egor#1e $hem. 1. R#gh$ cl#c+ $he U er (e5,ne. Re$ort node and selec$ -.. +ol.er. om!le$e $he de$a#ls #n $he d#alog. Hou can use $h#s 5older $o #m!or$ or add ne* re!or$s.

igure ,0" S@3 Developer >ser De$ined Reports 2. P. 3elec$ $he 5older "ou crea$ed and r#gh$ cl#c+ $o selec$ -.. Re$ort. A#'e $he re!or$ a name and descr#!$#on and en$er $he 5ollo*#ng 3K. $e/$.
SEKECT Ka+tGname% DE8ARTOELTGLAOE% C9TJ *RBO DE8ARTOELTS D% KBCAT9BLS K% EO8KBJEES E &$ERE:D.KBCAT9BLG9D - K.KBCAT9BLG9D; ALD:D.OALAWERG9D- E.EO8KBJEEG9D; ALD:E.DE8ARTOELTG9D- D.DE8ARTOELTG9D; order 1y C9TJ% DE8ARTOELTGLAOE

E.

7o$#ce $ha$ $he de5aul$ s$"le #s table. Hou can $es$ $he re!or$ $o ensure "our 3K. #s correc$, *#$hou$ lea'#ng $he d#alog. l#c+ $he Te t bu$$on on $he (eta,l $ab.

61

/#ing Orac&e )ata*a#e

igure ,," S@3 Developer (reate Report Dialog 5. F. l#c+ -$$ly $o sa'e and close. To run $he re!or$, selec$ #$ #n $he Re!or$s 7a'#ga$or. Hou are !rom!$ed 5or a da$abase connec$#on.

igure ,4" S@3 Developer >ser De$ined7 #abular Report Hou can crea$e $abular re!or$s, char$s, dr#ll do*n re!or$s and mas$erIde$a#l re!or$s. 2or all re!or$s, su!!l#ed and user de5#ned, "ou can e/!or$, #m!or$ and share "our re!or$s.

62

CHAPTER -

INSTALLIN! APACHE HTTP SER@ER


Th#s ha!$er g#'es "ou $he s$e!s needed $o #ns$all and con5#gure $he &!ache HTTP 3er'er 5or use *#$h PHP. >5 "ou alread" ha'e &!ache #ns$alled, "ou can s+#! $h#s cha!$er. The #ns$ruc$#ons uses &!ache HTTP 3er'er Release 2.0.5D 5rom http://httpd.apache.org/download.cg0. 3$e!s are g#'en 5or Oracle @n$er!r#se .#nu/ and 0#ndo*s NP Pro5ess#onal @d#$#on. The !rocedure $o #ns$all on o$her .#nu/ !la$5orms #s $he same as 5or Oracle @n$er!r#se .#nu/.

Note: Apache release 2.2.9 is the latest version available. The installation shown in this Chapter may need some variation if this latest version is used.

In'ta%%in" Apache HTTP Ser.er on Linu&


To #ns$all $he &!ache HTTP 3er'er 5or use *#$h PHP on Oracle @n$er!r#se .#nu/. 1. 2. Do*nload $he &!ache HTTP 3er'er 5rom http://httpd.apache.org/download.cg0. The 'ers#on used #n $h#s #ns$alla$#on #s httpd+2%2%,?%tar%b.2. .og #n as $he root user and e/$rac$ $he 5#les(
R tar -Z</f httpd-7.?.6>.tar.1Q7

>5 "ou are 5am#l#ar *#$h $he tar command on U7>N s"s$ems, "ou ma" be *onder#ng *h" *e d#d no$ need $o #n'o+e 1unQ0p7 $o e/$rac$ $he %tar 5#le. .#nu/ #ncludes $he A7U 'ers#on o5 tar, *h#ch has a ne* -Z 5lag $o au$oma$#call" uncom!ress a b1#!!ed %tar 5#le. >5 "ou do*nloaded $he %g. g1#!!ed 5#le, "ou could ha'e used $he -Q 5lag #ns$ead. P.
R R R R

on5#gure and bu#ld $he *eb ser'er( cd httpd-7.?.6> ./conf0gure --pref0<-/u+r/local/apache --ena1le-module-+o maNe maNe 0n+tall

0hen con5#gur#ng $he *eb ser'er, $he o!$#on --ena1le-module-+o allo*s PHP $o be com!#led as a D"nam#c 3hared Ob8ec$ :D3O;. &lso, $he --pref0<- o!$#on se$s *here &!ache HTTP 3er'er *#ll be #ns$alled dur#ng $he command maNe 0n+tall.

63

In#ta&&ing 0"ache H--P Server

Note: With Apache 2, you should use the default pre for! "#" $%"ulti #rocessin& "odule'(, because thread safety of many of the #)# libraries is not !nown.

Startin" and Stoppin" Apache HTTP Ser.er


The apachectl scr#!$ #s #ns$alled #n $he &!ache bin d#rec$or". Use $h#s scr#!$ $o s$ar$ and s$o! &!ache HTTP 3er'er. To s$ar$ &!ache HTTP 3er'er(
R /u+r/local/apache/10n/apachectl +tart

Hou should $es$ $ha$ &!ache has been #ns$alled !ro!erl" and #s s$ar$ed on "our mach#ne b" o!en#ng "our *eb bro*ser $o http://localho+t/ or http://27T.?.?.2/ $o d#s!la" $he &!ache home !age. 7o* s$o! &!ache so #$ can be con5#gured 5or PHP(
R /u+r/local/apache/10n/apachectl +top

Note: *f you are usin& +racle ,atabase-.g /elease -..2, but not +racle ,atabase 01, you must &ive the nobody user access to files in the +racle directory. With +racle ,atabase -.g /elease -..2...2 onwards, there is a script located in $ORACLE_HOME/install/changePerm.sh to do this.

>5 $here are errors, $he" are d#s!la"ed on "our screen. @rrors ma" also be recorded #n <usr<local<apache<logs<error8log. >5 "ou ha'e !roblems, chec+ "our httpd%con$ and php%ini con5#gura$#on 5#les 5or an" #ncorrec$ se$$#ngs, and ma+e correc$#ons.

Con2i"urin" Apache HTTP Ser.er on Linu&


0hen "ou use &!ache HTTP 3er'er *#$h PHP O >8, "ou mus$ se$ some Oracle en'#ronmen$ 'ar#ables be5ore s$ar$#ng $he *eb ser'er. 0h#ch 'ar#ables "ou need $o se$ are de$erm#ned b" ho* PHP #s #ns$alled, ho* "ou connec$, and *ha$ o!$#onal se$$#ngs are des#red. >n5orma$#on abou$ se$$#ng $he en'#ronmen$ #s #n Setting Oracle Environment Dariables $or Apache #n $he cha!$er (onnecting to Oracle >sing O()*.

Note: ,o not set +racle environment variables in #)# scripts with puten/:;. The web server may load +racle libraries and initiali2e +racle data structures before runnin& your script. 3sin& puten/:; causes hard to trac! errors as the behavior is not consistent for all variables, web servers, operatin& systems, or +C*4 functions. 5ariables should be set prior to Apache startin&.

In'ta%%in" Apache HTTP Ser.er on Windo9'


The 5ollo*#ng !rocedure descr#bes ho* $o #ns$all $he &!ache HTTP 3er'er on 0#ndo*s.
64

In#ta&&ing 0"ache H--P Server on Window#

1.

Do*nload $he &!ache HTTP 3er'er 0#ndo*s b#nar#es 5rom http://www.apache.org/d0+t/httpd/10nar0e+/w0n)7/. The release used #n $h#s #ns$alla$#on #s &!ache HTTP 3er'er 2.0.5D, and $he do*nloaded 5#le #s named apache82%2%,?+&in'2+x*4+no8ssl%msi% Double cl#c+ $he do*nloaded 5#le apache82%2%,?+&in'2+x*4+no8ssl%msi% 2ollo* $he &!ache HTTP 3er'er #ns$alla$#on *#1ards. Hou should selec$ $o #ns$all &!ache HTTP 3er'er 5or -ll U er ; on Port 80, because $he only 5or t'e C&rrent U er al$erna$#'e *#ll clash *#$h Oracle Da$abase @/!ress @d#$#onLs de5aul$ !or$ 8080.

2. P.

Startin" and Stoppin" Apache HTTP Ser.er


&s !ar$ o5 #ns$alla$#on, $he &!ache HTTP 3er'er #s s$ar$ed. Hou should no* $es$ $ha$ &!ache HTTP 3er'er has been #ns$alled !ro!erl" and s$ar$ed on "our mach#ne b" o!en#ng "our *eb bro*ser $o http://localho+t/ or http://27T.?.?.2/ $o d#s!la" $he &!ache home !age. Hour s"s$em $ra" has an &!ache Mon#$or con$rol $ha$ ma+es #$ eas" $o s$o! and res$ar$ $he &!ache HTTP 3er'er *hen needed. &l$erna$#'el", use $he &!ache o!$#ons added $o "our 0#ndo*s 3$ar$ menu.

65

In#ta&&ing 0"ache H--P Server

66

CHAPTER /

INSTALLIN! PHP
Th#s ha!$er d#scusses *a"s o5 #ns$all#ng PHP on .#nu/ and 0#ndo*s. >ns$alla$#on on o$her s"s$ems such as 3olar#s and Mac O3 N #s s#m#lar $o $he .#nu/ s$e!s. The ma#n #ns$alla$#on scenar#os are co'ered. Hou m#gh$ come across o$her s#$ua$#ons $ha$ re=u#re a comb#na$#on o5 s$e!s $o be 5ollo*ed. The &!ache HTTP 3er'er need $o be #ns$alled be5ore #ns$all#ng PHP, and a da$abase mus$ be access#ble #n "our ne$*or+.

In'ta%%in" PHP 9ith OCI0 on Linu&


These s$e!s assume $he de5aul$ &!ache httpd RPM !ac+age #n used on Oracle @n$er!r#se .#nu/ 5. Ho*e'er, #5 "ou manuall" #ns$alled &!ache #n <usr<local<apache, $hen $he command $o s$ar$ and s$o! &!ache #s <usr<local<apache<bin<apachectl, $he &PN3 u$#l#$" #s #n <usr<local<apache<bin<apxs, $he con5#gura$#on 5#le #s #n <usr<local<apache<con$<httpd%con$ and $he module d#rec$or" #s <usr<local<apache<modules. 0hen runn#ng PHP, use $he same 'ers#on o5 $he Oracle l#brar#es as *ere used dur#ng $he bu#ld !rocess.

In'ta%%in" OCI0 ,'in" a Loca% Data)a'e


To #ns$all O >8 on Oracle @n$er!r#se .#nu/ *h#ch has a da$abase #ns$alled, log#n as $he root user( 1. 3hu$do*n &!ache(
R /u+r/+10n/apachectl +top

2. P.

Do*nload PHP 5.2.C 5rom http://www.php.net/download+.php. .og #n as $he root user and e/$rac$ $he PHP source code(
R tar -Z<f php-6.7.T.tar.1Q7 R cd php-6.7.T

>5 "ou do*nloaded $he %tar%g. 5#le, e/$rac$ #$ *#$h tar -Q<f. E. on5#gure PHP *#$h O >8(
R e<port BRACKEG$BOE-/u+r/l01/oracle/<e/app/oracle/product/2?.7.?/+er/er R ./conf0gure E " --w0th-ap<+7-/u+r/+10n/ap<+ E " --w0th-oc0M-,BRACKEG$BOE E " --ena1le-+0gch0ld

Use $he a!!ro!r#a$e !a$h $o "our Oracle so5$*are #ns$alla$#on. The !a$h can be 5ound #n <etc<oratab. >5 "ou #ns$alled "our o*n &!ache, use $he a!!ro!r#a$e !a$h $o $he &!ache e/$ens#on $ool, 5or e/am!le <usr<local<apache<bin<apxs. >5 "ou ha'e &!ache 1.P #ns$ead o5 &!ache 2, change $he [-w0th \ap<+7 o!$#on $o [-w0th-ap<+.
67

In#ta&&ing PHP

Note: Althou&h conf0gure fails if it cannot find the +racle software, prior to #)# 6.2.7 it silently i&nores a misspelled option name. 8efore continuin&, review the output and ma!e sure the %checking or Oracle !OC"#$ s%&&ort' line was mar!ed as %yes'.

O$her des#red o!$#ons and e/$ens#ons can be used #n $he conf0gure command. To l#s$ all $he o!$#ons, use $he command(
conf0gure [-help

5.

Ma+e and #ns$all PHP(


R maNe R maNe 0n+tall

F.

o!" PHPLs su!!l#ed #n#$#al#1a$#on 5#le php%ini+recommended% To 5#nd $he des$#na$#on d#rec$or", use $he
--0n0 o!$#on $o command l#ne PHP( R php --0n0 Conf0gurat0on *0le :php.0n0; 8ath: Koaded Conf0gurat0on *0le: Scan for add0t0onal .0n0 f0le+ 0n: Add0t0onal .0n0 f0le+ par+ed: /u+r/local/l01 :none; :none; :none;

Th#s sho*s $he !a$h #s <usr<local<lib. o!" $he 5#le $o $ha$ d#rec$or"(
R cp php.0n0-recommended /u+r/local/l01/php.0n0

C.

2or $es$#ng #$ #s hel!5ul $o ed#$ php%ini and se$ d0+playGerror+ $o Bn so "ou see an" !roblems #n "our code. Ma+e sure "ou change $h#s con5#gura$#on o!$#on bac+ $o Bff be5ore ma+#ng "our a!!l#ca$#on a'a#lable $o users. @d#$ &!acheLs con5#gura$#on 5#le <etc<httpd<con$<httpd%con$ and add $he 5ollo*#ng l#nes(
R R Th0+ +ect0on w0ll call 8$8 for .php% .phtml% and .php+ f0le+ R AddType appl0cat0on/<-httpd-php .php AddType appl0cat0on/<-httpd-php .phtml AddType appl0cat0on/<-httpd-php-+ource .php+

8.

D.

>5 a KoadOodule l#ne *as no$ alread" #nser$ed b" $he PHP #ns$all, add #$ $oo(
KoadOodule php6Gmodule /u+r/l01/httpd/module+/l01php6.+o

10. 3e$ an" re=u#red Oracle en'#ronmen$ 'ar#ables, such as BRACKEG$BOE, KDGK9BRARJG8AT$ and LKSGKALW. 3ee Setting Oracle Environment Dariables $or Apache #n $he cha!$er (onnecting to Oracle >sing O()*.
6

In#ta&&ing PHP with O(I on ,inux

11. Res$ar$ &!ache(


R /u+r/+10n/apachectl +tart

In'ta%%in" OCI0 ,'in" Orac%e In'tant C%ient


PHP l#n+s *#$h Oracle l#brar#es. >5 PHP #s no$ #ns$alled on $he da$abase hos$ mach#ne, $hen a small se$ o5 l#brar#es called Oracle >ns$an$ l#en$ can be used. To #ns$all *#$h Oracle >ns$an$ l#en$( 1. 3hu$do*n &!ache(
R /u+r/+10n/apachectl +top

2. P.

Do*nload PHP 5.2.C 5rom http://www.php.net/download+.php. @/$rac$ $he PHP source code(
R tar -Z<f php-6.7.T.tar.1Q7

>5 "ou do*nloaded $he %tar%g. 5#le, e/$rac$ #$ *#$h tar -Q<f. E. Do*nload $he RPM or 9>P 5#les 5or $he 8a ,# and S(/ >ns$an$ l#en$ !ac+ages 5rom $he >ns$an$ l#en$ !age on $he Oracle Technolog" 7e$*or+( http://www.oracle.com/technology/tech/oc0/0n+tantcl0ent/0n+tantcl0ent.htm l ollec$#'el", $he $*o !ac+ages are abou$ E1 M- #n s#1e. The e'en smaller 8a ,#1l,te !ac+age can be subs$#$u$ed 5or 8a ,#, #5 #$s charac$er se$ and error message language res$r#c$#ons do no$ #m!ac$ "our a!!l#ca$#on. 5. >5 "ou are us#ng $he RPMs, #ns$all $he RPMs as $he root user(
R rpm -U/h oracle-0n+tantcl0ent22.2-1a+0c-22.2.?.T.?-2.0)MS.rpm R rpm -U/h oracle-0n+tantcl0ent22.2-de/el-22.2.?.T.?-2.0)MS.rpm

The 5#rs$ RPM !u$s $he Oracle l#brar#es #n <usr<lib<oracle<!!%!<client<lib and $he second crea$es headers #n <usr<include<oracle<!!%!<client% >5 "ou are us#ng $he >ns$an$ l#en$ 9>P 5#les, un1#! $he -as#c and $he 3D6 !ac+ages $o a d#rec$or" o5 "our cho#ce, 5or e/am!le EHO6E<instantclient8!!8!. The 5#les should be un1#!!ed $oge$her so $he 3D6 #s #n EHO6E<instantclient8!!8!<sd5. F. >5 >ns$an$ l#en$ *as #ns$alled 5rom $he 9>P 5#les, crea$e a s"mbol#c l#n+(
R cd ,$BOE/0n+tantcl0entG22G2 R ln -+ l01clnt+h.+o.22.2 l01clnt+h.+o

C.

on5#gure PHP(
R cd php-6.7.T R ./conf0gure E 6$

In#ta&&ing PHP " " " --w0th-ap<+7-/u+r/+10n/ap<+ E --w0th-oc0M-0n+tantcl0ent%/u+r/l01/oracle/22.2/cl0ent/l01 E --ena1le-+0gch0ld

>5 "ou are us#ng $he 9>P 5#les $hen change $he --w0th-oc0M o!$#on $o $he un1#!!ed d#rec$or"(
--w0th-oc0M-0n+tantcl0ent%,$BOE/0n+tantcl0entG22G2

>5 "ou are us#ng >ns$an$ l#en$ RPMs on FE b#$ .#nu/, use(
--w0th-oc0M-0n+tantcl0ent%/u+r/l01/oracle/22.2/cl0entS5/l01

8.

Rebu#ld PHP(
R maNe R maNe 0n+tall

D.

o!" PHPLs su!!l#ed #n#$#al#1a$#on 5#le(


R cp php.0n0-recommended /u+r/local/l01/php.0n0

10. 3e$ an" re=u#red Oracle en'#ronmen$ 'ar#ables such as KDGK9BRARJG8AT$ and LKSGKALW. 3ee Setting Oracle Environment Dariables $or Apache #n $he cha!$er (onnecting to Oracle >sing O()*. 11. Res$ar$ $he &!ache HTTP 3er'er(
R /u+r/+10n/apachectl +tart

,p"radin" PHP 9ith PECL OCI0 on Linu&


The la$es$ O >8 !ac+age 5rom P@ . can be used $o u!grade O >8 #n a 5ull PHP source bundle Q #5 $he P@ . 'ers#on #s more recen$ $han $ha$ #ncluded #n PHP. U!grad#ng O >8 #s tron*ly recommended #5 "ou mus$ use PHP E. 7o$e PHP E #s no longer ma#n$a#ned b" $he PHP commun#$" and should no$ be used 5or ne* !ro8ec$s. O >8 1.P #s also more recen$ $han $he O >8 e/$ens#on #n PHP 5.2. Th#s sec$#on uses PHP E as $he e/am!le $arge$ PHP d#s$r#bu$#on :O >8 1.P *#ll bu#ld *#$h PHP E.P.D on*ards;. The #ns$ruc$#ons are e=u#'alen$ 5or o$her 'ers#ons o5 PHP.

,p"radin" OCI0 a' a Static Li)rar: on Linu&


>5 O >8 #s :or *#ll be; s$a$#call" l#n+ed *#$h PHP, PHP mus$ be rebu#l$ us#ng $he ne* e/$ens#on source code. To bu#ld and #ns$all PHP E on Oracle @n$er!r#se .#nu/( 1. 2. Do*nload php+0%0%?%tar%b.2 5rom http://www.php.net/download+.php. @/$rac$ $he 5#le(
R tar -Z<f php-5.5.>.tar.1Q7

>5 "ou do*nloaded $he %tar%g. 5#le, e/$rac$ #$ *#$h tar -Q<f. P.
7%

Dele$e $he de5aul$ oc#8 e/$ens#on(

/"grading PHP with P'(, O(I on ,inux R rm -rf php-5.5.>/e<t/oc0M

>5 "ou onl" rename $he oci* d#rec$or" $he con$igure scr#!$ *#ll no$ be !ro!erl" crea$ed. E. 5. Do*nload $he la$es$ O >8 e/$ens#on 5rom http://pecl.php.net/pacNage/oc0M. @/$rac$ $he 5#les and mo'e $he d#rec$or" $o ext<oci*(
R tar -Q<f oc0M-2.).5.tgQ R m/ oc0M-2.).5 php-5.5.>/e<t/oc0M

F.

Rebu#ld $he con5#gura$#on scr#!$ *#$h $he u!da$ed e/$ens#onLs o!$#ons(


R cd php-5.5.> R rm [rf conf0gure conf0g.cache autom5te.conf R ./1u0ldconf [-force

PHP !re5ers older 'ers#ons o5 $he o!era$#ng s"s$em bu#ld $ools. -e5ore runn#ng 1u0ldconf, "ou m#gh$ need $o #ns$all $he autoconf72) !ac+age. >5 "ou ha'e bo$h old and ne* !ac+ages #ns$alled, la$er 'ers#ons o5 PHP can be 5orced $o use $he a!!ro!r#a$e 'ers#on, 5or e/am!le *#$h(
e<port 8$8GAUTBCBL*-autoconf-7.2) e<port 8$8GAUTB$EADER-autoheader-7.2)

C.

on5#gure and bu#ld PHP 5ollo*#ng s$e! E on*ard #n $he !re'#ous sec$#on )nstalling O()* >sing a 3ocal Database or, #5 "ou #n$end $o use Oracle >ns$an$ l#en$, b" 5ollo*#ng $he s$e! E on*ards #n $he sec$#on )nstalling O()* >sing Oracle )nstant (lient.

,p"radin" OCI0 on Linu& ,'in" the PECL Channe%


PHPGs P@ . !ac+ag#ng s"s$em can also be used $o #ns$all or u!grade O >8 as a shared l#brar". These s$e!s can also be used $o add O >8 $o $he de5aul$ PHP $ha$ Oracle @n$er!r#se .#nu/ comes *#$h. The php+pear and php+devel RPM !ac+ages are re=u#red $o ge$ $he de5aul$ pear, pecl and phpi.e e/ecu$ables on @n$er!r#se .#nu/. To #ns$all( 1. 3hu$do*n &!ache(
R /u+r/+10n/apachectl +top

2.

Remo'e an" e/#s$#ng O >8 e/$ens#on(


R pecl un0n+tall oc0M

P.

>5 "ou are beh#nd a 5#re*all, se$ $he !ro/", 5or e/am!le(
R pear conf0g-+et httpGpro<y http://e<ample.com:M?/

E.

Do*nload and #ns$all O >8(


R pecl 0n+tall oc0M

71

In#ta&&ing PHP

Res!ond $o $he !rom!$ as #5 #$ *ere a con5#gure [-w0th-oc0M o!$#on. >5 "ou ha'e a local da$abase, $"!e $he 5ull !a$h $o $he so5$*are loca$#on(
/u+r/l01/oracle/<e/app/oracle/product/2?.7.?/+er/er

O$her*#se #5 "ou ha'e Oracle >ns$an$ l#en$ 11.1.0.C RPMs, $"!e(


0n+tantcl0ent%/u+r/l01/oracle/22.2/cl0ent/l01

On FE b#$ .#nu/ *#$h >ns$an$ l#en$ RPMs $he l#ne *ould be


0n+tantcl0ent%/u+r/l01/oracle/22.2/cl0entS5/l01

5.

@d#$ php%ini and add(


e<ten+0on - oc0M.+o

>5 e<ten+0onGd0r #s no$ se$, se$ #$ $o $he d#rec$or" *here oc0M.+o *as #ns$alled, 5or e/am!le(
e<ten+0onGd0r - /u+r/l01/php/module+

F. C.

3e$ an" re=u#red Oracle en'#ronmen$ 'ar#ables such as KDGK9BRARJG8AT$ and LKSGKALW. 3ee Setting Oracle Environment Dariables $or Apache #n $he cha!$er (onnecting to Oracle >sing O()*. Res$ar$ &!ache(
R /u+r/+10n/apachectl +tart

,p"radin" OCI0 a' a Shared Li)rar: on Linu&


These s$e!s are $he manual e=u#'alen$ $o $he !re'#ous pecl 0n+tall oc0M command. To #ns$all O >8 on an e/#s$#ng PHP #ns$alla$#on as a shared l#brar"( 1. 3hu$do*n &!ache(
R /u+r/+10n/apachectl +top

2.

>5 O >8 *as !re'#ousl" #ns$alled, bac+u! or remo'e $he oci*%so 5#le
R rm /u+r/l01/php/module+/oc0M.+o

P. E.

Do*nload $he O >8 1.P.E e/$ens#on 5rom P@ ., http://pecl.php.net/pacNage/oc0M @/$rac$ and !re!are $he ne* code(
R tar -Q<f oc0M-2.).5.tgQ R cd oc0M-2.).5 R php0Qe

5.

on5#gure O >8. >5 "ou ha'e a local da$abase, use(


R ./conf0gure [-w0th-oc0M-+hared%,BRACKEG$BOE

72

/"grading PHP with P'(, O(I on ,inux

O$her*#se #5 "ou ha'e Oracle >ns$an$ l#en$ 11.1.0.C use(


R " ./conf0gure [-w0th-oc0M-E +hared%0n+tantcl0ent%/u+r/l01/oracle/22.2/cl0ent/l01

F.

-u#ld $he shared l#brar"(


R maNe

C.

>ns$all $he l#brar"(


R cd oc0M-2.).5 R maNe 0n+tall

8.

@d#$ <etc<php%ini and add $h#s l#ne(


e<ten+0on - oc0M.+o

D.

>5 e<ten+0onGd0r #s no$ se$, se$ #$ $o $he d#rec$or" *here oci*%so *as #ns$alled, 5or e/am!le(
e<ten+0onGd0r - /u+r/l01/php/module+

10. 3e$ an" re=u#red Oracle en'#ronmen$ 'ar#ables such as KDGK9BRARJG8AT$ and LKSGKALW. 3ee Setting Oracle Environment Dariables $or Apache #n $he cha!$er (onnecting to Oracle >sing O()*. 11. Res$ar$ &!ache(
R /u+r/+10n/apachectl +tart

In'ta%%in" PHP With OCI0 on Windo9'


Th#s sec$#on d#scusses $he PHP 0#ndo*s M3> #ns$aller. PHP #s also #ns$allable 5rom 9>P 5#les. Re5er $o $he PHP documen$a$#on 5or $hose s$e!s. To #ns$all O >8 on 0#ndo*s, $he Oracle 10g or 11g cl#en$ l#brar#es are re=u#red. These can be 5rom a local da$abase, or 5rom Oracle >ns$an$ l#en$. &!ache #s also re=u#red. PHP b#nar#es 5or 0#ndo*s come #n $hread?sa5e and non? $hread?sa5e bundles. The non? $hread?sa5e 'ers#on re=u#res $he *eb ser'er $o be #n a non?$hreaded mode.

In'ta%%in" OCI0 ,'in" a Loca% Data)a'e on Windo9'


To #ns$all O >8 on 0#ndo*s NP Pro5ess#onal @d#$#on( 1. 2. P. E. 5. Do*nload $he PHP 5.2.C #ns$aller !ac+age 5rom http://www.php.net/download+.php. Double cl#c+ on $he do*nloaded 5#le php+5.2.7+&in'2+installer%msi. The PHP 0#ndo*s #ns$aller s$ar$s. l#c+ Ne"t on $he 0elcome screen. The l#cense agreemen$ #s d#s!la"ed. &cce!$ $he l#cense agreemen$ b" chec+#ng $he bo/ and cl#c+ Ne"t. 3elec$ $he #ns$alla$#on loca$#on o5 $he PHP 5#les and cl#c+ Ne"t.
73

In#ta&&ing PHP

F. C.

>n $he 0eb 3er'er 3e$u! screen, selec$ $he *eb ser'er "ou *an$ $o use. Th#s #ns$alla$#on uses $he &!ache 2.0 module o!$#on. 3elec$ -$a#'e 2.0." Mo.&le and cl#c+ Ne"t. >n $he &!ache on5#gura$#on D#rec$or" screen, selec$ $he loca$#on o5 $he &!ache con$ d#rec$or" and cl#c+ Ne"t. 2or &!ache 2.0, $he s$andard con5#gura$#on d#rec$or" #s ("FProgram ilesFApacheGroupFApache2F con$F >n $he hoose >$ems $o >ns$all screen, scroll do*n $o $he E"ten ,on branch and add $he Ora#le 8 e/$ens#on $o $he #ns$alla$#on. l#c+ Ne"t. >n $he Read" $o >ns$all PHP 5.2.C screen, cl#c+ In tall. The #ns$aller #ns$alls PHP.

8. D.

10. & d#alog #s d#s!la"ed as+#ng *he$her "ou *an$ $o con5#gure &!ache. l#c+ Ye . 11. & con5#rma$#on message #s d#s!la"ed $o con5#rm $ha$ $he httpd%con$ 5#le has been u!da$ed. l#c+ O/. 12. & con5#rma$#on message #s d#s!la"ed $o con5#rm $ha$ $he mime%types 5#le has been u!da$ed. l#c+ O/. 1P. & 5#nal con5#rma$#on message #s d#s!la"ed $o con5#rm $ha$ $he PHP #ns$alla$#on has com!le$ed. l#c+ +,n, '.

1E. Res$ar$ &!ache *#$h Start S Pro*ra! S -$a#'e HTTP Ser0er 2.0.5: S Control -$a#'e Ser0er S Re tart. Th#s o!ens a console *#ndo* sho*#ng an" error messages. @rrors ma" also be recorded #n ("FProgram ilesFApache GroupFApache2FlogsFerror%log% >5 "ou ha'e errors, double chec+ "our httpd%con$ and php%ini 5#les, and correc$ an" !roblems.

In'ta%%in" OCI0 9ith In'tant C%ient on Windo9'


&5$er #ns$all#ng $he &!ache HTTP 3er'er, "ou can #ns$all Oracle >ns$an$ l#en$ and con5#gure PHP $o connec$ $o a remo$e Oracle da$abase. To #ns$all( 1. Do*nload $he >ns$an$ l#en$ -as#c !ac+age 5or 0#ndo*s 5rom $he >ns$an$ l#en$ !age on $he Oracle Technolog" 7e$*or+(
http://www.oracle.com/technology/tech/oc0/0n+tantcl0ent/0n+tantcl0ent.html

The 0#ndo*s P2 b#$ 9>P 5#le #s called instantclient+basic+&in'2+!!%!%2%4%2%.ip and #s around E2 M- #n s#1e. >5 "ou need $o connec$ $o Oracle 8i, $hen #ns$all $he Oracle 10g >ns$an$ l#en$. On some 0#ndo*s #ns$alla$#ons, $o use Oracle 10g >ns$an$ l#en$ "ou ma" need $o loca$e a co!" o5 msvcr-!%dll and !u$ #$ #n "our 8AT$. 2. P. rea$e a ne* d#rec$or" :5or e/am!le, ("Finstantclient8!!8!;. Un1#! $he do*nloaded 5#le #n$o $he ne* d#rec$or". @d#$ $he 0#ndo*s en'#ronmen$ and add $he loca$#on o5 $he Oracle >ns$an$ l#en$ 5#les, ("Finstantclient8!!8!, $o $he 8AT$ en'#ronmen$ 'ar#able, be5ore an" o$her Oracle d#rec$or#es. 2or e/am!le, on 0#ndo*s NP, use Start > Sett,n* > Control Panel > Sy te! > -.0an#e. > En0,ron!ent Var,a)le ; and ed#$ 8AT$ #n $he 3"s$em ,ar#ables l#s$.

74

In#ta&&ing PHP With O(I on Window#

>5 "ou are us#ng a tnsnames%ora 5#le $o de5#ne Oracle 7e$ connec$ names :da$abase al#ases;, co!" $he tnsnames%ora 5#le $o ("Finstantclient8!!8!7 and se$ $he user en'#ronmen$ 'ar#able TLSGADO9L $o ("Finstantclient8!!8!. 3e$ an" o$her re=u#red Oracle global#1a$#on language en'#ronmen$ 'ar#ables, such as LKSGKALW. >5 no$h#ng #s se$, $he de5aul$ local en'#ronmen$ #s used. 3ee $he Globali.ation cha!$er, 5or more #n5orma$#on on global#1a$#on *#$h PHP and Oracle. Unse$ an" Oracle en'#ronmen$ 'ar#ables $ha$ are no$ re=u#red, such as BRACKEG$BOE and BRACKEGS9D. E. 5. >ns$all PHP 5ollo*#ng $he s$e!s #n $he !re'#ous sec$#on, )nstalling O()* >sing a 3ocal Database on /indo&s. Res$ar$ $he &!ache HTTP 3er'er.

,p"radin" OCI0 on Windo9'

The pecl7win site mentioned here is no lon&er maintained and the steps no lon&er apply to recent versions of #)#. Current alternatives are to up&rade your complete #)# distribution, or build #)# yourself. The Windows build team in the #)# community is wor!in& on a replacement for pecl7win. This section has been retained for reference.

To #ns$all PHP E on 0#ndo*s NP Pro5ess#onal @d#$#on( 1. 2. P. E. Do*nload $he PHP E.E.D 9>P 5#le 5rom http://www.php.net/download+.php. Uncom!ress php+0%0%?+/in'2%.ip $o a d#rec$or" called ("Fphp+0%0%?+/in'2. Dele$e $he old O >8 e/$ens#on 5#le ("Fphp+0%0%?+/in'2FextensionsFphp8oci*%dll% Do*nload php8oci*%dll 5or $he E.E branch 5rom h$$!(II!eclE*#n.!h!.ne$Ie/$.!h!I!h!Joc#8.dll and mo'e #$ $o ("Fphp+0%0%?-Win'2FextensionsFphp8oci*%dll%

Note: The +C*4 ,99s for Windows re:uire the +racle -.g or --g Client libraries.

5. F. C.

o!" ("Fphp+0%0%?+/in'2Fphp0ts%dll #n$o $he ("Fphp+0%0%?+/in'2Fsapi d#rec$or". >5 "ou run $he PHP command l#ne #n$er5ace co!" #$ $o $he ("Fphp+0%0%?+/in'2Fcli d#rec$or" $oo. o!" php%ini+recommended $o ("FProgram ilesFApache GroupFApache2Fcon$Fphp%ini. @d#$ php%ini and !er5orm $he 5ollo*#ng(

hange e<ten+0onGd0r $o C:Ephp-5.5.>-&0n)7Ee<ten+0on+, *h#ch #s $he d#rec$or" con$a#n#ng php8oci*%dll and $he o$her PHP e/$ens#ons. Uncommen$ :remo'e $he sem#colon 5rom $he beg#nn#ng o5 $he l#ne; $he o!$#on e<ten+0on-phpGoc0M.dll.
75

In#ta&&ing PHP

8. D.

2or $es$#ng, #$ #s hel!5ul $o se$ d0+playGerror+ $o Bn7 so "ou see an" !roblems #n "our code. Ma+e sure $o unse$ #$ *hen "our a!!l#ca$#on #s released. @d#$ $he 5#le httpd%con$ and add $he 5ollo*#ng l#nes. Ma+e sure $o use 5or*ard slashes / #ns$ead o5 bac+ slashes E(
R R Th0+ w0ll load the 8$8 module 0nto Apache R KoadOodule php5Gmodule C:/php-5.5.>-&0n)7/+ap0/php5apache7.dll R R Th0+ +ect0on w0ll call 8$8 for .php% .phtml% and .php+ f0le+ R AddType appl0cat0on/<-httpd-php .php AddType appl0cat0on/<-httpd-php .phtml AddType appl0cat0on/<-httpd-php-+ource .php+ R R Th0+ 0+ the d0rectory conta0n0ng php.0n0 R 8$89n0D0r .C:/8rogram *0le+/Apache Wroup/Apache7/conf.

10. Res$ar$ &!ache.

In'ta%%in" OCI0 9ith Orac%e App%ication Ser.er on Linu&


Oracle #ncludes PHP *#$h #$s m#d?$#er &!!l#ca$#on 3er'er 10g Release P allo*#ng "ou $o use $he same *eb ser'er 5or PHP and 5or )2@@ a!!l#ca$#ons. PHP #s enabled b" de5aul$. The Oracle HTTP 3er'er documen$ roo$ #s
,BRACKEG$BOE/Apache/Apache/htdoc+

:Hes, Apache #s re!ea$ed $*#ce;. 2#les *#$h %php or %phtml e/$ens#ons #n $h#s d#rec$or" *#ll be e/ecu$ed b" PHP. 2#les *#$h a %phps e/$ens#on *#ll be d#s!la"ed as 5orma$$ed source code. ,ers#on 10.1.P.0 o5 $he &!!l#ca$#on 3er'er :&3; comes *#$h PHP E.P.11. The &3 10.1.P.2 !a$chse$ adds PHP 5.1.2. >5 "ou ha'e a s$rong need $o use a d#55eren$ 'ers#on o5 PHP *#$hou$ #ns$all#ng a ne* *eb ser'er, "ou ma" be able $o com!#le "our o*n PHP release us#ng $hese s$e!s.

Note: Chan&in& the version of #)# in A; is not supported $and hence is not recommended( but is technically possible in some circumstances. <or any A; support calls, re&ardless of whether they are #)# related, +racle ;upport will as! you to revert the chan&es before be&innin& investi&ation.

The $echn#cal !roblem 5aced *#$h bu#ld#ng PHP #s $ha$ $he Oracle l#brar#es 5or &3 do no$ #nclude header 5#les. Th#s can be o'ercome b" l#n+#ng PHP *#$h Oracle >ns$an$ l#en$ bu$ care needs $o be $a+en so $ha$ &3 #$sel5 does no$ use $he >ns$an$ l#en$ l#brar#es. O$her*#se "ou *#ll ge$ errors or un!red#c$able beha'#or. These s$e!s are 'er" 'ers#on and !la$5orm s!ec#5#c. The" ma" no$ be $echn#call" 5eas#ble #n all de!lo"men$s o5 &3.

76

In#ta&&ing O(I with Orac&e 0""&ication Server on ,inux

& !re'#ous #ns$alla$#on o5 &3 10.1.P #s assumed. To #ns$all a ne* 'ers#on o5 PHP( 1. .og on as $he oracle user and change $o $he home d#rec$or"(
, cd ,$BOE

2.

Do*nload $he Oracle 10g or 11g 8a ,# and S(/ >ns$an$ l#en$ 9>P !ac+ages 5rom $he >ns$an$ l#en$ !age on $he Oracle Technolog" 7e$*or+(
http://www.oracle.com/technology/tech/oc0/0n+tantcl0ent/0n+tantcl0ent.html

P.

@/$rac$ $he 9>P 5#les(


, unQ0p 0n+tantcl0ent-1a+0c-l0nu<)7-2?.7.?.)-7??S2226.Q0p , unQ0p 0n+tantcl0ent-+dN-l0nu<)7-2?.7.?.)-7??S2226.Q0p

E.

hange $o $he >ns$an$ l#en$ d#rec$or" and s"mbol#call" l#n+ libclntsh%so%!2%! $o libclntsh%so(
, cd 0n+tantcl0entG2?G7 , ln -+ l01clnt+h.+o.2?.2 l01clnt+h.+o

The >ns$an$ l#en$ RPMs could also be used, #n *h#ch case $h#s las$ s$e! #s unnecessar". -e *ar" o5 ha'#ng >ns$an$ l#en$ in <etc<ld%so%con$ s#nce >ns$an$ l#en$ l#brar#es can cause con5l#c$s *#$h &3. The opmnctl $ool ma" 5a#l *#$h $he error Oa0n: LKS 9n0t0al0Qat0on *a0led''. 5. Do*nload PHP 5.2.C 5rom http://www.php.net/download+.php and e/$rac$ $he 5#le(
, tar -Z<f php-6.7.T.tar.1Q7

F.

3e$ $he BRACKEG$BOE en'#ronmen$ 'ar#able $o "our &3 #ns$all d#rec$or"(


, e<port BRACKEG$BOE-,$BOE/product/2?.2.)/BracleASG2

C.

3hu$ do*n $he &!ache HTTP 3er'er(


, ,BRACKEG$BOE/opmn/10n/opmnctl +topproc 0a+-component-$TT8GSer/er

8.

@d#$ VOR& .@JHOM@I&!acheI&!acheIcon5Ih$$!d.con5 and commen$ ou$ $he PHP E 3oad6odule l#ne b" !re5#/#ng #$ *#$h R(
RKoadOodule php5Gmodule l01e<ec/l01php5.+o

>5 "ou had enabled PHP 5 5or &3 10.1.P.2, $he commen$ed l#ne *#ll be(
RKoadOodule php6Gmodule l01e<ec/l01php6.+o

D.

-ac+ u! EORA(3E8HO6E<Apache<Apache<libexec<libphp,%so s#nce #$ *#ll be re!laced.

10. 3e$ en'#ronmen$ 'ar#ables re=u#red 5or $he bu#ld $o com!le$e(


, e<port 8ERK6K9B-,BRACKEG$BOE/perl/l01 , e<port KDGK9BRARJG8AT$-,BRACKEG$BOE/l01:,KDGK9BRARJG8AT$ , e<port C*KAWS--DK9LUI 77

In#ta&&ing PHP

There #s no need $o se$ ( 3AGS #5 "ou ha'e &3 10.1.P.2. >$ #s needed *#$h &3 10.1.P.0 $o a'o#d a du!l#ca$e !ro$o$"!e error *#$h gethostname9: $ha$ resul$s #n com!#la$#on 5a#lure. 11. on5#gure PHP(
, cd php-6.7.T , ./conf0gure E " --pref0<-,BRACKEG$BOE/php E " --w0th-conf0g-f0le-path-,BRACKEG$BOE/Apache/Apache/conf E " --w0th-ap<+-,BRACKEG$BOE/Apache/Apache/10n/ap<+ E " --w0th-oc0M-0n+tantcl0ent%,$BOE/0n+tantcl0entG2?G7 E " --ena1le-+0gch0ld

0#$h $he older &3 10.1.2 and older >ns$an$ l#en$ releases, some users re!or$edl" also s!ec#5#ed --d0+a1le-rpath. 12. Ma+e and #ns$all PHP
, maNe , maNe 0n+tall

The #ns$alla$#on co!#es $he b#nar#es and u!da$es EORA(3E8HO6E<Apache<Apache<con$<httpd%con$, au$oma$#call" add#ng $he l#ne(
KoadOodule php6Gmodule l01e<ec/l01php6.+o

1P. -ac+ u! and u!da$e EORA(3E8HO6EApache<Apache<con$<php%ini *#$h o!$#ons 5or PHP 5.2.C, 5or e/am!le all $he ne* oc#8 d#rec$#'es. Re5er $o EHO6E<php+,%2%-<php%ini+recommended 5or ne* o!$#ons. 1E. The Oracle HTTP 3er'er can no* be res$ar$ed(
, ,BRACKEG$BOE/opmn/10n/opmnctl +tartproc 0a+-component-$TT8GSer/er

/eminder: these steps invalidates all support for A;, not =ust for the #)# component, and they should not be used in production environments.

In'ta%%in" PHP With PDO


To use PDO *#$h Oracle, #ns$all $he PDO e/$ens#on and $he PDOJO > da$abase dr#'er 5or Oracle. The PDO e/$ens#on and dr#'ers are #ncluded #n PHP 5rom release 5.1. The #ns$alla$#on sho*n uses PHP 5.2.C, &!ache 2.0.5D, Oracle @n$er!r#se .#nu/, and 0#ndo*s NP Pro5ess#onal @d#$#on. Th#s !rocedure *or+s 5or all 'ers#ons o5 PHP a5$er release 5.1. Hou can #ns$all PDOJO > and O >8 a$ $he same $#me b" comb#n#ng $he a!!ro!r#a$e o!$#ons $o conf0gure or b" selec$#ng bo$h e/$ens#ons dur#ng #ns$alla$#on on 0#ndo*s.

In#ta&&ing PHP With P)O

In'ta%%in" PDO on Linu&


To #ns$all PDO on Oracle @n$er!r#se .#nu/( 1. 2. Do*nload PHP 5.2.C 5rom http://www.php.net/download+.php. .og #n as $he root user and e/$rac$ $he PHP source code us#ng $he 5ollo*#ng commands(
R tar -Z<f php-6.7.T.tar.1Q7 R cd php-6.7.T

>5 "ou do*nloaded $he %tar%g. 5#le, e/$rac$ #$ *#$h tar -Q<f. P. on5#gure PHP(
R e<port BRACKEG$BOE-/u+r/l01/oracle/<e/app/oracle/product/2?.7.?/+er/er R ./conf0gure E " --w0th-ap<+7-/u+r/+10n/ap<+ E " --ena1le-pdo E " --w0th-pdo-oc0-,BRACKEG$BOE E " --ena1le-+0gch0ld

7o$e $here #s no M8O #n $he [-w0th-pdo-oc0 o!$#on name. Re'#e* $he ou$!u$ o5 conf0gure and chec+ $ha$ $he e/$ens#on *as enabled success5ull" be5ore con$#nu#ng. >5 "ou *an$ $o bu#ld PDOJO > *#$h $he Oracle >ns$an$ l#en$ RPMs, change $he [-w0th-pdo-oc0 o!$#on $o(
--w0th-pdo-oc0-0n+tantcl0ent%/u+r%22.2

Th#s #nd#ca$es $o use $he >ns$an$ l#en$ #n <usr<lib<oracle<!!%!. 7o$e $he Oracle 11.1.0.F >ns$an$ l#en$ RPMs ha'e 11.1.0.1 #n $he !ac+age name and #ns$alla$#on !a$h. >5 >ns$an$ l#en$ 9>P 5#les are used, $he o!$#on should be(
--w0th-pdo-oc0-0n+tantcl0ent%,$BOE/0n+tantcl0entG22G2%22.2

The $ra#l#ng 'ers#on number #n $h#s command 5or 9>P #ns$alls #s onl" used 5or san#$" chec+#ng and d#s!la" !ur!oses. 0#$h >ns$an$ l#en$ 9>P 5#les, #5 $he error
9#m too dum1 to f0gure out where the l01rar0e+ are 0n your 9n+tant Cl0ent 0n+tall

#s sho*n #$ means $he s"mbol#c l#n+ 5rom libclntsh%so $o libclntsh%so%!!%! :or $he a!!ro!r#a$e 'ers#on; #s m#ss#ng. Th#s l#n+ mus$ be manuall" crea$ed unless $he RPM 5#les are used. E. Ma+e and #ns$all PHP.
R maNe R maNe 0n+tall

5.

on$#nue 5rom s$e! F o5 $he !re'#ous .#nu/ sec$#on )nstalling O()* >sing a 3ocal Database $o crea$e $he
7$

In#ta&&ing PHP

php%ini scr#!$ and con5#gure &!ache.

In'ta%%in" PDO on Windo9'


To #ns$all PDO on 0#ndo*s NP Pro5ess#onal @d#$#on( 1. 2. P. >ns$all an Oracle l#en$ or Oracle Da$abase. 3ee $he sec$#on )nstalling O()* /ith Oracle )nstant (lient 5or s$e!s on #ns$all#ng Oracle >ns$an$ l#en$. 2ollo* $he s$e!s #n )nstalling O()* on /indo&s. &$ s$e! 8, add $he PDO e/$ens#on b" also selec$#ng $o #ns$all PHP S E"ten ,on S P(O S Ora#le10* Cl,ent an. a)o0e >n PHP 5.2.F and earl#er, $he o!$#on #s called Ora#le 8, <.

igure ,-" PDO )nstallation on /indo&s% >n PHP 5.2.F and earl#er "ou can selec$ $he Oracle C o!$#on #5 "ou ha'e Oracle 8i or Di cl#en$ l#brar#es. Th#s cho#ce #s no$ a'a#lable #n PHP 5.2.C on*ards. E. 5. on$#nue *#$h $he res$ o5 $he PHP #ns$alla$#on. Res$ar$ $he &!ache HTTP 3er'er.

The Oracle PDOJO > e/$ens#on #s no* #ns$alled and con5#gured *#$h PHP.

Chec>in" OCI0 and PDOAOCI In'ta%%ation


To con5#rm PHP *as #ns$alled correc$l", crea$e a scr#!$ $ha$ sho*s $he PHP con5#gura$#on se$$#ngs. The 5#le should be #n a d#rec$or" &!ache can read, such as $he documen$ roo$, s!ec#5#ed b" $he DocumentRoot se$$#ng #n $he httpd%con$ 5#le. On Oracle @n$er!r#se .#nu/ $he d#rec$or" #s <var<&&&<html.
%

(hec3ing O(I and P)O4O(I In#ta&&ation

Script 0" phpin$o%php


!php php0nfo:;( !"

.oad $he scr#!$ #n "our bro*ser(


http://localho+t/php0nfo.php

&l$erna$#'el", $he scr#!$ can be run #n a $erm#nal *#ndo* *#$h command?l#ne PHP(
, php php0nfo.php

>n $he ou$!u$, chec+ $he 3oaded (on$iguration ile en$r" sho*s $he php%ini $ha$ $he !re'#ous #ns$alla$#on s$e!s se$ u!. The @n'#ronmen$ sec$#on should sho* $he Oracle en'#ronmen$ 'ar#ables. 3ee Setting Oracle Environment Dariables $or Apache #n $he cha!$er (onnecting to Oracle >sing O()*. >5 O >8 *as #ns$alled, $here *#ll be a sec$#on 5or #$. The !arame$er 'alues are d#scussed #n la$er cha!$ers(

igure ,*" phpin$o9: output &hen O()* is enabled% >5 PDOJO > *as #ns$alled, #$s con5#gura$#on sec$#on *#ll loo+ l#+e(

In#ta&&ing PHP

igure ,?" phpin$o9: &hen PDO8O() is enabled%

CHAPTER 0

INSTALLIN! 1END CORE =OR ORACLE


Th#s ha!$er sho*s "ou ho* $o #ns$all 9end ore 5or Oracle Release 2.5, a 5ull" $es$ed and su!!or$ed PHP d#s$r#bu$#on $ha$ #ncludes #n$egra$#on *#$h Oracle Da$abase 10g cl#en$ l#brar#es. >$ #s a !re?bu#l$ s$ac+ $ha$ ma+es #$ eas#er $o ge$ s$ar$ed *#$h PHP and Oracle, as all $he hard *or+ o5 #ns$alla$#on and con5#gura$#on has been done 5or "ou. 9end ore 5or Oracle release 2.5 #ncludes PHP 5.2.5, $he re5ac$ored O >8 dr#'er, Oracle >ns$an$ l#en$, and an o!$#onal &!ache HTTP 3er'er 2.2.E. The collabora$#on be$*een Oracle and 9end re#n5orces OracleLs comm#$men$ $o $he o!en source PHP commun#$". 9end ore 5or Oracle #s su!!or$ed b" 9end on $he 5ollo*#ng o!era$#ng s"s$ems(

Oracle @n$er!r#se .#nu/ .#nu/ 3.@3 ,D W ,10 on /8F, /8F?FE .#nu/ RH@. ,E W ,5 on /8F, /8F?FE 0#ndo*s NP W 200P :P2 b#$;, ,#s$a :P2 W FE b#$; 3olar#s ,8, ,D W ,10 on 3!arc

The *eb ser'ers $ha$ are su!!or$ed are(


&!ache 1.P./ &!ache 2.0./ :com!#led #n !re5or+ mode onl"; &!ache 2.2./ :com!#led #n !re5or+ mode onl"; >>3 5, F, C Oracle HTTP 3er'er

9end ore 5or Oracle #s su!!or$ed aga#ns$ Oracle Da$abase 11g, 10g and Di. Tha$ means $ha$ "ou can ha'e a 5ull" su!!or$ed s$ac+ o5 da$abase, *eb ser'er and PHP.

In'ta%%in" 1end Core 2or Orac%e


Th#s !rocedure #ns$alls 9end ore 5or Oracle on .#nu/ and 0#ndo*s !la$5orms. The !rocedure $o #ns$all on o$her o!era$#ng s"s$ems #s s#m#lar as $he #ns$aller #s $he same on all o!era$#ng s"s$ems. There are some sl#gh$ d#55erences, l#+e $he names and loca$#ons o5 $he *eb ser'er, bu$ $he #ns$alla$#on #s s#m#lar on all !la$5orms. 3o "ou can use $h#s !rocedure on all su!!or$ed o!era$#ng s"s$ems.

In'ta%%in" 1end Core 2or Orac%e on Linu&


To #ns$all 9end ore 5or Oracle on .#nu/( 1. Do*nload $he 9end ore 5or Oracle 5#le 5rom $he Oracle Technolog" 7e$*or+
3

In#ta&&ing 5end (ore .or Orac&e

:http://otn.oracle.com/php;. 2. .og #n or +u as root #5 "ou are no$ alread".


, +u 8a++word:

P.

@/$rac$ $he con$en$s o5 $he do*nloaded 9end ore 5or Oracle so5$*are 5#le. Th#s e/am!le uses $he .#nu/ FE?b#$ #ns$all. >5 "ou are us#ng $he P2?b#$ #ns$all, $he 5#le name ma" 'ar" sl#gh$l".
R tar -Q<f ]endCore*orBracle-7.6.?-l0nu<-gl01c7)-amdS5.tar.gQ

2#les are e/$rac$ed $o a subd#rec$or" called 1end(ore orOracle+2%,%2+linux+glibc2'+amd40. E. hange d#rec$or" $o 1end(ore orOracle+2%,%2+linux+glibc2'+amd40 and s$ar$ $he 9end ore 5or Oracle #ns$alla$#on(
R cd ]endCore*orBracle-7.6.?-l0nu<-gl01c7)-amdS5 R ./0n+tall -g

The 9end ore 5or Oracle >ns$alla$#on screen #s d#s!la"ed.

igure 42" 1end (ore $or Oracle )nstallation screen% 5. l#c+ O/. The 9end ore 5or Oracle l#cense agreemen$ #s d#s!la"ed.

In#ta&&ing 5end (ore .or Orac&e

igure 4!" 1end (ore $or Oracle 3icense Agreement screen% F. l#c+ Ye . The 9end ore 5or Oracle #ns$alla$#on loca$#on screen #s d#s!la"ed.

igure 42" 1end (ore $or Oracle installation location screen% C. 3!ec#5" $he loca$#on 5or #ns$all#ng 9end ore 5or Oracle( acce!$ $he de5aul$< or en$er "our !re5erred loca$#on. l#c+ O/. The 9end ore 5or Oracle !ass*ord screen #s d#s!la"ed.

In#ta&&ing 5end (ore .or Orac&e

igure 4'" 1end (ore $or Oracle pass&ord screen% 8. @n$er a !ass*ord 5or $he 9end ore 5or Oracle &dm#n#s$ra$#on onsole. Th#s #s $he !ass*ord 5or $he *eb?based #n$er5ace $o 9end ore 5or Oracle. on5#rm $he !ass*ord and cl#c+ O/. The 9end ore 5or Oracle #ns$alla$#on o!$#ons screen #s d#s!la"ed.

igure 40" 1end (ore $or Oracle installation options screen% D. 3elec$ $he 9end ore 5or Oracle com!onen$s "ou *an$ $o #ns$all. 0e sugges$ "ou selec$ Con5,*&re -$a#'e 9e) er0er, and In tall Ora#le Cl,ent. Hou ma" o!$#onall" en$er "our 9end ne$*or+ user >D and !ass*ord $o be able $o use $he 9end ore onsole $o $rac+ *hen u!da$es $o 9end ore and PHP com!onen$s are a'a#lable. >5 "ou ha'e no$ reg#s$ered, or do no$ *an$ $o $rac+ u!da$es, ma+e sure $he Con5,*&re =en. Net6or4 #s no$ selec$ed. l#c+ O/. The 9end ore 5or Oracle selec$ *eb ser'er screen #s d#s!la"ed.

In#ta&&ing 5end (ore .or Orac&e

igure 4," 1end (ore $or Oracle select &eb server screen% 10. 9end ore 5or Oracle can #ns$all &!ache #5 "ou donL$ ha'e #$ on "our s"s$em. >5 "ou *an$ $o #ns$all and con5#gure &!ache, selec$ In tall t'e =en. Core )&n.le. -$a#'e, or #5 "ou *an$ $o use an e/#s$#ng *eb ser'er, selec$ $he *eb ser'er 5rom $he l#s$. Th#s #ns$alla$#on assumes $ha$ "ou ha'e selec$ed $o use an e/#s$#ng &!ache ser'er alread" #ns$alled on "our mach#ne. l#c+ O/. The 9end ore 5or Oracle '#r$ual hos$ selec$#on screen. screen #s d#s!la"ed.

igure 44" 1end (ore $or Oracle virtual host selection screen% 11. 3elec$ Con5,*&re =en. Core on t'e !a,n 'o t, and cl#c+ O/. The 9end ore 5or Oracle &!ache con5#gura$#on de$a#ls screen #s d#s!la"ed.

In#ta&&ing 5end (ore .or Orac&e

igure 4-" 1end (ore $or Oracle Apache con$iguration details screen% 12. on5#rm $he con5#gura$#on de$a#ls 5or "our &!ache #ns$alla$#on. l#c+ O/. The 9end ore 5or Oracle &!ache #ns$alla$#on me$hod screen #s d#s!la"ed.

igure 4*" 1end (ore $or Oracle Apache installation method screen% 1P. 3elec$ -$a#'e Mo.&le as $he #ns$alla$#on me$hod, and cl#c+ O/. Hou can also use $he +a tCGI o!$#on #5 "ou !re5er. The #ns$aller un!ac+s $he #ns$alla$#on 5#les and #ns$alls 9end ore 5or Oracle. The 9end ore 5or Oracle 5ur$her #ns$alla$#on o!$#ons screen #s d#s!la"ed.

In#ta&&ing 5end (ore .or Orac&e

igure 4?" 1end (ore $or Oracle $urther installation options screen% 1E. 3elec$ +,n, ' ,n tallat,on and cl#c+ O/. The 9end ore 5or Oracle #ns$alla$#on success5ul screen #s d#s!la"ed.

igure -2" 1end (ore $or Oracle installation success$ul screen% 15. Th#s screen con$a#ns use5ul con5#gura$#on commands and $he UR. 5or $he 9end ore 5or Oracle &dm#n#s$ra$#on onsole. Ta+e no$e o5 $he #n5orma$#on. The 9end ore 5or Oracle #ns$alla$#on #s no* com!le$e.

Te'tin" the 1end Core 2or Orac%e In'ta%%ation on Linu&


To $es$ $he 9end ore 5or Oracle #ns$alla$#on on .#nu/ !la$5orms( 1. on5#gure &!ache $o use a !ubl#c '#r$ual d#rec$or". &s root, ed#$ APA(HE8HO6E<con$<httpd%con$ and add a commen$ $o $he 5ollo*#ng l#ne(
RU+erD0r .d0+a1le. $

In#ta&&ing 5end (ore .or Orac&e

2.

Then remo'e $he R 5rom $he 5ollo*#ng l#ne(


U+erD0r pu1l0cGhtml

P.

&s "our normal user :no$ root;, crea$e a d#rec$or" called public8html #n "our home d#rec$or", and change d#rec$or" $o $he ne*l" crea$ed d#rec$or", en$er $he 5ollo*#ng commands #n a command *#ndo*(
R cd ,$BOE R mNd0r pu1l0cGhtml R cd pu1l0cGhtml

E.

rea$e a 5#le called hello%php $ha$ con$a#ns $he 5ollo*#ng PHP code(
!php echo .$ello world'.( !"

5. F.

Ma+e sure $he !erm#ss#ons on an" 5#les #n $he public8html d#rec$or", and $he d#rec$or#es abo'e #$ are se$ $o T66 so $he *eb ser'er can read and e/ecu$e $hem. O!en a *eb bro*ser and en$er $he 5ollo*#ng UR. #n "our bro*ser(
http://27T.?.?.2/^username/hello.php

The l#ne $ello world' a!!ears #n $he bro*ser. &n" errors #n "our PHP code are d#s!la"ed as "ou ha'e con5#gured PHP $o d#s!la" errors.

In'ta%%in" 1end Core 2or Orac%e on Windo9'


To #ns$all 9end ore 5or Oracle on 0#ndo*s !la$5orms( 1. 2. P. Do*nload $he 9end ore 5or Oracle. There #s a l#n+ $o $he 9end s#$e a'a#lable on $he Oracle Technolog" 7e$*or+ a$ http://otn.oracle.com/php. .og #n as a s"s$em adm#n#s$ra$or, or a user *#$h s"s$em adm#n#s$ra$or !r#'#leges. Double cl#c+ on $he 5#le named 1end(ore$orOracle+2%,%2+/indo&s+x*4%exe $o s$ar$ $he 9end ore 5or Oracle #ns$alla$#on. The 9end ore 5or Oracle 0elcome screen #s d#s!la"ed.

$%

In#ta&&ing 5end (ore .or Orac&e

igure -!" 1end (ore $or Oracle /elcome screen% E. >n $he 9end ore 5or Oracle 0elcome screen, cl#c+ Ne"t. The 9end ore 5or Oracle .#cense &greemen$ screen #s d#s!la"ed.

igure -2" 1end (ore $or Oracle 3icense Agreement screen% 5. Read and acce!$ $he $erms o5 $he l#cense, and cl#c+ Ne"t. The 9end ore 5or Oracle 3e$u! T"!e screen #s d#s!la"ed.

$1

In#ta&&ing 5end (ore .or Orac&e

igure -'" 1end (ore $or Oracle Setup #ype screen% F. On 0#ndo*s !la$5orms, "ou can choose $o !er5orm a Co!$lete #ns$alla$#on, or a C& to! #ns$alla$#on. Th#s #ns$alla$#on !rocedure assumes "ou selec$ $he C& to! #ns$alla$#on. >5 "ou !re5er, selec$ $he Co!$lete #ns$alla$#on 5or $he de5aul$ #ns$all o!$#ons. l#c+ Ne"t. The 9end ore 5or Oracle hoose Des$#na$#on .oca$#on screen #s d#s!la"ed.

igure -0" 1end (ore $or Oracle (hoose Destination 3ocation screen% C. 3!ec#5" $he loca$#on 5or #ns$all#ng 9end ore 5or Oracle< acce!$ $he de5aul$< or en$er "our !re5erred loca$#on. l#c+ Ne"t. The 9end ore 5or Oracle 3elec$ 2ea$ure screen #s d#s!la"ed.

$2

In#ta&&ing 5end (ore .or Orac&e

igure -," 1end (ore $or Oracle Select eatures screen% 8. 3elec$ =en. Core PHP an. E"ten ,on , =en. Core -.!,n, trat,on, and o!$#onall" Ora#le In tant Cl,ent and =en. +ra!e6or4. l#c+ Ne"t. The 9end ore 5or Oracle 0eb 3er'er 3elec$#on screen #s d#s!la"ed.

igure -4" 1end (ore $or Oracle /eb Server Selection screen% D. >5 "ou ha'e an &!ache #ns$all alread", selec$ S$e#,5y -$a#'e> lo#at,on, or #5 "ou do no$ ha'e &!ache #ns$alled, selec$ In tall =en. Core? )&n.le. -$a#'e 2.2.@. l#c+ Ne"t. >5 "ou selec$ed $o #ns$all 9end oreGs bundled &!ache ser'er, $he 9end ore 5or Oracle &!ache !or$ number screen #s d#s!la"ed.

$3

In#ta&&ing 5end (ore .or Orac&e

igure --" 1end (ore $or Oracle Apache port number screen% 10. >5 "ou ha'e chosen $o use an e/#s$#ng &!ache #ns$all, selec$ $he &!ache roo$ 5older. >5 "ou ha'e chosen $o #ns$all a ne* &!ache ser'er, selec$ $he !or$ number "ou *an$ &!ache $o l#s$en on. l#c+ Ne"t. The 9end ore 5or Oracle @/$ens#on &ssoc#a$#on screen #s d#s!la"ed.

igure -*" 1end (ore $or Oracle Extension Association screen% 11. 3elec$ $he e/$ens#ons "ou *an$ $o assoc#a$e *#$h PHP 5rom $he chec+ bo/es and cl#c+ Ne"t. The 9end ore 5or Oracle &dm#n#s$ra$#on Pass*ord screen #s d#s!la"ed.

$4

In#ta&&ing 5end (ore .or Orac&e

igure -?" 1end (ore $or Oracle Administration Pass&ord screen% 12. @n$er a !ass*ord 5or $he &dm#n#s$ra$#on onsole. on5#rm $he !ass*ord and cl#c+ Ne"t. The 9end ore 5or Oracle 9end 7e$*or+ 3ubscr#!$#on screen #s d#s!la"ed.

igure *2" 1end (ore $or Oracle 1end Get&or5 Subscription screen% 1P. On $he 9end 7e$*or+ 3ubscr#!$#on screen "ou ma" o!$#onall" en$er "our 9end ne$*or+ user >D and !ass*ord $o be able $o use $he 9end ore onsole $o $rac+ *hen u!da$es $o 9end ore and PHP com!onen$s are a'a#lable. >5 "ou ha'e no$ reg#s$ered, or do no$ *an$ $o $rac+ u!da$es, selec$ No. l#c+ Ne"t. The 9end ore 5or Oracle Pro/" on5#gura$#on screen #s d#s!la"ed.

$5

In#ta&&ing 5end (ore .or Orac&e

igure *!" 1end (ore $or Oracle Proxy (on$iguration screen% 1E. >5 "ou use a !ro/" ser'er $o connec$ $o $he >n$erne$, selec$ Hes $o en$er $he $he !ro/" ser'er #n5orma$#on. O$her*#se, selec$ 7o. l#c+ 7e/$. The 9end ore 5or Oracle Read" $o >ns$all screen #s d#s!la"ed.

igure *2" 1end (ore $or Oracle Ready to )nstall screen% 15. l#c+ $he In tall bu$$on $o beg#n $he #ns$alla$#on. The #ns$aller runs $hrough $he #ns$alla$#on. The 9end ore 5or Oracle >ns$alla$#on om!le$e screen #s d#s!la"ed.

$6

In#ta&&ing 5end (ore .or Orac&e

igure *'" 1end (ore $or Oracle )nstallation (omplete screen% 1F. The 5#nal #ns$alla$#on !age #s d#s!la"ed con5#rm#ng $he #ns$alla$#on #s com!le$e. l#c+ +,n, '. 9end ore 5or Oracle #s #ns$alled. Hou ma" need $o res$ar$ 0#ndo*s #5 "ou see $ha$ some o5 $he PHP e/$ens#ons arenG$ loaded correc$l".

Te'tin" the 1end Core 2or Orac%e In'ta%%ation on Windo9'


To $es$ $he 9end ore 5or Oracle #ns$alla$#on on 0#ndo*s !la$5orms( 1. rea$e a 5#le called hello%php #n $he &!ache ("FProgram ilesFApache GroupFApache2Fhtdocs d#rec$or" $ha$ con$a#ns $he 5ollo*#ng PHP code(
!php echo .$ello world'.( !"

2.

O!en a *eb bro*ser and en$er $he 5ollo*#ng UR. #n "our bro*ser(
http://27T.?.?.2/hello.php

The l#ne $ello world' a!!ears #n $he bro*ser. &n" errors #n "our PHP code are d#s!la"ed #5 "ou ha'e con5#gured PHP $o d#s!la" errors.

Con2i"urin" 1end Core 2or Orac%e


>n $h#s sec$#on, "ou con5#gure en'#ronmen$ 'ar#ables and 9end ore d#rec$#'es $ha$ con$rol de5aul$ error re!or$#ng #n *eb !ages. 1. @n$er $he 5ollo*#ng UR. #n a *eb bro*ser $o access $he 9end ore 5or Oracle &dm#n#s$ra$#on console(
http://27T.?.?.2/]endCore $7

In#ta&&ing 5end (ore .or Orac&e

igure *0" 1end (ore $or Oracle login screen% 2. P. E. 5. @n$er $he AU> !ass*ord $ha$ "ou !ro'#ded dur#ng 9end ore 5or Oracle #ns$alla$#on. l#c+ $he lo*,n >>> #con. l#c+ $he Con5,*&rat,on $ab $o d#s!la" $he con5#gura$#on o!$#ons. l#c+ $he < #con $o e/!and $he @rror Handl#ng and .ogg#ng con5#gura$#on en$r". 3e$ $he ., $layAerror d#rec$#'e $o On $o enable $he d#s!la" o5 errors #n $he HTM. scr#!$ ou$!u$ dur#ng de'elo!men$. Ma+e sure "ou se$ $h#s bac+ $o O55 be5ore "ou release an" a!!l#ca$#ons "ou bu#ld as users *#ll see $he errors and ga#n #n5orma$#on abou$ "our s"s$em $ha$ "ou o$her*#se donL$ *an$ $hem $o see or +no*.

igure *," 1end (ore $or Oracle con$iguration screen% F. -ecause $here are unsa'ed changes, $he BUnsa'ed con5#gura$#onB message a!!ears under $he !age header. l#c+ Sa0e Sett,n* $o sa'e $he con5#gura$#on change.

(on.iguring 5end (ore .or Orac&e

C.

-ecause "ou ha'e made con5#gura$#on changes, "ou mus$ res$ar$ $he &!ache *eb ser'er. Under $he !age header no$#ce $he BPlease Res$ar$ &!acheB message rem#nd#ng "ou $o do so. l#c+ Re tart Ser0er $o res$ar$ $he &!ache ser'er. >5 "ou are us#ng a 0#ndo*s o!era$#ng s"s$em, "ou should res$ar$ $he &!ache ser'er us#ng $he 3er'#ces d#alog #n on$rol Panel, or $he &!ache Mon#$or #n $he s"s$em $ra". l#c+ %o*o&t $o e/#$ $he 9end ore 5or Oracle &dm#n#s$ra$#on !age.

8.

$$

In#ta&&ing 5end (ore .or Orac&e

1%%

CHAPTER +

CONNECTIN! TO ORACLE ,SIN! OCI0


Th#s ha!$er co'ers connec$#ng $o an Oracle da$abase 5rom "our PHP a!!l#ca$#on, #nclud#ng $he $"!es o5 Oracle connec$#ons, en'#ronmen$ 'ar#ables $ha$ ma" a55ec$ connec$#ons, and $un#ng "our connec$#ons. & la$er cha!$er PHP Scalability and High Availability d#scusses connec$#on !ool#ng and ho* #$ a!!l#es $o connec$#on managemen$. -e5ore a$$em!$#ng $o connec$, re'#e* $he sec$#on Setting Oracle Environment Dariables $or Apache !ar$ *a" $hrough $h#s cha!$er and ma+e sure $ha$ "our en'#ronmen$ #s con5#gured a!!ro!r#a$el". The e/am!les use $he HR schema, a demons$ra$#on user accoun$ #ns$alled *#$h $he da$abase. Use &!!l#ca$#on @/!ress or 3K.TPlus $o unloc+ $he accoun$ and se$ a !ass*ord, as descr#bed #n $he cha!$er >sing Oracle Database%

Orac%e Connection T:pe'


There are $hree *a"s $o connec$ $o an Oracle da$abase #n a PHP a!!l#ca$#on, us#ng s$andard connec$#ons, un#=ue connec$#ons, or !ers#s$en$ connec$#ons. @ach me$hod re$urns a connec$#on resource $ha$ #s used #n subse=uen$ O >8 calls.

Standard Connection'
2or bas#c connec$#on $o Oracle use PHPLs oc0Gconnect:; call(
,c - oc0Gconnect:,u+ername% ,pa++word% ,d1name;(

Hou can call oc0Gconnect:; more $han once #n a scr#!$. >5 "ou do $h#s and use $he same username and da$abase name, $hen "ou ge$ a !o#n$er $o $he or#g#nal connec$#on.

8u%tip%e ,niEue Connection'


To ge$ a $o$all" #nde!enden$ connec$#on use oc0GnewGconnect:;(
,c - oc0GnewGconnect:,u+ername% ,pa++word% ,d1name;(

@ach connec$#on #s se!ara$e 5rom an" o$her. Th#s le$s "ou ha'e more $han one da$abase sess#on o!en a$ $he same $#me. Th#s can be use5ul *hen "ou *an$ $o do da$abase o!era$#ons #nde!enden$l" 5rom each o$her.

Per'i'tent Connection'
Pers#s$en$ connec$#ons can be made *#$h oc0Gpconnect:;(
,c - oc0Gpconnect:,u+ername% ,pa++word% ,d1name;(

1%1

(onnecting to Orac&e /#ing O(I

Pers#s$en$ connec$#ons are no$ au$oma$#call" closed a$ $he end o5 a PHP scr#!$ and rema#n o!en #n PHPLs !ers#s$en$ connec$#on cache 5or reuse #n o$her scr#!$s. Th#s ma+es oc0Gpconnect:; 5as$ 5or 5re=uen$l" used *eb a!!l#ca$#ons. Reconnec$#on does no$ re=u#re re?au$hen$#ca$#on $o $he da$abase. @ach cache en$r" uses $he username, $he da$abase name, $he charac$er se$ and $he connec$#on !r#'#lege $o ensure reconnec$#ons are $he same as $he or#g#nal.

igure *4" Persistent connections are cached in PHP and held open to the database% .#m#$s on $he number o5 !ers#s$en$ connec$#ons #n $he cache can be se$, and connec$#ons can be au$oma$#call" e/!#red $o 5ree u! resources. The !arame$ers 5or $un#ng !ers#s$en$ connec$#ons are d#scussed la$er #n $h#s cha!$er. 0hen $he PHP !rocess $erm#na$es, $he connec$#on cache #s des$ro"ed and all da$abase connec$#ons closed. Th#s means $ha$ 5or command l#ne PHP scr#!$s, !ers#s$en$ connec$#ons are e=u#'alen$ $o normal connec$#ons and $here #s no !er5ormance bene5#$.

Orac%e Data)a'e Na4e Connection Identi2ier'


The ,d1name connec$#on #den$#5#er #s $he name o5 $he local or remo$e da$abase $ha$ "ou *an$ $o a$$ach $o. >$ #s #n$er!re$ed b" Oracle 7e$, $he com!onen$ o5 Oracle $ha$ handles $he underl"#ng connec$#on $o $he da$abase and es$abl#shes a connec$#on $hrough $o $he ne$*or+ Ml#s$enerO on $he da$abase ser'er. The connec$#on #den$#5#er can be one o5(

&n @as" onnec$ s$r#ng & onnec$ Descr#!$or s$r#ng & onnec$ 7ame

1%2

Orac&e )ata*a#e 6a7e (onnection Identi.ier#

Ea': Connect Strin"


>5 "ou are runn#ng Oracle Da$abase N@ on a mach#ne called mymachine, and $he PHP?enabled *eb ser'er #s on $he same mach#ne, "ou could connec$ $o $he HR schema *#$h(
,c - oc0Gconnect:#hr#% #hrpwd#% #mymach0ne/IE#;(

>n $h#s gu#de, *e assume $he da$abase #s on $he same mach#ne as &!ache and PHP so *e use localho+t(
,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;(

De!end#ng on "our ne$*or+ con5#gura$#on, "ou ma" need $o use $he e=u#'alen$ >P address(
,c - oc0Gconnect:#hr#% #hrpwd#% #27T.?.?.2/IE#;(

The @as" onnec$ s$r#ng #s )D- ?l#+e. The Oracle 10g s"n$a/ #s(
3//4ho+tGname3:port43/+er/0ceGname4

>5 PHP l#n+s *#$h Oracle 11g l#brar#es, $he enhanced 11g s"n$a/ can be used(
3//4ho+tGname3:port43/+er/0ceGname43:+er/erGtype43/0n+tanceGname4

The !re5#/ // #s o!$#onal. The !or$ number de5aul$s $o OracleLs s$andard !or$, 1521. The ser'#ce name de5aul$s $o same name as $he da$abaseGs hos$ com!u$er name. The ser'er #s $he $"!e o5 !rocess $ha$ Oracle uses $o handle $he connec$#on, see $he cha!$er on Da$abase Res#den$ onnec$#on Pool#ng 5or an e/am!le. The #ns$ance name #s used *hen connec$#ng $o a s!ec#5#c mach#ne #n a clus$ered en'#ronmen$. 0h#le #$ #s common 5or Oracle da$abase s#$es $o use !or$ 1521, #$ #s rela$#'el" rare $ha$ a da$abase *#ll be #ns$alled *#$h $he ser'#ce name se$ $o $he hos$ name. Hou *#ll almos$ al*a"s need $o s!ec#5" $he connec$#on #den$#5#er as a$ leas$ ho+tGname/+er/0ceGname. The l+nrctl command on $he da$abase ser'er sho*s $he ser'#ce names $ha$ $he Oracle 7e$ l#s$ener acce!$s re=ues$s 5or.
, l+nrctl +er/0ce+ KSLRCTK for K0nu<: Ver+0on 2?.7.?.2.? - 8roduct0on on ?2-BCT-7??M 2M:2T:2? Copyr0ght :c; 2>>2% 7??6% Bracle. All r0ght+ re+er/ed.

Connect0ng to :DESCR98T9BL-:ADDRESS-:8RBTBCBK-98C;:CEJ-EIT8RBCG*BRGIE;;; Ser/0ce+ Summary... Ser/0ce .8KSE<t8roc. ha+ 2 0n+tance:+;. 9n+tance .8KSE<t8roc.% +tatu+ ULCLB&L% ha+ 2 handler:+; for th0+ +er/0ce... $andler:+;: .DED9CATED. e+ta1l0+hed:? refu+ed:? KBCAK SERVER Ser.ice /XE/ has 0 instance$s&. 9n+tance .IE.% +tatu+ READJ% ha+ 2 handler:+; for th0+ +er/0ce... $andler:+;: .DED9CATED. e+ta1l0+hed:66 refu+ed:? +tate:ready KBCAK SERVER . . .

1%3

(onnecting to Orac&e /#ing O(I

Th#s sho*s $he ser'#ce IE #s a'a#lable. Hou can use $he @as" onnec$ s"n$a/ $o connec$ $o Oracle8i, OracleDi7 Oracle10g, and Oracle11g da$abases as long as PHP #s l#n+ed *#$h Oracle 10g or grea$er l#brar#es. Th#s s"n$a/ #s usable #n 9end ore 5or Oracle. More #n5orma$#on on $he s"n$a/ can be 5ound #n $he OracleH Database Get Services AdministratorIs Guide !!g Release ! 9!!%!:.

Data)a'e Connect De'criptor Strin"


The 5ull Oracle 7e$ connect descriptor s$r#ng g#'es $o$al 5le/#b#l#$" o'er $he connec$#on.
,d1 - #:DESCR98T9BL :ADDRESS - :8RBTBCBK - TC8; :$BST - mymach0ne.mydoma0n;:8BRT - 2672;; :CBLLECTGDATA :SERVER - DED9CATED; :SERV9CEGLAOE - OJDB.OJDBOA9L;;;#( ,c - oc0Gconnect:,u+ername% ,pa++word% ,d1;(

The s"n$a/ can be more com!le/ $han $h#s e/am!le, de!end#ng on $he da$abase and Oracle 7e$ 5ea$ures used. 2or e/am!le, b" us#ng $he 5ull s"n$a/, "ou can enable 5ea$ures l#+e load balanc#ng and $*ea+ !ac+e$ s#1es. The @as" onnec$ s"n$a/ does no$ allo* $h#s 5le/#b#l#$".

Data)a'e Connect Na4e


Hou can s$ore $he connect descriptor s$r#ng #n a 5#le called tnsnames%ora and re5er $o #$ #n PHP us#ng a connect name(
R tn+name+.ora OJA - :DESCR98T9BL :ADDRESS - :8RBTBCBK - TC8; :$BST - mymach0ne.mydoma0n;:8BRT - 2672;; :CBLLECTGDATA :SERVER - DED9CATED; :SERV9CEGLAOE - OJDB.OJDBOA9L;;;

>n PHP "ou *ould use $he connect name OJA $o connec$ $o $he da$abase(
,c - oc0Gconnect:,u+ername% ,pa++word% #OJA#;(

PHP needs $o be able $o 5#nd $he tnsnames%ora 5#le $o resol'e $he OJA name. The d#rec$or" !a$hs $ha$ Oracle searches 5or tnsnames%ora de!end on "our o!era$#ng s"s$em. On .#nu/, $he search !a$h #ncludes(
,TLSGADO9L/tn+name+.ora /etc/tn+name+.ora ,BRACKEG$BOE/networN/adm0n/tn+name+.ora

>5 PHP *as com!#led us#ng $he Oracle l#brar#es #n an OR& .@JHOM@?s$"le #ns$all, $hen se$ BRACKEG$BOE be5ore s$ar$#ng $he *eb ser'er. The !re?su!!l#ed EORA(3E8HO6E<net&or5<admin<tnsnames%ora *#ll $hen au$oma$#call" be 5ound. >n Oracle Da$abase N@, ,BRACKEG$BOE #s(
1%4

Orac&e )ata*a#e 6a7e (onnection Identi.ier# /u+r/l01/oracle/<e/app/oracle/product/2?.7.?/+er/er

>5 PHP *as bu#l$ *#$h Oracle >ns$an$ l#en$, or 9end ore 5or Oracle #s used, $hen !u$ tn+name+.ora #n <etc, or se$ TLSGADO9L $o $he d#rec$or" con$a#n#ng #$ !r#or $o s$ar$#ng $he *eb ser'er. Ma+e sure &!ache has read !erm#ss#ons on tnsnames%ora. >n some OR& .@JHOM@?s$"le #ns$alls, $he de5aul$ !erm#ss#ons on $he 5#le are res$r#c$#'e.

Co44on Connection Error'


>5 9end ore 5or Oracle #s no$ used, some en'#ronmen$ 'ar#ables need $o be se$ be5ore s$ar$#ng $he *eb ser'er. Hou ma" also o!$#onall" *an$ $o se$ $he en'#ronmen$ $o con$rol OracleGs global#1a$#on se$$#ngs.

Note: ,o not set +racle environment variables in #)# scripts with puten/:; because +racle libraries may be loaded and initiali2ed before the scripts run.

The O >8 e/$ens#on al*a"s needs $o 5#nd Oracle l#brar#es, error message da$a and o!$#onall" needs $he tnsnames%ora 5#le. 7o$ 5#nd#ng $he l#brar#es can lead $o &!ache s$ar$u! errors :see $he &!ache error log 5#le; abou$ O >8 no$ be#ng #n#$#al#1ed, and $o scr#!$ run$#me errors l#+e(
8$8 *atal error: Call to undef0ned funct0on oc0Gconnect:;

Th#s error means $ha$ $he O >8 e/$ens#on #s no$ a'a#lable. hec+ $ha$ BRACKEG$BOE andIor KDGK9BRARJG8AT$ on .#nu/, or 8AT$ on 0#ndo*s are 'al#d. &no$her !o$en$#al source o5 !roblems #s ha'#ng mul$#!le #ns$alla$#ons o5 Oracle l#brar#es. Us#ng m#sma$ched 'ers#ons o5 Oracle l#brar#es and 5#les can lead $o PHP re$urn#ng errors such as(
BRA-27T?6: Cannot acce++ LKS data f0le+ or 0n/al0d en/0ronment +pec0f0ed

or(
BC9En/Ll+Create:; fa0led. There 0+ +ometh0ng wrong w0th your +y+tem

Users o5 older 'ers#ons o5 O >8 ma" see $he one o5 $he e=u#'alen$ errors(
BC9En/Create:; fa0led. There 0+ +ometh0ng wrong w0th your +y+tem

or
BC9En/9n0t:; fa0led. There 0+ +ometh0ng wrong w0th your +y+tem

Th#s en'#ronmen$ #n#$#al#1a$#on !roblem #s common on 0#ndo*s *hen mul$#!le Oracle en'#ronmen$s are #ns$alled and PHP #s no$ us#ng $he correc$ one. :3ome users mo'e $he >ns$an$ l#en$ D..s $o $he &!ache or PHP d#rec$or" as a =u#c+ solu$#on;. >5 "ou are us#ng an Oracle Da$abase 10g Release 2 da$abase o$her $han $he @/!ress @d#$#on :Oracle Da$abase N@;, "ou ma" need $o g#'e $he &!ache !rocess access $o OracleLs l#brar#es and global#1a$#on da$a. Re5er $o $he EORA(3E8HO6E<install<changePerm%sh scr#!$ #n la$er Oracle !a$chse$s. >5 an error l#+e $h#s occurs(
1%5

(onnecting to Orac&e /#ing O(I Error wh0le try0ng to retr0e/e te<t for error BRA-27265

#$ means $*o !roblems ha!!ened. 2#rs$, a connec$#on error OR&?1215E occurred. The second !roblem #s $he M@rror *h#le $r"#ng $o re$r#e'e $e/$O message, #nd#ca$#ng OracleLs message 5#les *ere no$ 5ound, mos$ l#+el" because BRACKEG$BOE #s no$ correc$l" se$. The e/!ec$ed descr#!$#on 5or OR&?1215E #s ac$uall"(
BRA-27265: TLS:could not re+ol/e +er/0ce name

#nd#ca$#ng $ha$ $he connec$#on s$r#ng #s no$ 'al#d, or $he tnsnames%ora 5#le :#5 one #s be#ng used; *asnG$ readable. The resul$ #s $ha$ O >8 does no$ +no* *h#ch mach#ne $o connec$ $o. & s#m#lar error(
BRA-27625 TLS:l0+tener doe+ not currently Nnow of +er/0ce reFue+ted 0n connect de+cr0ptor

means $ha$ O >8 *as able $o con$ac$ a mach#ne hos$#ng Oracle, bu$ $he e/!ec$ed da$abase #s no$ runn#ng on $ha$ com!u$er. 2or e/am!le, #5 Oracle Da$abase N@ #s curren$l" runn#ng on "our com!u$er and "ou $r" $o connec$ $o localho+t/a1c "ou *#ll ge$ $h#s error. The bo$$om l#ne #s $ha$ "our en'#ronmen$ should be se$ correc$l" and cons#s$en$l". The &!ache !rocess mus$ ha'e access $o Oracle l#brar#es and con5#gura$#on 5#les. @n'#ronmen$ 'ar#ables such as ,BRACKEG$BOE mus$ be se$ #n $he shell $ha$ s$ar$s &!ache.

Settin" Orac%e En.iron4ent @aria)%e' 2or Apache


0hen "ou use O >8, "ou mus$ se$ some Oracle en'#ronmen$ 'ar#ables be5ore s$ar$#ng $he *eb ser'er. >5 "ou ha'e en'#ronmen$ rela$ed !roblems Q une/!ec$ed connec$#on errors l#+e $hose abo'e are $"!#cal Q $hen chec+ $he ou$!u$ 5rom $he PHP php0nfo:; 5unc$#on( Script ," phpin$o%php
!php php0nfo:;( !"

.oo+ a$ $he Environment sec$#on :no$ $he Apache Environment sec$#on; and ma+e sure $he Oracle 'ar#ables are se$ $o $he 'alues "ou e/!ec$. The 'ar#ables needed are de$erm#ned b" ho* PHP #s #ns$alled, ho* "ou connec$, and *ha$ o!$#onal se$$#ngs are des#red.

#able ," (ommon Oracle environment variables on 3inux%


Orac%e En.iron4ent @aria)%e BRACKEG$BOE Purpo'e The d#rec$or" con$a#n#ng $he Oracle so5$*are. Th#s d#rec$or" mus$ be access#ble b" $he &!ache !rocess. The 'ar#able #s no$ needed #5 PHP uses Oracle >ns$an$ l#en$ or #5 9end ore 5or Oracle #s #ns$alled.

1%6

Setting Orac&e 'nviron7ent 8aria*&e# .or 0"ache

Orac%e En.iron4ent @aria)%e BRACKEGS9D

Purpo'e The Oracle 7e$ connec$ name o5 $he da$abase. Onl" used *hen PHP #s on same mach#ne as $he da$abase and $he connec$#on #den$#5#er #s no$ s!ec#5#ed #n $he PHP connec$ 5unc$#on. 7o$ used 5or Oracle >ns$an$ l#en$ or 9end ore 5or Oracle. 7o$ commonl" se$ 5or PHP. 3e$ $h#s $o #nclude $he Oracle l#brar#es, 5or e/am!le EORA(3E8HO6E<lib or EHO6E<instantclient8!!8!% 7o$ needed #5 $he l#brar#es are loca$ed b" an al$erna$#'e me$hod, such as *#$h $he <etc<ld%so%con$ l#n+er !a$h 5#le. 9end ore 5or Oracle se$s $h#s 'ar#able #5 necessar". De$erm#nes $he Mna$#onal language su!!or$O global#1a$#on o!$#ons 5or O >8. 3ee $he cha!$er Globali.ation 5or more de$a#ls. >5 no$ se$, a de5aul$ 'alue *#ll be chosen b" Oracle. 3e$$#ng $h#s #s recommended. The loca$#on o5 $he tnsnames%ora and o$her Oracle 7e$ con5#gura$#on 5#les. Onl" needed #5 a da$abase connec$ name 5rom a tnsnames%ora 5#le #s used #n $he O >8 connec$ 5unc$#ons and $he tnsnames%ora 5#le #s no$ #n EORA(3E8HO6E<net&or5<admin. 7o$ needed #5 @as" onnec$ s"n$a/ #s be#ng used #n $he connec$ 5unc$#ons :unless a sAlnet%ora 5#le #s used;.

KDGK9BRARJG8AT$

LKSGKALW

TLSGADO9L

0#$h Oracle Da$abase N@, "ou can se$ $he shellGs en'#ronmen$ b" us#ng $he oracle8env%sh scr#!$(
R . /u+r/l01/oracle/<e/app/oracle/product/2?.7.?/+er/er/10n/oracleGen/.+h

7o$e $he s!ace a5$er $he !er#od. Th#s command allo*s $he scr#!$ $o se$ $he en'#ronmen$ o5 $he shell #$sel5. On o$her ed#$#ons o5 $he Oracle da$abase, $he <usr<local<bin<oraenv or <usr<local<bin<coraenv scr#!$s se$ $he en'#ronmen$. Run one o5 $hese scr#!$s be5ore s$ar$#ng &!ache. Hou *#ll be !rom!$ed 5or $he da$abase $o connec$ $o(
R . /u+r/local/10n/oraen/ BRACKEGS9D - 34 ! orcl

>5 "our da$abase #s on a remo$e mach#ne, "ou *#ll ha'e $o se$ $he en'#ronmen$ manuall". To s#m!l#5" $h#ngs, "ou ma" crea$e a scr#!$ $o se$ $he en'#ronmen$ and s$ar$ &!ache, 5or e/am!le( Script 4" start8apache
R' /10n/+h BRACKEG$BOE-/u+r/l01/oracle/<e/app/oracle/product/2?.7.?/+er/er KDGK9BRARJG8AT$-,BRACKEG$BOE/l01:,KDGK9BRARJG8AT$ LKSGKALW-AOER9CALGAOER9CA.&EMOS&9L2767 e<port BRACKEG$BOE KDGK9BRARJG8AT$ LKSGKALW echo .Bracle $ome: ,BRACKEG$BOE. echo Start0ng Apache Re<port " /tmp/en//ar+ R uncomment to de1ug 1%7

(onnecting to Orac&e /#ing O(I /u+r/+10n/apachectl +tart

On Oracle @n$er!r#se .#nu/, #ns$ead o5 crea$#ng a shell scr#!$ $o call apachectl, "ou can add en'#ronmen$ 'ar#ables $o $he end o5 <etc<syscon$ig<httpd(
... e<port BRACKEG$BOE-/u+r/l01/oracle/<e/app/oracle/product/2?.7.?/+er/er e<port KDGK9BRARJG8AT$-,KDGK9BRARJG8AT$:,BRACKEG$BOE/l01 e<port LKSGKALW-AOER9CALGAOER9CA.&EMOS&9L2767

3ome &!ache 2 #ns$alla$#ons use an Men''arsO scr#!$ #n $he &!ache b#n d#rec$or" $o se$ 'ar#ables. Us#ng &!acheGs SetEn/ d#rec$#'e #s some$#mes #nsu55#c#en$. The #m!or$an$ $h#ng #s $o se$ $he en'#ronmen$ be5ore &!ache loads an" Oracle l#brar". >5 &!ache #s s$ar$ed au$oma$#call" *hen "our mach#nes boo$s, "ou *#ll need $o ma+e sure &!ache has $he en'#ronmen$ se$ a$ boo$ $#me.

Note: ,o not set +racle environment variables in #)# scripts with puten/:;. The web server may load +racle libraries and initiali2e +racle data structures before runnin& your script. 3sin& puten/:; causes hard to trac! errors as the behavior is not consistent for all variables, web servers, operatin& systems, or +C*4 functions. 5ariables should be set prior to Apache startin&.

9end ore 5or Oracle #s au$oma$#call" con5#gured $o use Oracle >ns$an$ l#en$. &s !ar$ o5 $he #ns$all, #$ mod#5#es <usr<local<1end<apache2<bin<envvars $o add KDGK9BRARJG8AT$. The envvars 5#le #s called b" apachectl. Ma+e sure "ou !reser'e $h#s 5#le #nclus#on #5 "ou la$er mod#5" $he apachectl scr#!$. The Oracle en'#ronmen$ 'ar#ables "ou ma" *an$ $o se$ e/!l#c#$l" 5or 9end ore 5or Oracle are LKSGKALW and TLSGADO9L. >5 PHP *as bu#l$ *#$h Oracle >ns$an$ l#en$, #$ can be con'en#en$ $o crea$e a 5#le <etc<ld%so%con$%d<instantclient%con$ con$a#n#ng $he !a$h $o $he >ns$an$ l#en$ l#brar#es(
/u+r/l01/oracle/22.2.?.2/cl0ent/l01

Run ldconf0g $o rebu#ld $he s"s$emGs l#brar" search !a$h. Onl" do $h#s #5 $here #s no o$her Oracle so5$*are #n use on $he mach#ne. The remo'es $he need $o se$ KDGK9BRARJG8AT$ e'er"*here, bu$ u!grad#ng re=u#res remember#ng $o change $he !a$h. >5 "ou ha'e mul$#!le 'ers#ons o5 Oracle #ns$alled, "ou m#gh$ be able $o use KDG8REKBAD or e=u#'alen$ $o 5orce &!ache $o load $he des#red Oracle libclntsh%so 5#le. PHP should be run us#ng $he same 'ers#on o5 $he Oracle l#brar#es as *ere used $o bu#ld $he PHP l#brar" and e/ecu$ables.

C%o'in" Orac%e Connection'


&$ $he end o5 each scr#!$, connec$#ons o!ened *#$h oc0Gconnect:; or oc0GnewGconnect:; are au$oma$#call" closed. Hou can also e/!l#c#$l" close $hem b" call#ng(
oc0Gclo+e:,c;(

&n" uncomm#$$ed da$a #s rolled bac+. The 5unc$#on has no e55ec$ on !ers#s$en$ connec$#ons. :3ee $he cha!$er on connec$#on !ool#ng 5or ca'ea$s;.

1%

(&o#ing Orac&e (onnection#

>5 a long runn#ng scr#!$ onl" s!ends a small amoun$ o5 $#me #n$erac$#ng *#$h $he da$abase, close connec$#ons as soon as !oss#ble $o 5ree da$abase resources 5or o$her users. 0hen $he &!ache or PHP command l#ne !rocess $erm#na$es, all da$abase connec$#ons are closed. The oc0Gclo+e:; 5unc$#on *as a Mno?o!O !r#or $o $he re?5ac$or#ng o5 O >8. Tha$ #s, #$ had no 5unc$#onal code, and ne'er ac$uall" closed a connec$#on. Hou could no$ e/!l#c#$l" close connec$#ons e'en #5 "ou *an$ed $oR Hou can re'er$ $o $h#s old beha'#or *#$h a php%ini se$$#ng(
oc0M.oldGoc0Gclo+eG+emant0c+ - Bn

C%o'e State4ent Re'ource' <e2ore C%o'in" Connection'


The oc0Gclo+e:; 5unc$#on *or+s b" re5erence coun$#ng. Onl" *hen all PHP re5erences $o $he da$abase connec$#on are 5#n#shed *#ll #$ ac$uall" be closed and da$abase resources 5reed. Th#s e/am!le sho*s $he e55ec$ o5 re5erence coun$#ng( Script -" close%php
!php ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( ,+ - oc0Gpar+e:,c% .+elect H from locat0on+.;( oc0Ge<ecute:,+;( oc0GfetchGall:,+% ,re+;( // oc0GfreeG+tatement:,+;( oc0Gclo+e:,c;( echo .Sleep0ng . . ..( +leep:2?;( echo .Done.( !"

0h#le close%php #s slee!#ng, #5 "ou =uer" as a !r#'#leged user(


SUK" +elect u+ername from /,+e++0on where u+ername 0+ not null(

"ou *#ll see $ha$ $R #s s$#ll sho*n as connec$ed un$#l $he +leep:; 5#n#shes and $he scr#!$ $erm#na$es. Th#s #s because $he oc0Gpar+e:; call crea$#ng $he s$a$emen$ resource ,+ #n$ernall" #ncreases $he re5erence coun$ on ,c. The da$abase connec$#on #s no$ closed un$#l PHPGs end?o5?scr#!$ !rocess#ng des$ro"s ,+. &n oc0GfreeG+tatement:,+; call *#ll e/!l#c#$l" decrease $he re5erence coun$ on ,c allo*#ng $he oc0Gclo+e:; $o ha'e an #mmed#a$e e55ec$. >5 $h#s 5ree#ng call #s uncommen$ed #n $he e/am!le, $he 3K.TPlus =uer" *#ll sho* $he da$abase connec$#on *as e/!l#c#$l" closed be5ore $he +leep:; s$ar$s. ,ar#ables and o$her +#nds o5 resources ma" also #ncrease $he re5erence coun$ on a connec$#on, and #n $urn ha'e $he#r o*n re5erence coun$ *h#ch mus$ be 1ero be5ore $he" can des$ro"ed. The re5erence coun$ *#ll decrease #5 $he 'ar#ables goes ou$ o5 sco!e or are ass#gned ne* 'alues. & common #d#om #s $o ass#gn null $o $he s$a$emen$ resource(
,+ - null( 1%$

(onnecting to Orac&e /#ing O(I

>n $he ne/$ e/am!le, ,c2 and ,c7 are $he one da$abase connec$#on :because oc0Gconnect:; re$urns $he same connec$#on resource *hen called more $han once #n a scr#!$;. The !h"s#cal da$abase connec$#on #s released onl" *hen ,c2 and c7 are bo$h closed. &lso $he s$a$emen$ resource mus$ be 5reed, *h#ch ha!!ens au$oma$#call" *hen doGFuery:; com!le$es and ,+ goes ou$ o5 sco!e. Script *" close2%php
!php funct0on doGFuery:,c% ,Fuery; @ ,+ - oc0Gpar+e:,c% ,Fuery;( oc0Ge<ecute:,+;( oc0GfetchGall:,+% ,re+;( echo . pre".( /arGdump:,re+;( echo . /pre".( A ,c2 - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,c7 - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( // Reu+e+ ,c2 DB connect0on doGFuery:,c2% #+elect u+er from dual#;( oc0Gclo+e:,c2;( doGFuery:,c2% #+elect u+er from dual#;( doGFuery:,c7% #+elect u+er from dual#;( oc0Gclo+e:,c7;( !" // // // // // Uuery 2 worN+ DB connect0on doe+n#t get clo+ed Uuery 7 fa0l+ Uuery ) worN+ DB connect0on 0+ now clo+ed

,ar#able ,c2 #s no$ usable a5$er oc0Gclo+e:,c2; #s e/ecu$ed( PHP has d#ssoc#a$ed #$ 5rom $he connec$#on. The scr#!$ ou$come #s $ha$ $he 5#rs$ and $h#rd =uer#es succeed bu$ $he second one 5a#ls.

Tran'action' and Connection'


Uncomm#$$ed da$a #s rolled bac+ *hen a connec$#on #s closed or a$ $he end o5 a scr#!$. 2or oc0Gpconnect:; $h#s means subse=uen$ scr#!$s reus#ng a cached da$abase connec$#on *#ll no$ see an" da$a $ha$ should no$ be shared. &'o#d le$$#ng da$abase $ransac$#ons rema#n o!en #5 a second oc0Gconnect:; or oc0Gpconnect:; call *#$h $he same user creden$#als #s e/ecu$ed *#$h#n a scr#!$, or #5 oc0Gclo+e:; #s used. Ma+#ng sure da$a #s comm#$$ed or rolled bac+ 5#rs$ can !re'en$ hard $o debug edge cases *here da$a #s no$ be#ng s$ored as e/!ec$ed. The ne/$ cha!$er co'ers da$abase $ransac$#ons #n de$a#l.

Se''ion State 9ith Per'i'tent Connection'


>$ #s !oss#ble 5or a scr#!$ $o change sess#on a$$r#bu$es 5or oc0Gpconnect:; $ha$ are no$ rese$ a$ $he end o5 $he scr#!$. :The Oracle $erm session #s e55ec$#'el" $he same as $he PHP $erm connection;. One e/am!le #s $he global#1a$#on se$$#ng 5or $he da$e 5orma$(
11%

(&o#ing Orac&e (onnection# ,c - oc0Gpconnect:.hr.% .hrpwd.% .localho+t/IE.;( doGFuery:,c% .+elect +y+date from dual.;( ,+ - oc0Gpar+e:,c% .alter +e++0on +et nl+GdateGformat-_JJJJ-OO-DD $$75:O9:SS_.;( ,r - oc0Ge<ecute:,+;( doGFuery:,c% .+elect +y+date from dual.;(

The 5#rs$ $#me $h#s #s called #n a bro*ser, $he $*o da$es re$urned b" $he =uer#es are(
2M-A8R-?T 7??T-?5-2M 25:72:?>

The 5#rs$ da$e has $he s"s$em de5aul$ 5orma$. The second #s d#55eren$ because $he AKTER SESS9BL command changed $he da$e 5orma$. all#ng $he scr#!$ a second $#me g#'es(
7??T-?5-2M 25:72:2? 7??T-?5-2M 25:72:2?

The !ers#s$en$ connec$#on has re$a#ned $he sess#on se$$#ng and $he 5#rs$ =uer" no longer uses $he s"s$em de5aul$ 5orma$. Th#s onl" ha!!ens #5 $he same &!ache !rocess ser'es bo$h HTTP re=ues$s. >5 a ne* &!ache !rocess ser'es $he second re=ues$ $hen #$ *#ll o!en a ne* connec$#on $o $he da$abase, *h#ch *#ll ha'e $he or#g#nal de5aul$ da$e 5orma$. :&lso $he s"s$em *#ll ha'e $*o !ers#s$en$ connec$#ons le5$ o!en #ns$ead o5 one.; 3ess#on changes l#+e $h#s ma" no$ be a concern. Hour a!!l#ca$#ons ma" ne'er need $o do an"$h#ng l#+e #$, or all connec$#ons ma" need $he same 'alues an"*a". >5 $here #s a !oss#b#l#$" #ncorrec$ se$$#ngs *#ll be #nher#$ed, ma+e sure "our a!!l#ca$#on rese$s 'alues a5$er connec$#ng.

Optiona% Connection Para4eter'


The oc0Gconnect:;, oc0GnewGconnect:; and oc0Gpconnect:; 5unc$#ons $a+e an o!$#onal e/$ra $*o !arame$ers(

onnec$#on charac$er se$ onnec$#on sess#on mode

Connection Character Set


The charac$er se$ #s a s$r#ng con$a#n#ng an Oracle charac$er se$ name, 5or e/am!le, YA2SUEC or AK)7UT*M(
,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.% #AK)7UT*M#;(

0hen no$ s!ec#5#ed or LUKK, $he LKSGKALW en'#ronmen$ 'ar#able se$$#ng #s used. Th#s se$$#ng de$erm#nes ho* Oracle $ransla$es da$a *hen #$ #s $rans5erred 5rom $he da$abase $o PHP. >5 $he da$abase charac$er se$ #s no$ e=u#'alen$ $o $he O >8 charac$er se$, some da$a ma" ge$ con'er$ed abnormall". >$ #s recommended $o se$ $h#s !arame$er $o #m!ro'e !er5ormance and guaran$ee a +no*n 'alue #s used. Oracle Da$abase N@ #s a'a#lable #n $*o d#s$r#bu$#ons, one *#$h a da$abase charac$er se$ o5 0@8M30>71252 and $he o$her o5 &.P2UT28 :OracleGs name 5or UT2?8;.

111

(onnecting to Orac&e /#ing O(I

>$ #s u! $o "our a!!l#ca$#on $o handle re$urned da$a correc$l", !erha!s b" us#ng PHPLs m1G+tr0ng, 0con/ or 0ntl e/$ens#ons. Alobal#1a$#on #s d#scussed #n more de$a#l #n $he Globali.ation cha!$er.

Connection Se''ion 8ode


The sess#on mode !arame$er allo*s !r#'#leged or e/$ernall" au$hen$#ca$ed connec$#ons $o be made.

"onnection Pri#ilege Le#el


The O >8 e/$ens#on allo*s !r#'#leged 3H3D-& and 3H3OP@R connec$#ons. Pr#'#leged connec$#ons are d#sabled b" de5aul$. The" can be enabled #n php%ini us#ng(
oc0M.pr0/0legedGconnect - 2

The 3H3D-& and 3H3OP@R !r#'#leges g#'e "ou $he ab#l#$" $o change $he s$a$e o5 $he da$abase, !er5orm da$a reco'er", and e'en access $he da$abase *hen #$ has no$ 5ull" s$ar$ed. -e 'er" care5ul abou$ e/!os#ng $h#s on cus$omer 5ac#ng *eb s#$es, $ha$ #s, do no$ do #$R >$ m#gh$ be use5ul 5or command l#ne PHP scr#!$s #n 'er" s!ec#al c#rcums$ances. 0hen "ou #ns$alled Oracle, $he sys adm#n#s$ra$#'e user accoun$ *as au$oma$#call" crea$ed *#$h $he !ass*ord $ha$ "ou su!!l#ed. &ll base $ables and '#e*s 5or $he da$abase da$a d#c$#onar" are s$ored #n $he 3H3 schema Q $he" are cr#$#cal 5or $he o!era$#on o5 Oracle. -" de5aul$, $he 3H3D-& !r#'#lege #s ass#gned onl" $o user sys7 bu$ #$ and 3H3OP@R can manuall" be gran$ed $o o$her users.

$perating S%ste& 'uthenticated Pri#ileged "onnections


Hou can ha'e $he o!era$#ng s"s$em !er5orm $he au$hen$#ca$#on 5or !r#'#leged connec$#ons based around $he user $ha$ #s runn#ng $he *eb ser'er s"s$em !rocess. &n o!era$#ng s"s$em au$hen$#ca$ed !r#'#leged connec$#on #s e=u#'alen$ $o $he 3K.TPlus connec$#on(
, +Flplu+ / a+ +yd1a

2or / a+ +y+d1a access :*here no username and !ass*ord #s used; #n PHP, all $hese mus$ be $rue(

The o!era$#ng s"s$em !rocess user #s run as a member o5 $he O3 dba grou! PHP #s l#n+ed *#$h $he BRACKEG$BOE so5$*are :$ha$ #s, no$ Oracle >ns$an$ l#en$, or 9end ore 5or Oracle; $ha$ $he da$abase #s us#ng The da$abase #s "our de5aul$ local da$abase, 5or e/am!le, s!ec#5#ed b" $he BRACKEGS9D en'#ronmen$ 'ar#able

Th#s *ould be $"!#call" be done b" com!#l#ng and runn#ng PHP *#$h $he Oracle l#brar#es used b" $he da$abase. 3cr#!$s $ha$ con$a#n o!era$#ng s"s$em au$hen$#ca$ed !r#'#leged connec$#on calls *#ll connec$ success5ull"(
,c - oc0Gconnect:./.% ..% null% null% BC9GSJSDBA;(

112

O"tiona& (onnection Para7eter#

>5 PHP #s #n'o+ed b" &!ache, $he l#brar" !a$h needs $o con$a#n $he same Oracle l#brar#es. &lso $he nobody user mus$ be #n $he !r#'#leged Oracle grou!, 5or e/am!le, #n $he o!era$#ng s"s$em dba grou!. Th#s #s no$ recommended. 3#m#larl", &3 3H3OP@R access #s a'a#lable 5or members o5 $he oper grou!. >n PHP use BC9GSJSB8ER #n oc0Gconnect:;. On 0#ndo*s, $he o!era$#ng s"s$em grou!s are called ORA8DCA and ORA8OPER.

e&ote Pri#ileged 'ccess


0#$h 9end ore 5or Oracle and an" o$her PHP based on Oracle >ns$an$ l#en$, a username and !ass*ord mus$ be g#'en *hen connec$#ng. These connec$#ons are cons#dered Mremo$eO 5rom $he da$abase because $he l#brar#es used b" PHP are no$ $hose used b" $he runn#ng da$abase. Remo$e users can ma+e !r#'#leged connec$#ons onl" *hen $he" ha'e been g#'en $he a!!ro!r#a$e Oracle access. >n 3K.TPlus a !r#'#leged sess#on *ould be s$ar$ed l#+e(
, +Flplu+ u+ername/pa++wordP+0d a+ +y+d1a

The da$abase *#ll no$ !erm#$ $he :!oss#bl" !h"s#call"; Mremo$eO o!era$#ng s"s$em $o au$hor#1e access. &n e/$ra Oracle !ass*ord 5#le needs $o be crea$ed and a !ass*ord needs $o be used #n $he da$abase connec$#on. To se$ u! a !ass*ord 5#le, chec+ $he da$abase #n#$#al#1a$#on !arame$er remoteGlog0nGpa++wordf0le #s EICKUS9VE. Th#s #s $he de5aul$ 'alue. To do $h#s, log #n $o $he o!era$#ng s"s$em shell as $he Oracle da$abase so5$*are o*ner, and s$ar$ 3K.TPlus(
, +Flplu+ / a+ +y+d1a SUK" +how parameter remoteGlog0nGpa++wordf0le LAOE TJ8E VAKUE ----------------------------- ----------- -----------remoteGlog0nGpa++wordf0le +tr0ng EICKUS9VE

& se$$#ng o5 EICKUS9VE means $he !ass*ord 5#le #s onl" used *#$h one da$abase and no$ shared among se'eral da$abases on $he hos$, and enables "ou $o ha'e mul$#!le users connec$ $o $he da$abase as $hemsel'es, and no$ 8us$ as sys. >5 $h#s !arame$er #s no$ se$ $o EICKUS9VE, "ou can change $he 'alue *#$h $he 3K.TPlus and en$er a command s#m#lar $o $he 5ollo*#ng command(
SUK" alter +y+tem +et remoteGlog0nGpa++wordf0le-#e<clu+0/e# 7 +cope-+pf0le +0d-#H#(

2rom $he o!era$#ng s"s$em shell, crea$e an Oracle !ass*ord 5#le(


, ,BRACKEG$BOE/10n/orapwd f0le-,BRACKEG$BOE/d1+/acct.pwd E " pa++word-+ecret entr0e+-2?

Th#s crea$es a !ass*ord 5#le named acct%p&d $ha$ allo*s u! $o 10 !r#'#leged users *#$h d#55eren$ !ass*ords :$h#s number can be changed la$er;. The 5#le #s #n#$#all" crea$ed *#$h $he !ass*ord secret 5or users connec$#ng *#$h $he username sys. To add a ne* user $o $he !ass*ord 5#le use 3K.TPlus(
SUK" create u+er c2 0dent0f0ed 1y c2pw( 113

(onnecting to Orac&e /#ing O(I SUK" grant connect to c2( SUK" grant +y+d1a to c2( SUK" +elect H from /,pwf0leGu+er+( USERLAOE SJSDBA ------------------------------ -----SJS TRUE C2 TRUE

SJSB8ER ------TRUE *AKSE

7o* #n PHP "ou can use $he 5ollo*#ng connec$#on command(


,c - oc0Gconnect:.c2.% .c2pw.% #localho+t/IE#% null% BC9GSJSDBA;(

One 5ea$ure o5 a !r#'#leged connec$#on #s $ha$ #5 "ou #ssue a SEKECT USER *RBO DUAK s$a$emen$, an" BC9GSJSDBA connec$#on *#ll sho* $he user as sys no$ c!. & connec$#on made *#$h BC9GSJSB8ER *#ll sho* a user o5 public.

!xternal 'uthentication
O >8 1.P su!!or$s Oracle @/$ernal &u$hen$#ca$#on. >ns$ead o5 s$or#ng a username and !ass*ord #n PHP scr#!$s and au$hen$#ca$#ng aga#ns$ a username and !ass*ord s$ored #n $he da$abase, creden$#als can be au$hen$#ca$ed b" an ou$s#de s"s$em such as Oracle 0alle$. The o!era$#ng s"s$em user runn#ng $he &!ache !rocess could be gran$ed read access $o $he *alle$ us#ng &ccess on$rol .#s$s. To use e/$ernal au$hen$#ca$#on, 5#rs$ con5#gure $he da$abase $o use an e/$ernal au$hen$#ca$#on me$hod. Re5er $o Oracle documen$a$#on 5or de$a#ls. >n O >8, !ass $he 5lag BC9GCREDGEIT as $he session8mode !arame$er $o oc0Gconnect:;, oc0GnewGconnect:; or oc0Gpconnect:;(
,c - oc0Gconnect:./.% ..% ,d1% null% BC9GCREDGEIT;( BC9GCREDGEIT can onl" be used *#$h username o5 ./. and a em!$" !ass*ord. The php%ini !arame$er oc0M.pr0/0legedGconnect0on ma" be Bn or Bff. The 5lag ma" be comb#ned *#$h $he e/#s$#ng BC9GSJSB8ER or BC9GSJSDBA modes. 2or e/am!le( ,c - oc0Gconnect:./.% ..% ,d1% null% BC9GCREDGEITDBC9GSJSB8ER;(

7o$e( oc0M.pr0/0legedGconnect0on needs $o be Bn 5or BC9GSJSDBA and BC9GSJSB8ER use. The e/$ernal au$hen$#ca$#on 5ea$ure #s no$ a'a#lable #n O >8 on 0#ndo*s 5or secur#$" reasons.

Chan"in" the Data)a'e Pa''9ord


The O >8 e/$ens#on allo*s Oracle da$abase !ass*ords $o be changed.

Chan"in" Pa''9ord' On De4and


&5$er connec$#ng, a !ass*ord can be changed *#$h oc0Gpa++wordGchange:;(
,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( oc0Gpa++wordGchange:,c% #hr#% #hrpwd#% #newhrpwd#;(

3ubse=uen$ scr#!$s ma" no* connec$ us#ng(


114

(hanging the )ata*a#e Pa##word ,c - oc0Gconnect:#hr#% #newhrpwd#% #localho+t/IE#;(

Chan"in" E&pired Pa''9ord'


3ome$#mes connec$#on ma" 5a#l because $he !ass*ord #s no longer 'al#d. 2or e/am!le, $he D-& ma" ha'e se$ a !ass*ord !ol#c" $o e/!#re !ass*ords a$ a cer$a#n $#me b" us#ng CREATE 8RB*9KE% or ma" ha'e e/!#red a !ass*ord #mmed#a$el" *#$h AKTER USER 5orc#ng $he user $o choose a ne* !ass*ord $he 'er" 5#rs$ $#me $he" connec$. 0hen $he user $r#es $o connec$, $he#r !ass*ord #s recogn#1ed bu$ $he" ge$ an BRA-7M??2: the pa++word ha+ e<p0red message and *#ll no$ be able $o com!le$e $he#r log on. >n $h#s case, #ns$ead o5 $he user ha'#ng $o bo$her $he D-& $o manuall" rese$ $he e/!#red !ass*ord, oc0Gpa++wordGchange:; can be used $o re?connec$ and change $he !ass*ord one o!era$#on. The ne/$ e/am!le sho*s $h#s #n ac$#on. Script ?" connectexpired%sAl
drop u+er peregr0ne ca+cade( create u+er peregr0ne 0dent0f0ed 1y a1c( grant create +e++0on to peregr0ne( alter u+er peregr0ne pa++word e<p0re(

Script !2" connectexpired%php


!php ,un - .peregr0ne.( ,pw - .a1c.( ,d1 - .localho+t/IE.( // Lew temporary u+er to 1e created. // 9n0t0al pa++word for ,un // Data1a+e to connect to

funct0on doGconnect:,un% ,pw% ,d1; @ echo .Call0ng oc0Gconnect:; 1r"En.( ,c - oc0Gconnect:,un% ,pw% ,d1;( 0f :,c; @ echo .Connected +ucce++fully 1r"En.( A el+e @ ,m - oc0Gerror:;( 0f :,m3#code#4 -- 7M??2; @ // Connect and change the pa++word to a new one formed 1y // append0ng #<# to the or0g0nal pa++word. // 9n an appl0cat0on you could prompt the u+er to choo+e // the new pa++word. echo .Connect0on fa0led: the pa++word for ,un ha+ e<p0red 1r"En.( ,c - changeGandGconnect:,un% ,pw% ,pw..<.% ,d1;( A el+e @ echo .Error: .% ,m3.me++age.4% . 1r"En.( e<0t( A A 115

(onnecting to Orac&e /#ing O(I return:,c;(

funct0on changeGandGconnect:,un% ,pw2% ,pw7% ,d1; @ echo .Call0ng oc0Gpa++wordGchange:; to connect 1r"En.( ,c - oc0Gpa++wordGchange:,d1% ,un% ,pw2% ,pw7;( 0f :',c; @ ,m - oc0Gerror:;( echo .Error: .% ,m3.me++age.4% . 1r"En.( A el+e @ echo .Connected and changed pa++word to ,pw7 1r"En.( A return:,c;( A funct0on +howGu+er:,c; @ ,+ - oc0Gpar+e:,c% .+elect u+er from dual.;( oc0Ge<ecute:,+;( oc0GfetchGall:,+% ,re+;( echo .Jou are connected a+ @,re+3#USER#43?4A 1r"En.( A // Connect a+ ,un and conf0rm connect0on +ucceeded ,c - doGconnect:,un% ,pw% ,d1;( +howGu+er:,c;( !"

-e5ore runn#ng $he PHP scr#!$, 5#rs$ run connectexpired%sAl as a !r#'#leged user(
, +Flplu+ +y+tem/+y+tempwdPlocalho+t/IE Pconnecte<p0red.+Fl

0hen oc0Gconnect:; #n doGconnect:; 5a#ls *#$h an OR&?28001 error, changeGandGconnect:; #s called $o change $he !ass*ord and connec$ #n a s#ngle s$e!. >n $h#s e/am!le, $he ne* !ass*ord #s s#m!l" 5ormed b" conca$ena$#ng an M/O $o $he curren$ !ass*ord. >n an a!!l#ca$#on, $he user *ould be !rom!$ed 5or $he ne* !ass*ord. The ou$!u$ o5 connectexpired%php #s(
Call0ng oc0Gconnect:; Connect0on fa0led: the pa++word for peregr0ne ha+ e<p0red Call0ng oc0Gpa++wordGchange:; to connect Connected and changed pa++word to a1c< Jou are connected a+ 8EREWR9LE

The !ass*ord change call oc0Gpa++wordGchange:,d1% ,un% ,pw2% ,pw7;( d#55ers 5rom $he e/am!le #n $he !re'#ous sec$#on (hanging Pass&ords On Demand #n $ha$ #$ !asses a da$abase connec$#on #den$#5#er #den$#5#er, localho+t/IE, as $he 5#rs$ !arame$er #ns$ead o5 !ass#ng $he connec$#on resource o5 an alread" o!ened connec$#on. Th#s ne* usage connec$s $o $he da$abase and changes $he !ass*ord $o ,pw7 all a$ $he same $#me. 3ubse=uen$ scr#!$s *#ll be able $o connec$ us#ng $he ne* !ass*ord. Th#s me$hod o5 connec$#ng *#$h oc0Gpa++wordGchange:; also *or+s #5 $he !ass*ord has not e/!#red.
116

-uning Orac&e (onnection# in PHP

Tunin" Orac%e Connection' in PHP


onnec$#ons $o Oracle can be $uned b" chang#ng $he *a" O >8 calls are made, b" chang#ng $he ne$*or+ con5#gura$#on, or b" $un#ng $he da$abase.

,'e the <e't Connection =unction


Us#ng oc0Gpconnect:; ma+es a b#g #m!ro'emen$ #n o'erall connec$#on s!eed o5 5re=uen$l" used a!!l#ca$#ons because #$ uses $he connec$#on cache #n PHP. & ne*, !h"s#cal connec$#on $o $he da$abase does no$ ha'e $o be crea$ed #5 one alread" e/#s$s #n PHPLs cache. Ho*e'er #5 curren$l" unused, o!en !ers#s$en$ connec$#ons consume $oo much memor" on $he da$abase ser'er, cons#der $un#ng $he $#meou$ !arame$ers or us#ng connec$#on !ool#ng.

Pa'' the Character Set


@/!l#c#$l" !ass#ng $he cl#en$ charac$er se$ name as $he 5our$h !arame$er $o $he connec$#on 5unc$#ons #m!ro'es !er5ormance(
,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.% #&EMDEC#;(

>5 "ou do no$ en$er a charac$er se$, PHP has $o de$erm#ne a cl#en$ charac$er se$ $o use. Th#s ma" #n'ol'e a !o$en$#all" e/!ens#'e en'#ronmen$ loo+u!. Use $he a!!ro!r#a$e charac$er se$ 5or "our re=u#remen$s.

Do Not Set the Date =or4at ,nnece''ari%:


Hou can o5$en remo'e AKTER SESS9BL s$a$emen$s used a5$er connec$#ng. 2or e/am!le, #5 "our connec$#on rou$#ne al*a"s se$s $he da$e 5orma$(
funct0on myGconnect:,un% ,pw% ,d1; @ ,c - oc0Gpconnect:,un% ,pw% ,d1;( ,+ - oc0Gpar+e:,c% .alter +e++0on +et nl+GdateGformat-#JJJJ-OO-DD $$75:O9:SS#.;( oc0Ge<ecute:,+;( return ,c( A

One *a" $o o!$#m#1e $h#s #s $o s#m!l" se$ $he en'#ronmen$ 'ar#able LKSGDATEG*BROAT #n $he shell $ha$ s$ar$s $he *eb ser'er. @ach PHP connec$#on *#ll ha'e $he re=u#red da$e 5orma$ au$oma$#call". 3ome$#mes d#55eren$ da$abase users should ha'e d#55eren$ sess#on 'alues so se$$#ng LKSGDATEG*BROAT globall" #s no$ !oss#ble. 0hen oc0Gconnect:; #s called mul$#!le $#mes #n $he one scr#!$ or *hen !ers#s$en$ connec$#ons are used, $he AKTER SESS9BL can be mo'ed $o a logon $r#gger. Th#s #s because sess#on se$$#ngs are re$a#ned #n cached connec$#ons. Us#ng a $r#gger means $he da$e 5orma$ #s onl" se$ *hen $he !h"s#cal da$abase connec$#on #s crea$ed $he 5#rs$ $#me. The $r#gger does no$ 5#re *hen subse=uen$ connec$ calls re$urn a cached connec$#on. & logon $r#gger can be crea$ed us#ng 3K.TPlus b" connec$#ng as a !r#'#leged da$abase user(
, +Flplu+ +y+tem/+y+tempwdPlocalho+t/IE

117

(onnecting to Orac&e /#ing O(I

Then run logontrig%sAl( Script !!" logontrig%sAl


create or replace tr0gger myG+etGdate after logon on data1a+e 1eg0n 0f :u+er - #$R#; then e<ecute 0mmed0ate #alter +e++0on +et nl+GdateGformat - ##JJJJ-OO-DD $$75:O9:SS## #( end 0f( end myG+etGdate( /

Th#s $r#gger se$s $he sess#onLs da$e 5orma$ e'er" $#me hr connec$s $o $he da$abase 5rom an" cl#en$ $ool. 7o$e $he use o5 s#ngle =uo$es. The da$e 5orma$ s$r#ng #s enclosed #n a !a#r o5 $*o =uo$es, *h#ch #s $he Oracle me$hod o5 nes$#ng s#ngle =uo$es #ns#de a =uo$ed s$r#ng. >n PHP, $he connec$#on 5unc$#on can s#m!l" become(
funct0on myGconnect:,un% ,pw% ,d1; @ ,c - oc0Gpconnect:,un% ,pw% ,d1;( return ,c( A

Oracle does all $he *or+ se$$#ng $he da$e 5orma$ *hen $he !h"s#cal da$abase connec$#on #s or#g#nall" es$abl#shed and 5#rs$ used. 0hen PHP la$er uses a cached connec$#on #$ *#ll alread" ha'e $he des#red da$e 5orma$.
,c - myGconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,+ - oc0Gpar+e:,c% #+elect +y+date from dual#;( oc0Ge<ecute:,+;( ,row - oc0GfetchGarray:,+% BC9GASSBC;( echo ,row3#SJSDATE#4 . . 1r"En.(

Th#s connec$s as hr and =uer#es $he curren$ da$e. >$ sho*s $he ne* 5orma$ se$ b" $he $r#gger(
7??T-?6-?5 2):6?:)6

>5 $he connec$#on #s an" user o$her $han hr $he s$andard de5aul$ da$e 5orma$ *#ll be d#s!la"ed(
?5-OAJ-?T

Us#ng a $r#gger l#+e $h#s onl" *or+s *hen $he re=u#red sess#on se$$#ng #s $he same 5or all PHP a!!l#ca$#on users $ha$ share $he same da$abase user name. >5 "ou canno$ use a $r#gger because each PHP #n'oca$#on needs d#55eren$ se$$#ngs, and "ou need more $han one sess#on 'alue changed, "ou can !u$ $he s$a$emen$s #ns#de a P.I3K. !rocedure. &5$er connec$#ng "ou can call $he P.I3K. !rocedure, *h#ch #s one oc0Ge<ecute:; call $o $he da$abase, #ns$ead o5 mul$#!le calls $o e/ecu$e AKTER SESS9BL s$a$emen$s. The sugges$ed !rac$#ce #s $o use KBWBL $r#ggers onl" 5or se$$#ng sess#on a$$r#bu$es and no$ 5or e/ecu$#ng !er PHP?connec$#on log#c such as cus$om logon aud#$#ng.

11

9anaging Per#i#tent (onnection#

8ana"in" Per'i'tent Connection'


Pers#s$en$ connec$#ons are grea$ #5 $he cos$ o5 o!en#ng a connec$#on #s h#gh. 0ha$ "ou cons#der h#gh de!ends on "our a!!l#ca$#on re=u#remen$s and on #m!lemen$a$#on #ssues such as *he$her $he *eb ser'er and da$abase are on $he same hos$, *h#ch *#ll a55ec$ $he $#me $a+en $o es$abl#sh a connec$#on, and on memor" a'a#lab#l#$". The dra*bac+ #s !ers#s$en$ connec$#ons use Oracle resources e'en *hen no one #s access#ng $he a!!l#ca$#on or da$abase. &nd #5 &!ache s!a*ns a number o5 ser'er !rocesses, each o5 $hem ma" ha'e #$s o*n se$ o5 connec$#ons $o $he da$abase. The !rol#5era$#on o5 connec$#ons can be con$rolled $o some e/$en$ *#$h php%ini d#rec$#'es and &!ache con5#gura$#on se$$#ngs. The cha!$er PHP Scalability and High Availability d#scusses connec$#on !ool#ng #n Oracle Da$abase 11g *h#ch !ro'#des an ad'anced solu$#on $o connec$#on managemen$.

8a&i4u4 Nu4)er o2 Per'i'tent Connection' A%%o9ed


oc0M.ma<Gper+0+tent

Th#s !arame$er l#m#$s $he number o5 !ers#s$en$ connec$#ons cached b" each &!ache?PHP !rocess. >$ #s a no$ a s"s$em?*#de res$r#c$#on on da$abase usage. 0hen $he l#m#$ #s reached b" a PHP !rocess, all oc0Gpconnect:; calls are $rea$ed l#+e oc0Gconnect:; calls and are closed a$ $he end o5 $he scr#!$. 3e$$#ng #$ $o -2 :$he de5aul$; means $here #s no l#m#$. >5 "our PHP scr#!$s connec$ us#ng $he same da$abase creden$#als, each PHP !rocess *#ll onl" ha'e one connec$#on en$r" #n #$s cache.

Ti4eout 2or ,nu'ed Per'i'tent Connection'


oc0M.per+0+tentGt0meout

Th#s !arame$er #s $he leng$h #n seconds $ha$ an &!ache !rocess ma#n$a#ns an #dle !ers#s$en$ connec$#on. 3e$$#ng $h#s !arame$er $o -2 :$he de5aul$; means $here #s no $#meou$. >5 a connec$#on has been e/!#red, $he ne/$ $#me oc0Gpconnect:; #s called a ne* connec$#on #s crea$ed. >$ #s no$ an as"nchronous $#mer. The e/!#r" chec+ ha!!ens *hene'er an" PHP scr#!$ 5#n#shes, regardless o5 *he$her O >8 calls *ere made. Th#s #s an unresol'able *ea+ness *#$h PHP( "ou *an$ #dle connec$#ons $o be closed, bu$ #5 PHP #s #dle $hen no scr#!$s e/ecu$e and $he $#meou$ #s no$ $r#ggered. .uc+#l" Oracle 11g connec$#on !ool#ng ma+es $h#s #ssue #rrele'an$.

Pin"in" 2or C%o'ed Per'i'tent Connection'


oc0M.p0ngG0nter/al

There #s no guaran$ee $ha$ $he connec$#on descr#!$or re$urned b" oc0Gpconnect:; re!resen$s a usable connec$#on $o $he da$abase. Dur#ng $he $#me PHP s$ored an unaccessed connec$#on resource #n #$s cache, $he connec$#on $o $he da$abase ma" ha'e become unusable due $o a ne$*or+ error, a da$abase error, or be#ng e/!#red b" $he D-&. >5 $h#s ha!!ens, oc0Gpconnect:; a!!ears $o be success5ul bu$ an error #s $hro*n *hen $he connec$#on #s la$er used, 5or e/am!le #n oc0Ge<ecute:;. The !#ng #n$er'al #s an eas" *a" $o #m!ro'e connec$#on rel#ab#l#$" 5or !ers#s$en$ connec$#ons.

11$

(onnecting to Orac&e /#ing O(I

Th#s !arame$er #s $he number o5 seconds $ha$ !ass be5ore O >8 does a !#ng dur#ng a oc0Gpconnect:; call. >5 $he !#ng de$erm#nes $he connec$#on #s no longer usable, a ne* connec$#on #s $rans!aren$l" crea$ed and re$urned b" oc0Gpconnect:;. To d#sable !#ng#ng, se$ $he 'alue $o -2. 0hen se$ $o ?, PHP chec+s $he da$abase each $#me oc0Gpconnect:; #s called. The de5aul$ 'alue #s F0 seconds. Regardless o5 $he 'alue o5 oc0M.p0ngG0nter/al, oc0Gpconnect:; *#ll al*a"s chec+ an #n$ernal Oracle cl#en$?s#de 'alue $o see #5 $he ser'er *as +no*n $o be a'a#lable $he las$ $#me an"$h#ng *as rece#'ed 5rom $he da$abase. Th#s #s a =u#c+ o!era$#on. 3e$$#ng oc0M.p0ngG0nter/al !h"s#call" sends a message $o $he ser'er, caus#ng a Mround?$r#!O o'er $he ne$*or+. Th#s #s a Mbad $h#ngO 5or scalab#l#$". Aood a!!l#ca$#on des#gn grace5ull" reco'ers 5rom 5a#lures. >n an" a!!l#ca$#on $here are a number o5 !o$en$#al !o#n$s o5 5a#lure #nclud#ng $he ne$*or+, $he hard*are and user ac$#ons such as shu$$#ng do*n $he da$abase. Oracle #$sel5 ma" be con5#gured $o close #dle connec$#ons and release $he#r da$abase resources. The da$abase adm#n#s$ra$or ma" ha'e #ns$alled user !ro5#les *#$h CREATE 8RB*9KE 9DKEGT9OEBUT, or $he Oracle ne$*or+ la"er ma" $#me ou$ $he ne$*or+. Hou need $o balance !er5ormance :no !#ngs; *#$h ha'#ng $o handle d#sconnec$ed Oracle sess#ons :or o$her changes #n $he Oracle en'#ronmen$; #n "our PHP code. 2or h#ghes$ rel#ab#l#$" and scalab#l#$" #$ #s generall" recommended $ha$ "ou do no$ use oc0M.p0ngG0nter/al, bu$ do error reco'er" #n "our a!!l#ca$#on code.

Apache Con2i"uration Para4eter'


Hou can also $une &!ache $o +#ll #dle !rocesses, *h#ch *#ll also 5ree u! Oracle resources used b" !ers#s$en$ connec$#ons. Table F l#s$s $he &!ache con5#gura$#on !arame$ers $ha$ can be used $o $une PHP.

#able 4" Apache con$iguration parameters%


Para4eter Oa<ReFue+t+8erCh0ld Oa<SpareSer/er+ CeepAl0/e Purpo'e 3e$s ho* man" re=ues$s &!ache *#ll ser'e be5ore res$ar$#ng. 3e$s ho* man" ser'ers $o +ee! #n memor" $ha$ are no$ handl#ng re=ues$s. De5#nes *he$her &!ache can ser'e a number o5 documen$s $o $he one user o'er $he same HTTP connec$#on.

3e$$#ng Oa<ReFue+t+8erCh0ld $oo lo* *#ll cause !ers#s$en$ connec$#ons $o be closed more o5$en $han !erha!s necessar", remo'#ng an" !o$en$#al !er5ormance ga#n o5 cach#ng. Man" s#$es use Oa<ReFue+t+8erCh0ld $o res$ar$ PHP occas#onall", a'o#d#ng an" !o$en$#al memor" lea+s or o$her un*an$ed beha'#ors. )ohn oggeshallLs ar$#cle )mproving Per$ormance #hrough Persistent (onnections d#scusses &!ache con5#gura$#on #n more de!$h.

Reducin" Data)a'e Ser.er 8e4or: ,'ed <: Per'i'tent Connection'


There are se'eral $echn#=ues $ha$ can be used #5 da$abase ser'er memor" #s l#m#$ed bu$ !ers#s$en$ connec$#ons are re=u#red 5or !er5ormance.

12%

9anaging Per#i#tent (onnection#


Use Oracle Da$abase 11g connec$#on !ool#ng. 3ee $he cha!$er on PHP Scalability and High Availability. 3e$ oc0M.per+0+tentGt0meout. @/!#re &!ache !rocesses *#$h &!ache con5#gura$#on !arame$ers. Reduce $he number o5 da$abase user creden$#als used b" $he a!!l#ca$#on. @ach !ers#s$en$ connec$#on $ha$ uses a d#55eren$ se$ o5 creden$#als *#ll crea$e a se!ara$e !rocess on $he da$abase hos$. >5 $he a!!l#ca$#on connec$s *#$h a large number o5 d#55eren$ schemas, $he number o5 !ers#s$en$ connec$#ons can be reduced b" connec$#ng as one user *ho has been gran$ed !erm#ss#on $o $he or#g#nal schemasG ob8ec$s. The a!!l#ca$#on can e#$her be recoded $o use e/!l#c#$ schema names #n =uer#es(
+elect H from oldu+er.myta1le(

Or, #5 $he a!!l#ca$#on #s large, $he 5#rs$ s$a$emen$ e/ecu$ed can se$ $he de5aul$ schema. 3ubse=uen$ =uer#es *#ll re$urn(
alter +e++0on +et currentG+chema - oldu+er( +elect H from myta1le(

3e$$#ng $he de5aul$ schema $h#s *a" re=u#res an e/$ra da$abase o!era$#on bu$, de!end#ng on $he a!!l#ca$#on, #$ ma" be bundled #n a P.I3K. bloc+ $ha$ does o$her o!era$#ons.

Orac%e Net and PHP


Oracle 7e$ has so!h#s$#ca$ed con$rol o'er ne$*or+ connec$#on managemen$ 5or bas#c connec$#'#$", !er5ormance and 5ea$ures such as encr"!$#on o5 ne$*or+ $ra55#c be$*een PHP and $he da$abase. Th#s sec$#on g#'es an o'er'#e* o5 some Oracle 7e$ 5ea$ures o5 #n$eres$ $o PHP a!!l#ca$#ons. Tun#ng $he O3, hard*are and T PI>P s$ac+ *#ll also subs$an$#all" hel! #m!ro'e !er5ormance and scalab#l#$". 3ome o5 $he Oracle 7e$ se$$#ngs are con5#gured #n a 5#le called sAlnet%ora $ha$ "ou can crea$e. 2or PHP, #$ should be !u$ #n $he same d#rec$or" as $he tnsnames%ora 5#le #5 "ou use one. O$her*#se, se$ $he TLSGADO9L en'#ronmen$ 'ar#able $o $he d#rec$or" con$a#n#ng sAlnet%ora. The da$abase ser'er can also ha'e a sAlnet%ora 5#le, *h#ch should be #n EORA(3E8HO6E<net&or5<admin. Th#s d#rec$or" on $he da$abase ser'er also con$a#ns listener%ora, a 5#le au$oma$#call" crea$ed dur#ng da$abase #ns$alla$#on, *h#ch con5#gures $he Oracle 7e$*or+ l#s$ener !rocess.

Connection Rate Li4itin"


.arge s#$es $ha$ ha'e abnormal s!#+es #n $he number o5 users connec$#ng can !re'en$ da$abase hos$ PU o'erload b" l#m#$#ng $he ra$e $ha$ connec$#ons can be es$abl#shed. The da$abase listener%ora 5#le can s!ec#5" a RATEGK9O9T clause $o se$ $he ma/#mum number o5 re=ues$s !er second $ha$ *#ll be ser'#ced(
K9STELER-:ADDRESSGK9ST:ADDRESS-:8RBTBCBK-tcp;:$BST-+ale+;:8BRT-2672;:RATEGK9O9T-5;;

121

(onnecting to Orac&e /#ing O(I

The 'alue *#ll de!end on $he hard*are #n use.

Settin" Connection Ti4eout'


2rom Oracle 10.2.0.P on*ards, "ou can s!ec#5" a connec$#on $#meou$ #n case $here #s ne$*or+ !roblem. Th#s le$s PHP re$urn an Oracle error $o $he user 5as$er, #ns$ead o5 a!!ear#ng $o MhangO. 3e$ SUKLET.BUTBBULDGCBLLECTGT9OEBUT #n $he cl#en$ s#de :$he PHP?s#de; sAlnet%ora 5#le. Th#s se$s $he u!!er $#me l#m#$ 5or es$abl#sh#ng a connec$#on r#gh$ $hrough $o $he da$abase, #nclud#ng $he $#me 5or a$$em!$s $o connec$ $o o$her da$abase ser'#ces. >n Oracle 11g, a sl#gh$l" l#gh$er?*e#gh$ solu$#on TC8.CBLLECTGT9OEBUT *as #n$roduced. >$ #s also a sAlnet%ora !arame$er. >$ bounds 8us$ $he T P connec$#on es$abl#shmen$ $#me, *h#ch #s mos$l" *here connec$#on !roblems occur.

Con2i"urin" Authentication 8ethod'


There are man" *a"s $o con5#gure connec$#on and au$hen$#ca$#on. 2or e/am!le a connec$#on(
,c - oc0Gconnect:.hr.% .hrpwd.% .a1c.;(

could be e'alua$ed b" Oracle 10g as us#ng $he @as" onnec$ s"n$a/ $o hos$ mach#ne abc :us#ng $he de5aul$ !or$ and da$abase ser'#ce; or us#ng a ne$ al#as abc con5#gured #n a tnsnames%ora 5#le. The 5le/#b#l#$" can cause a dela" #n ge$$#ng an error bac+ #5 $he connec$#on de$a#ls are #n'al#d or a da$abase #s no$ o!era$#onal. -o$h #n$ernal connec$#on me$hods ma" be $r#ed #n se=uence add#ng $o $he $#me dela" be5ore a PHP scr#!$ ge$s $he error. Th#s de!ends on "our Oracle 7e$ and D73 se$$#ngs. Ho* Oracle #s con5#gured $o au$hen$#ca$e $he userLs creden$#als :here a username and !ass*ord; can also ha'e an e55ec$. The #ssue #s no$ s!ec#5#c $o PHP. >n 3K.TPlus $he connec$#on(
, +Flplu+ hr/hrpwdPa1c

*ould be $he same. >n a bas#c Oracle 10g or 11g #ns$alla$#on, one *a" $o re$urn an error as soon as !oss#ble #s $o se$ $h#s #n "our O >8 sAlnet%ora 5#le(
LAOES.D9RECTBRJG8AT$ - :TLSLAOES; SUKLET.AUT$ELT9CAT9BLGSERV9CES - :LBLE;

Th#s D9RECTBRJG8AT$ 'alue d#sables @as" onnec$Ls ho+tname:port/+er/0ce s"n$a/. >ns$ead o5 us#ng @as" onnec$ s"n$a/, a connec$ name and tnsnames%ora 5#le *ould be needed. :Th#s ma" also add a small measure o5 secur#$" #5 "our scr#!$s acc#den$all" allo* arb#$rar" connec$#on #den$#5#ers. >$ s$o!s users guess#ng da$abase ser'er hos$names $he" should no$ +no* abou$.; 3e$$#ng AUT$ELT9CAT9BLGSERV9CES $o LBLE s$o!s d#55eren$ au$hen$#ca$#on me$hods be#ng $r#ed. &l$hough $h#s ma" !re'en$ !r#'#leged da$abase connec$#ons, *h#ch re=u#re o!era$#ng s"s$em au$hor#1a$#on, $h#s, aga#n, m#gh$ be bene5#c#al. hec+ $he Oracle 7e$ documen$a$#on 5or de$a#ls and 5or o$her au$hen$#ca$#on me$hods and au$hen$#ca$#on?$"!e s!ec#5#c $#meou$ !arame$ers.

122

Orac&e 6et and PHP

Detectin" Dead PHP Apache Se''ion'


>5 a PHP &!ache !rocess hangs, #$s da$abase ser'er !rocess *#ll no$ be closed. Th#s *#ll no$ !re'en$ o$her PHP !rocesses 5rom con$#nu#ng $o *or+, unless a re=u#red loc+ *as no$ released. The T P 6ee!al#'e 5ea$ure *#ll au$oma$#call" de$ec$ unusable connec$#ons based on $he o!era$#ng s"s$em $#meou$ se$$#ng, *h#ch #s $"!#call" some hours. Th#s de$ec$#on #s enabled on $he da$abase ser'er b" de5aul$. Oracle 7e$ #$sel5 can also be con5#gured $o de$ec$ dead connec$#ons. Th#s #s con5#gured b" SUKLET.EI89REGT9OE #n $he da$abase sAlnet%ora. & s$ar$#ng recommenda$#on #s $o se$ #$ $o 10 m#nu$es. >5 a dead or $erm#na$ed connec$#on #s #den$#5#ed, $he ser'er !rocess e/#$s. -o$h se$$#ngs *#ll use some resources. &'o#d se$$#ng $hem $oo shor$, *h#ch ma" #n$erru!$ normal user ac$#'#$".

Other Orac%e Net Opti4i;ation'


Oracle 7e$ le$s "ou $une a lo$ o5 o$her o!$#ons $oo. hec+ $he Oracle Get Services AdministratorJs Guide and $he Oracle Get Services Re$erence 5or de$a#ls and more 5ea$ures. & 5e* $#!s are men$#oned belo*. The bes$ sess#on da$a un#$ s#1e *#ll de!end on $he a!!l#ca$#on. &n 86 s#1e :$he ne* de5aul$ #n Oracle 11g; #s su#$able 5or man" a!!l#ca$#ons. >5 .O-s are used, a b#gger 'alue m#gh$ be be$$er. >$ should be se$ $he same 'alue #n bo$h $he da$abase ser'er sAlnet%ora and $he O >8 tnsnames%ora 5#le. 2or s#$es $ha$ ha'e a large number o5 connec$#ons be#ng made, $une $he UUEUES9]E o!$#on #n $he listener%ora 5#le. 6ee!#ng $he 8AT$ shor$ 5or $he oracle user on $he da$abase mach#ne can reduce $#me 5or 5or+#ng a da$abase ser'er !rocess. Th#s #s o5 mos$ bene5#$ 5or s$andard PHP connec$#ons. Reduc#ng $he number o5 en'#ronmen$ 'ar#ables also hel!s.

Tracin" Orac%e Net


3ome$#mes "our ne$*or+ #s $he bo$$lenec+. >5 "ou sus!ec$ $h#s #s $he case, $urn on Oracle 7e$ $rac#ng #n "our O >8 sAlnet%ora 5#le and see *here $#me #s be#ng s!en$. The e/am!le sAlnet%ora #n EORA(3E8HO6E<net&or5< admin<sample has some no$es on $he !arame$ers $ha$ hel!. 2or e/am!le, *#$h a USER le'el $race #n sAlnet%ora(
traceGle/elGcl0ent - USER traceGd0rectoryGcl0ent - /tmp

&nd $he PHP code(


,c - oc0Gconnect:.hr.% .hrpwd.% .Rc.;( // 0n/al0d d1 name

The $race 5#le, 5or e/am!le <tmp<cli8'2'2%trc, sho*s(


... 32?-OAJ-7??S ?>:65:6M:2??4 nnftmlfGmaNeG+y+temGaddrf0le: +y+tem name+ f0le 0+ ... 32?-OAJ-7??S ?>:66:??:M654 +nl0nWetAddr9nfo: Lame re+olut0on fa0led for Rc ...

123

(onnecting to Orac&e /#ing O(I

The le5$ hand column #s $he $#mes$am! o5 each lo* le'el call. Here, #$ sho*s a rela$#'el" b#g $#me dela" do#ng name resolu$#on 5or $he non?e/#s$en$ hos$ Rc. The cause #s $he con5#gura$#on o5 $he mach#ne ne$*or+ name resolu$#on. The logg#ng #n5ras$ruc$ure o5 Oracle 11g changed s#gn#5#can$l". .oo+ 5or $race5#les #n a sub?d#rec$or" o5 $he Oracle d#agnos$#c d#rec$or", 5or e/am!le #n EHO6E<oradiag8c;ones<diag<clients<user8c;ones 5or command l#ne PHP. 2or Oracle 7e$ log 5#les crea$ed b" a *eb ser'er runn#ng PHP, loo+ #n <root<oradiag8root #5 no o$her !a$h *as con5#gured.

Connection 8ana"e4ent in Sca%a)%e S:'te4'


Oracle ach#e'ed #$s *ell?+no*n scalab#l#$" #n !ar$ $hrough a mul$#?$hreaded arch#$ec$ure. PHP #ns$ead has a mul$#?!rocess arch#$ec$ure. Th#s d#55erence means care #s re=u#red *hen des#gn#ng scalable a!!l#ca$#ons. Us#ng !ers#s$en$ connec$#ons #s common 5or *eb s#$es $ha$ ha'e h#gh numbers o5 connec$#ons be#ng es$abl#shed. Reus#ng a !re'#ousl" o!ened connec$#on #s s#gn#5#can$l" 5as$er $han o!en#ng a 5resh one. .arge s#$es should benchmar+ Oracle 3hared 3er'ers also +no*n as MMul$# Threaded 3er'ersO :MT3;, and Oracle 11g connec$#on !ool#ng. 3ee $he cha!$er PHP Scalability and High Availability. Ma+e sure $ha$ "ou unders$and $he l#5e$#me o5 "our a!!l#ca$#ons connec$#ons. Reuse connec$#ons *here !oss#ble, bu$ do no$ be a5ra#d $o crea$e ne* connec$#ons. lose connec$#ons *hen no longer needed. @ach connec$#on *#ll $a+e some Oracle memor", so o'erall load can be reduced #5 #dle connec$#ons are closed *#$h &!ache !rocess $#meou$s or *#$h $he php%ini !arame$ers $o e/!#re !ers#s$en$ connec$#ons. 2or s#$es *#$h hundreds o5 connec$#ons a second, $une $he cache s#1e o5 an #n$ernal se=uence genera$or, +y+.aud+e+,. & s$ar$#ng !o#n$ #s $o change #$ $o !erha!s 2????(
SUK" alter +eFuence +y+.aud+e+, cache 2????(

Th#s #s also recommended #5 "ou are us#ng Oracle R& :MReal &!!l#ca$#on lus$ersO;. 2or bo$h R& and non?R& da$abase, $he DBOSGSERV9CE !ac+age le$s "ou s!ec#5" *or+load managemen$ goals. Th#s #s a de$a#led $o!#c< re5er $o OracleLs manuals 5or more #n5orma$#on. 2#nall", ma+e sure $ha$ "our a!!l#ca$#ons are as e55#c#en$ as !oss#ble. Th#s m#n#m#1es $he leng$h o5 $#me connec$#ons are held.

124

CHAPTER 1*

E7EC,TIN! S3L STATE8ENTS WITH OCI0


Th#s ha!$er d#scusses us#ng 3K. s$a$emen$s *#$h $he PHP O >8 e/$ens#on, #nclud#ng ho* s$a$emen$s are e/ecu$ed, $he 5unc$#ons a'a#lable, $ransac$#ons, $un#ng =uer#es, and some use5ul $#!s and $r#c+s.

S3L State4ent E&ecution Step'


Kuer#es us#ng $he O >8 e/$ens#on 5ollo* a model 5am#l#ar #n $he Oracle *orld( !arse, e/ecu$e and 5e$ch. 3$a$emen$s l#+e CREATE and 9LSERT re=u#re onl" !ars#ng and e/ecu$#ng. Pars#ng #s reall" 8us$ a !re!ara$or" s$e!, s#nce OracleLs ac$ual $e/$ !arse can occur a$ $he e/ecu$#on s$age. Hou can o!$#onall" bind local 'alues #n$o a s$a$emen$ s#m#lar $o $he *a" "ou use =+ !r#n$ 5orma$ s!ec#5#ca$#ons #n s$r#ngs. Th#s #m!ro'es !er5ormance and secur#$". Hou can also de$ine *here "ou *an$ $he resul$s $o be s$ored, bu$ almos$ all scr#!$s le$ $he O >8 5e$ch 5unc$#ons $a+e care o5 $h#s. The !oss#ble s$e!s are( 1. 2. P. E. 5. Par e( Pre!ares a s$a$emen$ 5or e/ecu$#on. 8,n.B O!$#onall" le$s "ou b#nd da$a 'alues, 5or e/am!le, #n $he &$ERE clause, 5or be$$er !er5ormance and secur#$". (e5,ne( O!$#onal s$e! allo*#ng "ou $o s!ec#5" *h#ch PHP 'ar#ables *#ll hold $he resul$s. Th#s #s no$ commonl" used. E"e#&teB The da$abase !rocesses $he s$a$emen$ and bu55ers an" resul$s. +et#'( Ae$s an" =uer" resul$s bac+ 5rom $he da$abase.

There #s no one?s$o! 5unc$#on $o do all $hese s$e!s #n a s#ngle PHP call, bu$ #$ #s $r#'#al $o crea$e one #n "our a!!l#ca$#on and "ou can $hen add cus$om error handl#ng re=u#remen$s. To sa5eguard 5rom run?a*a" scr#!$s, PHP *#ll $erm#na$e #5 a scr#!$ $a+es longer $han P0 seconds. >5 "our 3K. s$a$emen$s $a+e longer, change $he php%ini !arame$er ma<Ge<ecut0onGt0me or use $he +etGt0meGl0m0t:; 5unc$#on. Hou ma" also need $o al$er $he con5#gura$#on o5 "our *eb ser'er. 3#m#larl", #5 "ou are man#!ula$#ng large amoun$s o5 da$a, "ou ma" need $o #ncrease $he php%ini !arame$er memoryGl0m0t, *h#ch ca!s $he amoun$ o5 memor" each PHP !rocess can consume.

3uer: E&a4p%e
& bas#c =uer" #n O >8 #s( Script !2" Auery%php
!php 125

'xecuting S1, State7ent# With O(I

,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( ,+ - oc0Gpar+e:,c% #+elect c0ty% po+talGcode from locat0on+#;( oc0Ge<ecute:,+;( pr0nt # ta1le 1order-.2."#( wh0le :,row - oc0GfetchGarray:,+% BC9GLUODBC9GRETURLGLUKKS;; @ pr0nt # tr"#( foreach :,row a+ ,0tem; pr0nt # td"#.htmlent0t0e+:,0tem;.# /td"#( pr0nt # /tr"#( A pr0nt # /ta1le"#( oc0GfreeG+tatement:,+;( !"

The ou$!u$ 5rom $he scr#!$ Auery%php #s(

igure *-" Output $rom Auery%php% >n PHP, s#ngle and double =uo$es are used 5or s$r#ngs. 3$r#ngs *#$h embedded =uo$es can be made b" esca!#ng $he nes$ed =uo$es *#$h a bac+slash, or b" us#ng bo$h =uo$#ng s$"les. The ne/$ e/am!le sho*s s#ngle =uo$es around $he c#$" name. To ma+e $he =uer" a PHP s$r#ng #$, $here5ore, mus$ enclosed #n double =uo$es(
,+ - oc0Gpar+e:,c% .+elect H from locat0on+ where c0ty - #Sydney#.;(

PHP 5.P #n$roduces a LB&DBC s"n$a/ $ha$ #s use5ul 5or embedd#ng =uo$es and dollar s#gns #n s$r#ngs, such as $h#s e/am!le $ha$ =uer#es one o5 OracleGs adm#n#s$ra$#on '#e*s V,SUK(
,+Fl #ELD# +elect par+eGcall+% e<ecut0on+ from /,+Fl ELD( 126

S1, State7ent 'xecution Ste"#

,+ - oc0Gpar+e:,c% ,+Fl;( . . .

(reeing State&ents
>n long scr#!$s #$ #s recommended $o close s$a$emen$s *hen $he" are com!le$e(
oc0GfreeG+tatement:,+;(

Th#s allo*s resources $o be reused e55#c#en$l". 2or bre'#$", and because $he e/am!les e/ecu$e =u#c+l", mos$ code sn#!!e$s #n $h#s boo+ do no$ 5ollo* $h#s !rac$#ce.

Orac%e Datat:pe'
@ach column has a da$a$"!e, *h#ch #s assoc#a$ed *#$h a s!ec#5#c s$orage 5orma$. The common bu#l$?#n Oracle da$a$"!es are(

H&R ,&R H&R2 7UM-@R D&T@ T>M@3T&MP >7T@R,&. -.O.O-2>.@ NM.T"!e

The H&R, ,&R H&R2, 7UM-@R, D&T@, T>M@3T&MP and >7T@R,&. da$a$"!es are s$ored d#rec$l" #n PHP 'ar#ables. -.O-, .O-, and -2>.@ da$a$"!es use PHP descr#!$ors and are sho*n #n $he >sing 3arge Ob;ects in O()* cha!$er. NM.T"!es are re$urned as s$r#ngs or .O-s, as d#scussed #n $he >sing =63 &ith Oracle and PHP cha!$er. OracleGs 7 H&R, 7,&R H&R2, and 7 .O- $"!es are no$ su!!or$ed #n $he O >8 e/$ens#on.

=etch =unction'
There are a number o5 O >8 5e$ch 5unc$#ons, all documen$ed #n $he PHP Oracle O()* 6anual. Table C l#s$s $he 5unc$#ons.

127

'xecuting S1, State7ent# With O(I

#able -" O()* $etch $unctions%


OCI0 =unction oc0GfetchGall:; oc0GfetchGarray:; oc0GfetchGa++oc:; oc0GfetchGo1Zect:; oc0GfetchGrow:; oc0Gfetch:; Purpo'e Ae$s all $he resul$s a$ once. Ae$s $he ne/$ ro* as an arra" o5 "our cho#ce. Ae$s $he ne/$ ro* as an assoc#a$#'e arra". Ae$s a ne* ro* as an ob8ec$. Ae$s $he ne/$ ro* as an #n$eger #nde/ed arra". Used *#$h oc0Gre+ult:;, *h#ch re$urns $he resul$ o5 a g#'en 5#eld or *#$h oc0Gdef0neG1yGname:; *h#ch !rese$s *h#ch 'ar#able $he da$a *#ll be re$urned #n$o.

3ome o5 $he 5unc$#ons ha'e o!$#onal !arame$ers. Re5er $o $he PHP manual 5or more #n5orma$#on. The 5unc$#on commonl" used #s oc0GfetchGarray:;(
,rowarray - oc0GfetchGarray:,+tatement% ,mode;(

The mode #s o!$#onal. Table 8 l#s$s $he a'a#lable modes.

#able *" oci8$etch8array9: options%


Para4eter BC9GASSBC BC9GLUO BC9GBBT$ BC9GRETURLGLUKKS BC9GRETURLGKBBS Purpo'e Re$urn resul$s as an assoc#a$#'e arra". Re$urn resul$s as a numer#call" #nde/ed arra". Re$urn resul$s as bo$h assoc#a$#'e and numer#c arra"s. Th#s #s $he de5aul$. Re$urn PHP 7U.. 'alue 5or 7U.. da$a. Re$urn $he ac$ual .O- da$a #ns$ead o5 an O >? .O- resource.

Modes can be used $oge$her b" add#ng $hem(


,rowarray - oc0GfetchGarray:,+% BC9GLUO 1 BC9GRETURLGLUKKS;(

The oc0GfetchGa++oc:; and oc0GfetchGrow:; 5unc$#ons are s!ec#al cases o5 oc0GfetchGarray:;.

=etchin" a' a Nu4eric Arra:


& bas#c e/am!le $o 5e$ch resul$s #n a numer#call" #nde/ed PHP arra" #s(
,+ - oc0Gpar+e:,c% .+elect c0ty% po+talGcode from locat0on+.;( oc0Ge<ecute:,+;( wh0le :,re+ - oc0GfetchGarray:,+% BC9GLUO;; @ echo ,re+3?4 . . - . . ,re+324 . . 1r"En.( A 12

:etch :unction#

The $*o columns are #nde/ ? and #nde/ 2 #n $he resul$ arra". Th#s d#s!la"s(
Roma - ??>M> Ven0ce - 2?>)5 ToNyo - 2SM> $0ro+h0ma - SM7) SouthlaNe [ 7S2>7 . . .

3ome o5 $he 5e$ch 5unc$#ons do no$ re$urn 7U.. da$a b" de5aul$. Th#s can be $r#c+" *hen us#ng numer#call" #nde/ed arra"s. The resul$ arra" can a!!ear $o ha'e 5e*er columns $han selec$ed, and "ou canL$ al*a"s $ell *h#ch column *as 7U... @#$her use assoc#a$#'e arra"s so $he column names are d#rec$l" assoc#a$ed *#$h $he#r 'alues, or s!ec#5" $he BC9GRETURLGLUKKS 5lag(
,re+ - oc0GfetchGarray:,+% BC9GLUODBC9GRETURLGLUKKS;(

=etchin" a' an A''ociati.e Arra:


&ssoc#a$#'e arra"s are +e"ed b" $he u!!ercase column name.
,+ - oc0Gpar+e:,c% .+elect po+talGcode from locat0on+.;( oc0Ge<ecute:,+;( wh0le :,re+ - oc0GfetchGarray:,+% BC9GASSBC;; @ echo ,re+3.8BSTAKGCBDE.4 . . 1r"En.( A

Th#s d#s!la"s(
??>M> 2?>)5 2SM> SM7) 7S2>7 . . .

>n an assoc#a$#'e arra" $here #s no $able !re5#/ 5or $he column name. >5 "ou 8o#n $ables *here $he same column name occurs *#$h d#55eren$ mean#ngs #n bo$h $ables, use a column al#as #n $he =uer". O$her*#se onl" one o5 $he s#m#larl" named columns *#ll be re$urned b" PHP. Th#s con$r#'ed e/am!le selec$s $he reg0onG0d column $*#ce(
,+ - oc0Gpar+e:,c% .+elect reg0onGname% reg0on+.reg0onG0d as myre,% countryGname% countr0e+.reg0onG0d from countr0e+ 0nner Zo0n reg0on+ on countr0e+.reg0onG0d - reg0on+.reg0onG0d.;( oc0Ge<ecute:,+;( wh0le :,re+ - oc0GfetchGarray:,+% BC9GASSBC;; @ echo ,re+3.REW9BLGLAOE.4 . . . . ,re+3.OJREW.4 . . - . .

12$

'xecuting S1, State7ent# With O(I ,re+3.CBULTRJGLAOE.4 . . . . ,re+3.REW9BLG9D.4 . . . . . 1r"En.( A

The =uer" column al#as OJREW #s used as $he #nde/ $o $he resul$ arra" 5or one o5 $he reg0onG0d columns. The scr#!$ ou$!u$ #s(
Amer0ca+ A+0a ) Europe 2 Amer0ca+ Amer0ca+ . . . 7 - Argent0na 7 Au+tral0a ) - Belg0um 2 7 - BraQ0l 7 7 - Canada 7

=etchin" a' an O)6ect


2e$ch#ng as ob8ec$s allo*s !ro!er$"?s$"le access $o be used.
,+ - oc0Gpar+e:,c% #+elect H from locat0on+#;( oc0Ge<ecute:,+;( wh0le :,row - oc0GfetchGo1Zect:,+;; @ /arGdump:,row;( A

Th#s sho*s each ro* #s an ob8ec$ and g#'es #$s !ro!er$#es. The /arGdump:; 5unc$#on !r#n$s and au$oma$#call" 5orma$s $he 'ar#able ,row. Th#s 5unc$#on #s commonl" used 5or debugg#ng PHP scr#!$s. The ou$!u$ #s(
o1Zect:+tdCla++;R2 :S; @ 3.KBCAT9BLG9D.4-" +tr0ng:5; .2???. 3.STREETGADDRESS.4-" +tr0ng:7?; .27>T V0a Cola d0 R0e. 3.8BSTAKGCBDE.4-" +tr0ng:6; .??>M>. 3.C9TJ.4-" +tr0ng:5; .Roma. 3.STATEG8RBV9LCE.4-" LUKK 3.CBULTRJG9D.4-" +tr0ng:7; .9T. A . . .

>5 $he loo! #s changed $o(


wh0le :,row - oc0GfetchGo1Zect:,+;; @ echo .Addre++ 0+ . . ,row-"STREETGADDRESS . . 1r"En.( A

$he ou$!u$ #s(


Addre++ 0+ 27>T V0a Cola d0 R0e Addre++ 0+ >)?>2 Calle della Te+ta 13%

:etch :unction# Addre++ 0+ 7?2T Sh0nZuNu-Nu Addre++ 0+ >56? Cam0ya-cho Addre++ 0+ 7?25 Ya11erwocNy Rd . . .

De2inin" Output @aria)%e'


@/!l#c#$l" se$$#ng ou$!u$ 'ar#ables can be done *#$h oc0Gdef0neG1yGname:;. Th#s e/am!le 5e$ches c#$" names(
,+ - oc0Gpar+e:,c% #+elect c0ty from locat0on+#;( oc0Gdef0neG1yGname:,+% #C9TJ#% ,c0ty;( // column name 0+ upperca+e oc0Ge<ecute:,+;( wh0le :oc0Gfetch:,+;; @ echo .C0ty 0+ . . ,c0ty . . 1r"En.( A

The de5#ne #s done be5ore e/ecu$#on so Oracle +no*s *here $o s$ore $he ou$!u$. The column name #n $he oc0Gdef0neG1yGname:; call mus$ be #n u!!ercase. The resul$ #s(
C0ty 0+ C0ty 0+ C0ty 0+ C0ty 0+ C0ty 0+ . . . Roma Ven0ce ToNyo $0ro+h0ma SouthlaNe

The oc0Gdef0neG1yGname:; 5unc$#on has an o!$#onal $"!e !arame$er $ha$ #s use5ul, 5or e/am!le, $o s!ec#5" $ha$ $he PHP 'ar#able should be an #n$eger #ns$ead o5 a s$r#ng.

=etchin" and Wor>in" 9ith Nu4)er'


7umbers are 5e$ched as s$r#ngs b" O >8 *h#ch can ha'e #m!l#ca$#ons 5or subse=uen$ PHP ar#$hme$#c. &lso PHP and Oracle d#55er #n $he#r !rec#s#on so a cho#ce mus$ be made *here $o do calcula$#ons. >5 "our a!!l#ca$#on de!ends on numer#c accurac" *#$h 5#nanc#al da$a, do ar#$hme$#c #n Oracle 3K. or P.I3K., or cons#der us#ng PHPLs bcmath e/$ens#on. Th#s e/am!le sho*s ho* b" de5aul$ PHP 5e$ches numbers as s$r#ngs, and $he d#55erence be$*een do#ng ar#$hme$#c #n PHP and $he da$abase. 3K. s$a$emen$s $o crea$e $he number da$a are(
create ta1le dt :cn2 num1er% cn7 num1er;( 0n+ert 0nto dt :cn2% cn7; /alue+ :T2% T?.S;( comm0t(

PHP code $o 5e$ch $he ro* #s(


,+ - oc0Gpar+e:,c% .+elect cn2% cn7% cn2 - cn7 a+ d0ff from dt.;( oc0Ge<ecute:,+;( ,row - oc0GfetchGarray:,+% BC9GASSBC;( /arGdump:,row;(

The /arGdump:; 5unc$#on sho*s $he PHP da$a$"!e 5or numer#c columns #s +tr0ng(
131

'xecuting S1, State7ent# With O(I array:); @ 3.CL2.4-" +tr0ng:7; .T2. 3.CL7.4-" +tr0ng:5; .T?.S. 3.D9**.4-" +tr0ng:7; ..5. A

The con'ers#on 5rom number $o s$r#ng #s done b" Oracle and means Oracle 5orma$s $he da$a accord#ng $o #$s global#1a$#on se$$#ngs. >n some reg#ons $he dec#mal se!ara$or 5or numbers m#gh$ be a comma, caus#ng !roblems #5 PHP la$er cas$s $he s$r#ng $o a number 5or an ar#$hme$#c o!era$#on. OracleLs de5aul$ 5orma$s can be changed eas#l" and #$ #s recommended $o e/!l#c#$l" se$ $hem so "our scr#!$s are !or$able. 3ee Oracle Gumber ormats #n $he cha!$er Globali.ation. &r#$hme$#c calcula$#ons are handled *#$h d#55eren$ !rec#s#on #n PHP. The !re'#ous e/am!le sho*ed $he resul$ o5 $he sub$rac$#on *as $he e/!ec$ed 'alue. >5 $he code #s changed $o do $he sub$rac$#on #n PHP(
,row - oc0GfetchGarray:,+% BC9GASSBC;( ,d0ff - ,row3#CL2#4 - ,row3#CL7#4( echo .8$8 d0fference 0+ . . ,d0ff . .En.(

The ou$!u$ sho*s(


8$8 d0fference 0+ ?.5????????????2

PHP has a php%ini !arame$er prec0+0on *h#ch de$erm#nes ho* man" s#gn#5#can$ d#g#$s are d#s!la"ed #n 5loa$#ng !o#n$ numbers. -" de5aul$ #$ #s se$ $o 1E.

=etchin" and Wor>in" 9ith Date'


Oracle has ca!able da$e handl#ng 5unc$#onal#$", su!!or$#ng 'ar#ous needs. Da$es and $#mes *#$h user s!ec#5#ed !rec#s#ons can be s$ored. OracleGs da$e ar#$hme$#c ma+es calendar *or+ eas". D&T@, D&T@T>M@ and >7T@R,&. $"!es are 5e$ched 5rom Oracle as s$r#ngs, s#m#lar $o $he PHP re$urns OracleGs numer#c $"!es. The D&T@ $"!e has resolu$#on $o $he second bu$ $he de5aul$ 5orma$ #s o5$en 8us$ $he da", mon$h and "ear. Th#s e/am!le sho*s $he ou$!u$ 5rom a da$e column.
,+ - oc0Gpar+e:,c% .+elect h0reGdate from employee+ where employeeG0d - 7??.;( oc0Ge<ecute:,+;( ,row - oc0GfetchGarray:,+% BC9GASSBC;( echo .$0re date 0+ . . ,row3#$9REGDATE#4. .En.(

>n $h#s e/am!le $he de5aul$ 5orma$ *as $he Oracle &mer#can s$andard o5 DD?MO7?HH so $he ou$!u$ #s(
$0re date 0+ 2T-SE8-MT

Da$es #nser$ed are e/!ec$ed $o be #n $he de5aul$ 5orma$ $oo(


,+ - oc0Gpar+e:,c% .0n+ert 0nto mydt1 :dcol; /alue+ :#?5-AUW-?T#;.;( oc0Ge<ecute:,+;(

132

:etch :unction#

The de5aul$ 5orma$ can be changed *#$h OracleGs global#1a$#on se$$#ngs be5ore or a5$er PHP s$ar$s. 3ee $he cha!$er Globali.ation% Regardless o5 $he de5aul$, an" s$a$emen$ can use #$s o*n cus$om 5orma$. 0hen =uer"#ng, use $he TBGC$AR:; 5unc$#on. 0hen #nser$#ng, use TBGDATE:;(
// 0n+ert a date ,+ - oc0Gpar+e:,c% .0n+ert 0nto mydt1 :dcol; /alue+ :toGdate:#7??S/?2/?2 ?6:)S:6?#% #JJJJ/OO/DD $$:O9:SS#;;.;( oc0Ge<ecute:,+;( // fetch a date ,+ - oc0Gpar+e:,c% .+elect toGchar:dcol% #DD/OO/JJ#; a+ dcol from mydt1.;( oc0Ge<ecute:,+;( ,row - oc0GfetchGarray:,+% BC9GASSBC;( echo .Date 0+ . . ,row3.DCBK.4 . .En.(

The ou$!u$ #s(


Date 0+ ?2/?2/?S

To 5#nd $he curren$ da$abase ser'er $#me, use $he SJSDATE 5unc$#on. Here $he da$e and $#me re$urned b" SJSDATE are d#s!la"ed(
,+ - oc0Gpar+e:,c% .+elect toGchar :+y+date% #JJJJ-OO-DD $$75:O9:SS#; a+ now from dual.;( ,r - oc0Ge<ecute:,+;( ,row - oc0GfetchGarray:,+% BC9GASSBC;( echo .T0me 0+ . . ,row3.LB&.4 . .En.(

The ou$!u$ #s(


T0me 0+ 7??T-?M-?2 26:7M:55

OracleGs T>M@3T&MP $"!e s$ores 'alues !rec#se $o 5rac$#onal seconds. Hou can o!$#onall" s$ore a $#me 1one or local $#me 1one. 2or PHP, $he local $#me 1one *ould be $he $#me 1one o5 $he *eb ser'er, *h#ch ma" no$ be rele'an$ $o user loca$ed remo$el". 2or an e/am!le, SJST9OESTAO8, *h#ch #s analogous $o SJSDATE, g#'es $he curren$ ser'er $#me s$am! and $#me 1one(
,+ ,r ,row echo oc0Gpar+e:,c% .+elect +y+t0me+tamp from dual.;( oc0Ge<ecute:,+;( - oc0GfetchGarray:,+% BC9GASSBC;( .T0me 0+ . . ,row3.SJST9OESTAO8.4 . .En.(

The ou$!u$ #s(


T0me 0+ ?2-AUW-?T ?).7M.55.7))MMT 8O -?T:??

&n >7T@R,&. $"!e re!resen$s $he d#55erence be$*een $*o da$e 'alues. >n$er'als are use5ul 5or OracleGs anal"$#c 5unc$#ons. >n PHP $he" are 5e$ched as s$r#ngs, l#+e D&T@ and T>M@3T&MP are 5e$ched.

133

'xecuting S1, State7ent# With O(I

In'ertB ,pdateB De%eteB Create and Drop


@/ecu$#ng Da$a De5#n#$#on .anguage :DD.; and Da$a Man#!ula$#on .anguage :DM.; s$a$emen$s, l#+e CREATE and 9LSERT, s#m!l" re=u#res a !arse and e/ecu$e(
,+ - oc0Gpar+e:,conn% .create ta1le 02te+t :col2 num1er;.;( oc0Ge<ecute:,+;(

The onl"?run?once #ns$alla$#on sec$#ons o5 a!!l#ca$#ons should con$a#n almos$ all $he CREATE TABKE s$a$emen$s used. &!!l#ca$#ons #n Oracle do no$ commonl" need $o crea$e $em!orar" $ables a$ run $#me, and #$ #s e/!ens#'e $o do so. Use #nl#ne '#e*s, or 8o#n $ables *hen re=u#red. >n some cases Mglobal temporary tablesO m#gh$ be use5ul bu$ one ca'ea$ #s $ha$ $em!orar" $ables ha'e no s$a$#s$#cs 5or $he Oracle o!$#m#1er $o e'alua$e.

Tran'action'
Us#ng $ransac$#ons $o !ro$ec$ $he #n$egr#$" o5 da$a #s as #m!or$an$ #n PHP as an" o$her rela$#onal da$abase a!!l#ca$#on. @/ce!$ #n s!ec#al cases, "ou *an$ e#$her all "our changes $o be comm#$$ed, or none o5 $hem. Unnecessar#l" comm#$$#ng or roll#ng bac+ #m!ac$s da$abase !er5ormance as #$ causes unnecessar" ne$*or+ $ra55#c :round trips; be$*een PHP and $he da$abase.

igure **" Each round trip bet&een PHP and the Database reduces scalability% >$ #s also causes e/$ra !rocess#ng and more >O $o $he da$abase 5#les. To ma/#m#1e e55#c#enc", use $ransac$#ons *here a!!ro!r#a$e. O >8Ls de5aul$ comm#$ beha'#or #s l#+e o$her PHP e/$ens#ons and d#55eren$ 5rom OracleLs s$andard. The de5aul$ mode o5 oc0Ge<ecute:; #s BC9GCBOO9TGBLGSUCCESS $o comm#$ changes. Th#s can eas#l" be o'err#dden #n O >8. -u$ $a+e care *#$h comm#$$#ng and roll#ng bac+. H#dden $ransac$#onal cons#s$enc" !roblems can be crea$ed b" no$ unders$and#ng *hen comm#$s or rollbac+s occur. 3uch !roblems ma" no$ be a!!aren$ #n normal cond#$#ons, bu$ an abnormal e'en$ m#gh$ cause onl" !ar$ o5 a $ransac$#on $o be comm#$$ed. Problems can also be caused b" !rogrammers $r"#ng $o s=uee1e ou$ absolu$el" o!$#mal !er5ormance b" comm#$$#ng or roll#ng bac+ onl" *hen absolu$el" necessar".

134

-ran#action#

3cr#!$s $ha$ call a connec$#on 5unc$#on more $han once *#$h $he same creden$#als should ma+e sure $ransac$#ons are com!le$e be5ore re?connec$#ng. 3#m#larl", be care5ul a$ $he end o5 sco!e #5 $he $ransac$#on s$a$e #s uncomm#$$ed. >n $he 5ollo*#ng e/am!le a ne* record #s comm#$$ed *hen $he oc0Ge<ecute:; call #s called(
,+ - oc0Gpar+e:,c% .0n+ert 0nto te+tta1le /alue+ :#my data#;.;( oc0Ge<ecute:,+;( // automat0cally comm0tted

O$her users o5 $he $able *#ll #mmed#a$el" be able $o see $he ne* record. &u$o?comm#$$#ng can be hand" 5or s#ngle 9LSERTs and U8DATEs, bu$ $ransac$#onal and !er5ormance re=u#remen$s should be $hough$ abou$ be5ore us#ng $he de5aul$ mode e'er"*here. Hou s!ec#5" no$ $o au$o?comm#$ bu$ $o beg#n a $ransac$#on *#$h(
,+ - oc0Gpar+e:,c% .0n+ert 0nto te+tte+t /alue+ :#my data 7#;.;( oc0Ge<ecute:,+% BC9GDE*AUKT;( // not comm0tted

The PHP !arame$er name BC9GDE*AUKT #s borro*ed 5rom OracleLs all >n$er5ace, *here $he 'alue o5 $he macro *#$h $he same name #s ac$uall" $he de5aul$ 'alue *hen no$h#ng else #s s!ec#5#ed. >n PHP a be$$er name *ould ha'e been LBGAUTBGCBOO9T, bu$ *e are s$uc+ *#$h $he a*+*ard name. To comm#$ an" un?comm#$$ed $ransac$#ons 5or "our connec$#on, do(
oc0Gcomm0t:,c;(

To rollbac+, do(
oc0Groll1acN:,c;(

&n" ou$s$and#ng $ransac$#on #s au$oma$#call" rolled bac+ *hen a connec$#on #s closed or a$ $he end o5 $he scr#!$.

Note: 8e careful mi>in& and matchin& oc0Ge<ecute:; calls with both commit modes in one script, since you may commit at incorrect times. *n particular, e>ecutin& a :uery will commit an outstandin& transaction if BC9GDEA*UKT is not used in the :uery?s oc0Ge<ecute:; call. Any CREATE or DRB8 statement will automatically commit re&ardless of the oc0Ge<ecute:; mode. This is a feature of the +racle database that cannot be altered.

>5 all "our da$abase calls #n a scr#!$ are =uer#es, or are calls $o P.I3K. !ac+ages $ha$ handle $ransac$#ons #n$ernall", use(
oc0Ge<ecute:,+;(

>5 "ou !ass BC9GDE*AUKT, PHP *#ll send an e/!l#c#$ rollbac+ $o $he da$abase a$ $he end o5 e'er" scr#!$, e'en $hough #$ #s unnecessar".

135

'xecuting S1, State7ent# With O(I

Autono4ou' Tran'action'
OracleLs !rocedural language 5or 3K., P.I3K., allo*s "ou $o do au$onomous $ransac$#ons, *h#ch are e55ec$#'el" sub?$ransac$#ons. &n au$onomous $ransac$#on can be comm#$$ed or rolled bac+ *#$hou$ a55ec$#ng $he ma#n $ransac$#on. Th#s m#gh$ be use5ul 5or logg#ng da$a access ? an aud#$ record can be #nser$ed e'en #5 $he user dec#des $o rollbac+ $he#r ma#n change. &n e/am!le #s( Script !'" logger%sAl
drop ta1le myta1le( drop ta1le logta1le( create ta1le myta1le :c2 /archar7:2?;;( create ta1le logta1le :e/ent /archar7:)?;;( create or replace procedure updatelog:pGe/ent 0n /archar7; a+ pragma autonomou+Gtran+act0on( 1eg0n 0n+ert 0nto logta1le :e/ent; /alue+:pGe/ent;( comm0t( end( /

Hou could call $he P.I3K. 5unc$#on 5rom PHP $o log e'en$s( Script !0" logger%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,+ - oc0Gpar+e:,c% .0n+ert 0nto myta1le /alue+ :#a1c#;.;( oc0Ge<ecute:,+% BC9GDE*AUKT;( // don#t comm0t ,+ - oc0Gpar+e:,c% .1eg0n updatelog:#9LSERT attempted#;( end(.;( oc0Ge<ecute:,+% BC9GDE*AUKT;( // don#t comm0t oc0Groll1acN:,c;( !"

@'en $hough BC9GDE*AUKT #s used, $he au$onomous $ransac$#on comm#$s $o $he log $able. Th#s comm#$ does no$ comm#$ $he scr#!$ #nser$ #n$o mytable. &5$er runn#ng logger%php, $he $ables con$a#n(
SUK" +elect H from myta1le( no row+ +elected SUK" +elect H from logta1le( EVELT -----------------------------9LSERT attempted

136

-ran#action#

The Tran'actiona% <eha.ior o2 Connection'


To see $he $ransac$#onal beha'#or o5 $he $hree connec$#on 5unc$#ons, use 3K.TPlus $o crea$e a $able *#$h a s#ngle da$e column(
SUK" create ta1le myta1le :col2 date;(

Rerun transactions%php a 5e* $#mes, chang#ng oc0Gconnect:; $o oc0GnewGconnect:; and oc0Gpconnect:;( Script !," transactions%php
!php funct0on doGFuery:,c% ,Fuery; @ ,+ - oc0Gpar+e:,c% ,Fuery;( oc0Ge<ecute:,+% BC9GDE*AUKT;( oc0GfetchGall:,+% ,re+;( echo . pre".( /arGdump:,re+;( // 8$8 de1ugg0ng funct0on for d0+play0ng output echo . /pre".( A ,c2 - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( // f0r+t connect0on ,+ - oc0Gpar+e:,c2% .0n+ert 0nto myta1le /alue+ :#. . date:#Z:O:y#; . .#;.;( oc0Ge<ecute:,+% BC9GDE*AUKT;( // doe+ not comm0t doGFuery:,c2% .+elect H from myta1le.;( ,c7 - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( doGFuery:,c7% .+elect H from myta1le.;( !" // +econd connect0on

The scr#!$ #nser$s :bu$ does no$ comm#$; us#ng one connec$#on and =uer#es bac+ $he resul$s *#$h $he or#g#nal and a second connec$#on. Us#ng an oc0Gconnect:; connec$#on le$s "ou =uer" $he ne*l" #nser$ed :bu$ uncomm#$$ed; da$a bo$h $#mes because ,c2 and ,c7 re5er $o $he same Oracle connec$#on. Us#ng oc0Gpconnect:; #s $he same as oc0Gconnect:;. The ou$!u$ #s(
array:2; @ 3.CBK2.4-" array:2; @ 3?4-" +tr0ng:>; .2S-YUL-?T. A A array:2; @ 3.CBK2.4-" array:2; @ 3?4-" +tr0ng:>; .2S-YUL-?T. A 137

'xecuting S1, State7ent# With O(I A

Us#ng oc0GnewGconnect:; 5or ,c7 g#'es a ne* connec$#on *h#ch canno$ see $he uncomm#$$ed da$a. The ou$!u$ sho*s $he second =uer" does no$ 5e$ch an" ro*s(
array:2; @ 3.CBK2.4-" array:2; @ 3?4-" +tr0ng:>; .2S-YUL-?T. A A array:2; @ 3.CBK2.4-" array:?; @ A A

PHP Error Hand%in"


The PHP #ns$alla$#on cha!$er recommended se$$#ng d0+playGerror+ $o Bn #n php%ini $o a#d debugg#ng. Hou m#gh$ cons#der also enabl#ng $he EGSTR9CT le'el 5or $he errorGreport0ng !arame$er so "ou can ca$ch an" !o$en$#al !roblems $ha$ *#ll cause u!grade #ssues. >n a !roduc$#on s"s$em "ou should ma+e sure error ou$!u$ #s logged #ns$ead o5 d#s!la"ed. Hou do no$ *an$ $o lea+ #n$ernal #n5orma$#on $o *eb users, and "ou do no$ *an$ a!!l#ca$#on !ages con$a#n#ng ugl" error messages. @rror handl#ng can also be con$rolled a$ run$#me. 2or e/am!le, $o see all errors d#s!la"ed, scr#!$s can se$(
errorGreport0ng:EGAKK;( 0n0G+et:#d0+playGerror+#% true;(

De!end#ng on $he php%ini 'alue o5 d0+playGerror+, "ou m#gh$ cons#der us#ng PHPLs P !re5#/ $o com!le$el" su!!ress au$oma$#c d#s!la" o5 5unc$#on errors, al$hough $h#s #m!ac$s !er5ormance(
,c - Poc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;(

To $ra! ou$!u$ and reco'er 5rom errors, PHPLs ou$!u$ bu55er#ng 5unc$#ons ma" be use5ul. >5 an error occurs !ar$ *a" dur#ng crea$#on o5 $he HTM. !age be#ng ou$!u$, $he !ar$#all" com!le$e !age con$en$s can be d#scarded and an error !age crea$ed #ns$ead.

Hand%in" OCI0 Error'


The error hand#ng o5 an" sol#d a!!l#ca$#on re=u#res care5ul des#gn. @/!ec$ $he une/!ec$ed. hec+ all re$urn codes. Oracle ma" piggy+bac5 calls $o $he da$abase $o o!$#m#1e !er5ormance. Th#s means $ha$ errors ma" occur dur#ng la$er O >8 calls $han "ou m#gh$ e/!ec$. To d#s!la" O >8 errors, use $he oc0Gerror:; 5unc$#on. The 5unc$#on re=u#res a d#55eren$ argumen$ de!end#ng on $he call#ng con$e/$, as sho*n la$er. >$ re$urns an arra".

13

PHP 'rror Hand&ing

#able ?" Error array a$ter Ee K oci8error9:%


@aria)%e ,e3.code.4 ,e3.me++age.4 ,e3.off+et.4 ,e3.+Flte<t.4 De'cription Oracle error number Oracle error message olumn !os#$#on #n $he 3K. s$a$emen$ o5 $he error The $e/$ o5 $he 3K. s$a$emen$

2or #n5orma$#on on ge$$#ng e/$ra #n5orma$#on 5or errors dur#ng crea$#on o5 P.I3K. !rocedures, see $he cha!$er >sing P3<S@3 &ith O()*%

$")* "onnection !rrors


2or connec$#on errors, no argumen$ $o oc0Gerror:; #s needed(
,c - oc0Gconnect:.hr.% .notGhrpwd.% .localho+t/IE.;( 0f :',c; @ ,e - oc0Gerror:;( // Lo parameter pa++ed /arGdump:,e;( A

0#$h $he #n'al#d !ass*ord, $he ou$!u$ #s(


array:5; @ 3.code.4-" 0nt:2?2T; 3.me++age.4-" +tr0ng:6?; .BRA-?2?2T: 0n/al0d u+ername/pa++word( logon den0ed. 3.off+et.4-" 0nt:?; 3.+Flte<t.4-" +tr0ng:?; .. A

The ou$!u$ sho*s $ha$ ,e #s an arra". The code en$r" 2?2T ma$ches $he error number #n $he error message. 3#nce $here *as no 3K. s$a$emen$ #n'ol'ed, $he +Flte<t #s em!$" and $he off+et #s ?. The #n$ernal re#m!lemen$a$#on o5 O >8 1.P connec$#on managemen$ ma+es #$ be$$er a$ au$oma$#call" reco'er#ng 5rom da$abase una'a#lab#l#$" errors. @'en so, *#$h !ers#s$en$ connec$#ons PHP can re$urn a cached connec$#on *#$hou$ +no*#ng #5 $he da$abase #s s$#ll a'a#lable. >5 $he da$abase has res$ar$ed s#nce $he $#me o5 5#rs$ connec$#on, or $he D-& had enabled resource managemen$ $ha$ l#m#$ed $he ma/#mum connec$#on $#me 5or a user, or e'en #5 $he D-& #ssued an AKTER SESS9BL C9KK command $o close $he userGs da$abase sess#on, an O >8 call m#gh$ re$urn an Oracle error *hen #$ $r#es $o reuse a !ers#s$en$ connec$#on. Ho*e'er O >8 *#ll $hen mar+ $he connec$#on as #n'al#d and $he ne/$ $#me $he &!acheIPHP !rocess $r#es $o connec$, a ne* connec$#on *#ll be success5ull" crea$ed and usable. 2or e/am!le, cons#der a scr#!$ $ha$ uses a !ers#s$en$ connec$#on(
!php

13$

'xecuting S1, State7ent# With O(I

,c - oc0Gpconnect:#hr#% #hrpwd#% #localho+t/IE#;( 0f :',c; @ ,e - oc0Gerror:;( echo ,e3#me++age#4 . . 1r"En.( e<0t( A ... !"

&5$er $he scr#!$ com!le$es, #5 $he D-& #ssues an AKTER SESS9BL C9KK command 5or $he da$abase connec$#on crea$ed, $he ne/$ $#me $he scr#!$ #s run #$ *#ll d#s!la" $he error(
BRA-???7M: your +e++0on ha+ 1een N0lled

Ho*e'er #5 $he scr#!$ #s run a $h#rd $#me #$ *#ll connec$ and run $o com!le$#on normall".

$")* Parse !rrors


2or !arse errors, !ass oc0Gerror:; $he connec$#on resource(
,+ - oc0Gpar+e:,c% .+elect c0ty from locat0on+.;( 0f :',+; @ ,e - oc0Gerror:2c;( // Connect0on re+ource pa++ed echo ,e3.me++age.4 . . 1r"En.( A

$")* !xecution and (etching !rrors


2or e/ecu$#on and 5e$ch#ng errors, !ass oc0Gerror:; $he s$a$emen$ resource(
,rc - oc0Ge<ecute:,+;( 0f :',rc; @ ,e - oc0Gerror:2s;( /arGdump:,e;( A

// Statement re+ource pa++ed

,rc - oc0GfetchGall:,+% ,re+ult+;( 0f :',rc; @ ,e - oc0Gerror:2s;( // Statement re+ource pa++ed /arGdump:,e;( A

&n e/am!le o5 an e/ecu$#on error #s *hen $he $able be#ng =uer#ed does no$ e/#s$(
,+ - oc0Gpar+e:,c% .+elect c0ty from notGlocat0on+.;( ,rc - oc0Ge<ecute:,+;( 0f :',rc; @ ,e - oc0Gerror:,+;( /arGdump:,e;( A 14%

PHP 'rror Hand&ing

The ou$!u$ sho*s $he message number and $e/$. >$ also con$a#ns $he $e/$ o5 $he s$a$emen$ and $he column o55se$ !os#$#on o5 $he error #n $ha$ s$a$emen$. olumn 1C #s $he $able name notGlocat0on+.
array:5; @ 3.code.4-" 0nt:>57; 3.me++age.4-" +tr0ng:)>; .BRA-??>57: ta1le or /0ew doe+ not e<0+t. 3.off+et.4-" 0nt:2T; 3.+Flte<t.4-" +tr0ng:)?; .+elect c0ty from notGlocat0on+. A

Tunin" S3L State4ent' in PHP App%ication'


Tun#ng #s an ar$ and a sc#ence. The da$abase?cen$r#c a!!roach $o $un#ng #s $o 5#rs$ $une $he a!!l#ca$#on, ne/$ $une $he 3K., and 5#nall" $une $he da$abase. 3K. $un#ng and Da$abase $un#ng are co'ered #n $he Oracle documen$a$#on. 3K. $un#ng *#ll hel! ma+e e55#c#en$ use o5 $able des#gn, #nde/es and $he Oracle o!$#m#1er. Da$abase $un#ng ma/#m#1es $hrough!u$ and allo*s e55#c#en$ use o5 bu55er#ng and >IO s$ra$eg#es. On $he O >8 s#de, $ransac$#on managemen$ can hel! reduce round $r#!s 5rom PHP $o $he da$abase and reduce !rocess#ng o'erheads. Ma+e sure $o comm#$ onl" *hen necessar". >$ #s almos$ al*a"s more e55#c#en$ $o selec$ $he m#n#mum amoun$ o5 da$a necessar" and onl" re$urn ro*s and columns $ha$ *#ll be used b" PHP. ons#der us#ng P.I3K. !ac+ages 5or com!le/ da$a o!era$#ons $o m#n#m#1e da$a $rans5ers. @55#c#en$ cach#ng o5 s$a$emen$s b" ha'#ng reusable code and us#ng b#nd 'ar#ables are 5undamen$al #n #m!ro'#ng da$abase !er5ormance.

,'in" <ind @aria)%e'


-#nd 'ar#ables are 8us$ l#+e =+ !r#n$ 5orma$ s!ec#5#ers. The" le$ "ou re?e/ecu$e a s$a$emen$ *#$h d#55eren$ 'alues 5or $he 'ar#ables and ge$ d#55eren$ resul$s. >n $he PHP commun#$" s$a$emen$s l#+e $h#s are +no*n as !re!ared s$a$emen$s. >5 "ou do no$ b#nd, Oracle mus$ re!arse and cache mul$#!le s$a$emen$s.

141

'xecuting S1, State7ent# With O(I

igure *?" Got binding &astes database resources% -#nd#ng #s highly recommended. >$ can #m!ro'e o'erall da$abase $hrough!u$. Oracle #s more l#+el" $o 5#nd $he ma$ch#ng s$a$emen$ #n #$s cache and be able $o reuse $he e/ecu$#on !lan and con$e/$ 5or $ha$ s$a$emen$, e'en #5 someone else or#g#nall" e/ecu$ed #$.

igure ?2" Cinding improves per$ormance and security% -#nd 'ar#ables are also an #m!or$an$ *a" $o !re'en$ 3K. #n8ec$#on secur#$" a$$ac+s. 3K. #n8ec$#on ma" occur *hen 3K. s$a$emen$s are hard coded $e/$ conca$ena$ed *#$h user #n!u$(
,w - .u+er0d - 2.( ,+ - oc0Gpar+e:,c% .+elect H from myta1le where ,w.;(

>5 $he user #n!u$ #s no$ care5ull" chec+ed, $hen #$ ma" be !oss#ble 5or a mal#c#ous user $o e/ecu$e a 3K. s$a$emen$ o5 $he#r cho#ce #ns$ead o5 $he one "ou #n$ended. >n Oracle, a b#nd 'ar#able #s a colon?!re5#/ed name #n $he 3K. $e/$. & oc0G10ndG1yGname:; call $ells Oracle *h#ch PHP 'ar#able $o ac$uall" use *hen e/ecu$#ng $he s$a$emen$.

142

-uning S1, State7ent# in PHP 0""&ication#

Script !4" bindvar%php


!php ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( ,+ - oc0Gpar+e:,c% .+elect la+tGname from employee+ where employeeG0d - :e0d1/.;( ,mye0d - 2?2( oc0G10ndG1yGname:,+% .:e0d1/.% ,mye0d;( oc0Ge<ecute:,+;( ,row - oc0GfetchGarray:,+% BC9GASSBC;( echo .Ka+t name 0+: .. ,row3#KASTGLAOE#4 .. 1r"En.( !"

The ou$!u$ #s $he las$ name o5 em!lo"ee 101(


Ka+t name 0+: Cochhar

There #s no need $o :and 5or e55#c#enc" "ou should no$; re?!arse $he 3K. s$a$emen$ #5 "ou 8us$ *an$ $o change $he 'alue o5 $he b#nd 'ar#able. The 5ollo*#ng code *ould *or+ *hen a!!ended $o $he end o5 bindvar%php(
// Lo need to re-par+e or re-10nd ,mye0d - 2?7( oc0Ge<ecute:,+;( ,row - oc0GfetchGarray:,+% BC9GASSBC;( echo .Ka+t name 0+: .. ,row3#KASTGLAOE#4 .. 1r"En.(

Re?runn#ng bindvar%php no* g#'es(


Ka+t name 0+: Cochhar Ka+t name 0+: De $aan

Hou can b#nd a s#ngle 'alue *#$h each oc0G10ndG1yGname:; call. Mul$#!le 'alues can be bound *#$h ano$her 5unc$#on, oc0G10ndGarrayG1yGname:;, and !assed $o P.I3K. bloc+s. Th#s #s d#scussed #n $he

cha!$er on P.I3K.. The s"n$a/ o5 oc0G10ndG1yGname:; #s(

,rc - oc0G10ndG1yGname:,+tatement% ,10nd/arname% ,php/ar0a1le% ,length% ,type;

The leng$h and $"!e are o!$#onal. The de5aul$ $"!e #s $he s$r#ng $"!e, 3K.TJ HR. Oracle *#ll con'er$ mos$ bas#c $"!es $o or 5rom $h#s as needed. 2or e/am!le *hen b#nd#ng a number, "ou can om#$ $he $"!e !arame$er. >$ #s recommended $o !ass $he leng$h #5 a =uer" #s be#ng re?e/ecu$ed #n a scr#!$ *#$h d#55eren$ b#nd 'alues. 2or e/am!le *hen b#nd#ng, !ass $he leng$h o5 $he larges$ !o$en$#al s$r#ng. Pass#ng $he leng$h also a'o#ds !o$en$#al edge?case beha'#or d#55erences #5 a scr#!$ runs *#$h mul$#!le d#55eren$ Oracle charac$er se$s. 3ome older PHP e/am!les use ` *#$h oc0G10ndG1yGname:; !arame$ers. Do no$ do $h#s. 3#nce a call? b"?re5erence clean u! #n $he #m!lemen$a$#on o5 PHP, $h#s s"n$a/ has been de!reca$ed and ma" cause !roblems.
143

'xecuting S1, State7ent# With O(I

& b#nd call $ells Oracle *h#ch memor" address $o read da$a 5rom. Tha$ address needs $o con$a#n 'al#d da$a *hen oc0Ge<ecute:; #s called. >5 $he b#nd call #s made #n a d#55eren$ sco!e 5rom $he e/ecu$e call $here could be a !roblem. 2or e/am!le, #5 $he b#nd #s #n a 5unc$#on and a 5unc$#on?local PHP 'ar#able #s bound, $hen Oracle ma" read an #n'al#d memor" loca$#on #5 $he e/ecu$e occurs a5$er $he 5unc$#on has re$urned. Th#s has an un!red#c$able ou$come. >$ #s a common !roblem *#$h b#nd#ng #n a foreach loo!(
,1a - array:#:dname# -" #9T Support#% #:loc# -" 2T??;( foreach :,1a a+ ,Ney -" ,/al; @ oc0G10ndG1yGname:,+% ,Ney% ,/al;( A

The !roblem here #s $ha$ ,/al #s local $o $he loo! :and #s reused;. The 3K. s$a$emen$ *#ll no$ e/ecu$e as e/!ec$ed. hang#ng $he b#nd call #n $he loo! $o use ,1a3,Ney4 sol'es $he !roblem( Script !-" bindloop%php
!php ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( ,+ - oc0Gpar+e:,c% #+elect H from department+ where departmentGname - :dname and locat0onG0d - :loc#;( ,1a - array:#:dname# -" #9T Support#% #:loc# -" 2T??;( foreach :,1a a+ ,Ney -" ,/al; @ oc0G10ndG1yGname:,+% ,Ney% 2ba324ey5;( A oc0Ge<ecute:,+;( wh0le ::,row - oc0GfetchGarray:,+% BC9GASSBC;;; @ foreach :,row a+ ,0tem; @ echo ,0tem.. .( A echo . 1r"En.( A !"

There #s one case *here "ou m#gh$ dec#de no$ $o use b#nd 'ar#ables. 0hen =uer#es con$a#n b#nd 'ar#ables, $he o!$#m#1er does no$ ha'e an" #n5orma$#on abou$ $he 'alue "ou ma" e'en$uall" use *hen $he s$a$emen$ #s e/ecu$ed. >5 "our da$a #s h#ghl" s+e*ed, "ou m#gh$ *an$ $o hard code 'alues. -u$ #5 $he da$a #s der#'ed 5rom user #n!u$ be sure $o san#$#1e #$. 2#nall", Oracle does no$ use =ues$#on mar+ G!G 5or b#nd 'ar#able !laceholders a$ all and O >8 su!!or$s onl" named !laceholders *#$h a colon !re5#/. 3ome PHP da$abase abs$rac$#on la"ers *#ll s#mula$e su!!or$ 5or =ues$#on mar+s b" scann#ng "our s$a$emen$s and re!lac#ng $hem *#$h su!!or$ed s"n$a/.

144

-uning S1, State7ent# in PHP 0""&ication#

Binding with L)+! and !G!,P-L)+! "lauses


3#m#lar $o $he s#m!le e/am!le abo'e, "ou can b#nd $he 'alue used #n a !a$$ern?ma$ch#ng 3K. K9CE or REWEI8GK9CE clause( Script !*" bindli5e%php
!php ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( ,+ - oc0Gpar+e:,c% .+elect c0ty% +tateGpro/0nce from locat0on+ where c0ty l0Ne :1/.;( ,c0ty - #South=#( oc0G10ndG1yGname:,+% .:1/.% ,c0ty;( oc0Ge<ecute:,+;( oc0GfetchGall:,+% ,re+;( /arGdump:,re+;( !"

Th#s uses OracleGs $rad#$#onal .>6@ s"n$a/, *here G=G means ma$ch an"$h#ng. &n underscore #n $he !a$$ern s$r#ng GGG *ould ma$ch e/ac$l" one charac$er. The ou$!u$ 5rom bindli5e%php #s c#$#es and s$a$es *here $he c#$" s$ar$s *#$h G3ou$hG(
array:7; @ 3.C9TJ.4-" array:); @ 3?4-" +tr0ng:26; .South Brun+w0cN. 324-" +tr0ng:2>; .South San *ranc0+co. 374-" +tr0ng:>; .SouthlaNe. A 3.STATEG8RBV9LCE.4-" array:); @ 3?4-" +tr0ng:2?; .Lew Yer+ey. 324-" +tr0ng:2?; .Cal0forn0a. 374-" +tr0ng:6; .Te<a+. A A

Oracle also su!!or$s regular e/!ress#on ma$ch#ng *#$h 5unc$#ons l#+e REWEI8GK9CE, REWEI8G9LSTR, REWEI8GSUBSTR, and REWEI8GRE8KACE. >n a =uer" 5rom PHP "ou m#gh$ b#nd $o REWEI8GK9CE us#ng(

145

'xecuting S1, State7ent# With O(I

Script !?" bindregexp.php


!php ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( ,+ - oc0Gpar+e:,c% .+elect c0ty from locat0on+ where rege<pGl0Ne:c0ty% :1/;.;( ,c0ty - #.H0ng.H#( oc0G10ndG1yGname:,+% .:1/.% ,c0ty;( oc0Ge<ecute:,+;( oc0GfetchGall:,+% ,re+;( /arGdump:,re+;( !"

Th#s d#s!la"s all $he c#$#es $ha$ con$a#n $he le$$ers G#ngG(
array:2; @ 3.C9TJ.4-" array:7; @ 3?4-" +tr0ng:T; .Be0Z0ng. 324-" +tr0ng:>; .S0ngapore. A A

Binding .ultiple /alues in an )0 "lause


User da$a 5or a b#nd 'ar#able #s al*a"s $rea$ed as !ure da$a and ne'er as !ar$ o5 $he 3K. s$a$emen$. -ecause o5 $h#s, $r"#ng $o use a comma se!ara$ed l#s$ o5 #$ems #n a s#ngle b#nd 'ar#able *#ll be recogn#1ed b" Oracle onl" as a s#ngle 'alue, no$ as mul$#!le 'alues. The common use case #s *hen allo*#ng a *eb user $o choose mul$#!le o!$#ons 5rom a l#s$ and *an$#ng $o do a =uer" on all 'alues. Hard cod#ng mul$#!le 'alues #n an 9L clause #n $he 3K. s$a$emen$ *or+s(
,+ - oc0Gpar+e:,c% .+elect la+tGname from employee+ where employeeG0d 0n :2?2%2?7;.;( oc0Ge<ecute:,+;( oc0GfetchGall:,+% ,re+;( foreach :,re+3#KASTGLAOE#4 a+ ,name; @ echo .Ka+t name 0+: .. ,name .. 1r"En.( A

Th#s d#s!la"s bo$h surnames bu$ #$ leads $o $he scal#ng and secur#$" #ssues $ha$ b#nd 'ar#ables o'ercome. Tr"#ng $o emula$e $he =uer" *#$h a b#nd 'ar#able does no$ *or+(
,+ - oc0Gpar+e:,c% .+elect la+tGname from employee+ where employeeG0d 0n ::e0d1/;.;( ,mye0d+ - .2?2%2?7.( oc0G10ndG1yGname:,+% .:E9DBV.% ,mye0d+;( oc0Ge<ecute:,+;( 146

-uning S1, State7ent# in PHP 0""&ication# oc0GfetchGall:,+% ,re+;(

&ll $h#s g#'es #s $he error BRA-?2T77: 0n/al0d num1er because $he ,mye0d+ s$r#ng #s $rea$ed as a s#ngle 'alue and #s no$ recogn#1ed as a l#s$ o5 numbers. The solu$#on 5or a 5#/ed, small number o5 'alues #n $he 9L clause #s $o use #nd#'#dual b#nd 'ar#ables. LUKKs can be bound 5or an" un+no*n 'alues( Script 22" bindinlist%php
!php ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( ,+ - oc0Gpar+e:,c% .+elect la+tGname from employee+ where employeeG0d 0n ::e2% :e7% :e);.;( ,mye2 - 2?)( ,mye7 - 2?5( ,mye) - LUKK( // pretend we were not g0/en th0+ /alue oc0G10ndG1yGname:,+% .:E2.% ,mye2;( oc0G10ndG1yGname:,+% .:E7.% ,mye7;( oc0G10ndG1yGname:,+% .:E).% ,mye);( oc0Ge<ecute:,+;( oc0GfetchGall:,+% ,re+;( foreach :,re+3#KASTGLAOE#4 a+ ,name; @ echo .Ka+t name 0+: .. ,name .. 1r"En.( A !"

The ou$!u$ #s(


Ka+t name 0+: Ern+t Ka+t name 0+: $unold

Tom 6"$e d#scusses $he general !roblem and g#'es solu$#ons 5or o$her cases #n $he March Q &!r#l 200C Oracle 6aga.ine.

Using Bind /ariables to (etch Data


&s *ell as *ha$ are called 9L b#nds, *h#ch !ass da$a #n$o Oracle, $here are also BUT b#nds $ha$ re$urn 'alues. These are mos$l" used $o re$urn 'alues 5rom P.I3K. !rocedures and 5unc$#ons. :3ee $he cha!$er on us#ng P.I3K.;. >5 $he PHP 'ar#able assoc#a$ed *#$h an BUT b#nd does no$ e/#s$, "ou need $o s!ec#5" $he o!$#onal leng$h !arame$er. &no$her case *hen $he leng$h should be s!ec#5#ed #s *hen re$urn#ng numbers. -" de5aul$ #n O >8, numbers are con'er$ed $o and 5rom s$r#ngs *hen $he" are bound. Th#s means $he leng$h !arame$er should also be !assed $o oc0G10ndG1yGname:; *hen re$urn#ng a number o$her*#se d#g#$s ma" be $runca$ed(
oc0G10ndG1yGname:,+% .:OB.% ,m1% 2?;(

147

'xecuting S1, State7ent# With O(I

There #s also an o!$#onal 5#5$h !arame$er, *h#ch #s $he da$a$"!e. Th#s mos$l" used 5or b#nd#ng .O-3 and resul$ se$s as sho*n #n a la$er cha!$er. One m#cro?o!$#m#1a$#on *hen numbers are +no*n $o be #n$egral, #s $o s!ec#5" $he da$a$"!e as SUKTG9LT. Th#s a'o#ds $he $"!e con'ers#on cos$(
oc0G10ndG1yGname:,+% .:OB.% ,m1% -2% SUKTG9LT;(

>n $h#s e/am!le, $he leng$h *as se$ $o [2 mean#ng use $he na$#'e da$a s#1e o5 an #n$eger.

Binding in an $ D! B1 "lause
3ome a!!l#ca$#ons allo* $he user $o choose $he !resen$a$#on order o5 resul$s. T"!#call" $he number o5 'ar#a$#ons 5or an BRDER BJ clause are small and so ha'#ng d#55eren$ s$a$emen$s e/ecu$ed 5or each cond#$#on #s e55#c#en$(
+w0tch :,/; @ ca+e 2: ,o1 - # order 1y f0r+tGname#( 1reaN( default: ,o1 - # order 1y la+tGname#( 1reaN( A ,+ - oc0Gpar+e:,c% #+elect f0r+tGname% la+tGname from employee+# . ,o1;(

-u$ #5 "our $un#ng #nd#ca$es $ha$ b#nd#ng #n a BRDER BJ clause #s necessar", and $he columns are o5 $he same $"!e, $r" us#ng a 3K. CASE s$a$emen$(
,+ - oc0Gpar+e:,c% .+elect f0r+tGname% la+tGname from employee+ order 1y ca+e :o1 when #*9RSTGLAOE# then f0r+tGname el+e la+tGname end.;( oc0G10ndG1yGname:,+% .:o1.% ,/+;( oc0Ge<ecute:,+;(

Using $W)D Bind /ariables


The !seudo?column RB&9D un#=uel" #den$#5#es a ro* *#$h#n a $able. Th#s e/am!le sho*s 5e$ch#ng a record, chang#ng $he da$a, and b#nd#ng #$s row0d #n $he &$ERE clause o5 an U8DATE s$a$emen$. Script 2!" ro&id%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( // *etch a record ,+ - oc0Gpar+e:,c% 14

-uning S1, State7ent# in PHP 0""&ication# #+elect row0d% +treetGaddre++ from locat0on+ where locat0onG0d - :lG1/ for update#;( ,loc0d - )???( // locat0on to fetch oc0G10ndG1yGname:,+% #:lG1/#% ,loc0d;( oc0Ge<ecute:,+% BC9GDE*AUKT;( ,row - oc0GfetchGarray:,+% BC9GASSBCDBC9GRETURLGLUKKS;( ,r0d - ,row3#RB&9D#4( ,addr - ,row3#STREETGADDRESS#4( // Change the addre++ to upper ca+e ,addr - +trtoupper:,addr;( // Sa/e new /alue ,+ - oc0Gpar+e:,c% #update locat0on+ +et +treetGaddre++ - :aG1/ where row0d - :rG1/#;( oc0G10ndG1yGname:,+% #:rG1/#% ,r0d% -2% BC9GBGRB&9D;( oc0G10ndG1yGname:,+% #:aG1/#% ,addr;( oc0Ge<ecute:,+;( !"

&5$er runn#ng ro&id%php, $he address has been changed 5rom


Ourten+tra++e >72

$o
OURTELSTRASSE >72

Tunin" the Pre2etch Si;e


Hou can $une PHPLs o'erall =uer" !er5ormance *#$h $he con5#gura$#on !arame$er(
oc0M.defaultGprefetch - 2??

Th#s !arame$er se$s $he number o5 ro*s re$urned #n a ba$ch *hen each 5e$ch call $o $he da$abase occurs. The de5aul$ 'alue #s 100. Pr#or $o O >8 1.P, $he de5aul$ *as 10 and $he O >8 e/$ens#on also ca!!ed $he memor" used b" $he !re5e$ch bu55er a$ 2?75 H oc0M.defaultGprefetch b"$es. >ncreas#ng $he !re5e$ch 'alue can s#gn#5#can$l" #m!ro'e !er5ormance o5 =uer#es $ha$ re$urn a large number o5 ro*s. >$ m#n#m#1es da$abase ser'er round+trips b" re$urn#ng as much da$a as !oss#ble each $#me a re=ues$ $o $he da$abase #s made. Tes$#ng *#ll sho* $he o!$#mal s#1e 5or "our =uer#es. There #s no bene5#$ us#ng $oo large a !re5e$ch 'alue. on'ersel", because Oracle d"nam#call" alloca$es s!ace, $here #s l#$$le $o be ga#ned b" reduc#ng $he 'alue $oo small. The cache #s managed b" Oracle. The da$abase re$urns $he s!ec#5#ed number o5 ro*s $o a cache #n #$s cl#en$ bu55ers. PHP 5unc$#ons l#+e oc0GfetchGarray:; re$urn one ro* $o $he user !er call regardless o5 $he !re5e$ch s#1e. 3ubse=uen$ O >8 5e$ches *#ll consume $he da$a #n $he cache un$#l e'en$uall" ano$her ba$ch o5 records #s 5e$ched 5rom $he da$abase.

14$

'xecuting S1, State7ent# With O(I

igure ?!" #he $irst reAuest to the database $etches multiple ro&s to the cache% SubseAuent $etches read $rom the cache% Hou can also change $he !re5e$ch 'alue a$ run$#me *#$h $he oc0G+etGprefetch:; 5unc$#on(
,+ - oc0Gpar+e:,c% .+elect c0ty from locat0on+.;( oc0Ge<ecute:,+;( oci_set_pre)etch$2s6 *++&' ,row - oc0GfetchGarray:,+% BC9GASSBC;(

2or oc0GfetchGall:;, *h#ch re$urns all =uer" ro*s $o $he user, PHP O >8 #n$ernall" 5e$ches all $he records #n ba$ches o5 $he s!ec#5#ed !re5e$ch s#1e. Pre5e$ch#ng #s no$ used *hen =uer#es con$a#n .O7A or .O- columns, or *hen 5e$ch#ng 5rom a R@2 UR3OR.

Tunin" the State4ent Cache Si;e


Per5ormance #s #m!ro'ed *#$h OracleGs Mcl#en$O :$ha$ #s, PHP O >8; s$a$emen$ cach#ng 5ea$ure. >n $he PHP e/$ens#on $he de5aul$ s$a$emen$ cache s#1e #s 20 s$a$emen$s. Hou can change $he s#1e *#$h $he php%ini d#rec$#'e(
oc0M.+tatementGcacheG+0Qe - 7?

The recommenda$#on #s $o use $he number o5 s$a$emen$s #n $he a!!l#ca$#onLs *or+#ng se$ o5 3K. as $he 'alue. ach#ng can be d#sabled b" se$$#ng $he s#1e $o 0. The cl#en$?s#de s$a$emen$ cache #s #n add#$#on $o $he s$andard da$abase s$a$emen$ cache. The cl#en$ cache means e'en $he $e/$ o5 $he s$a$emen$ does no$ need $o be $ransm#$$ed $o $he da$abase more $han once, reduc#ng ne$*or+ $ra55#c and da$abase ser'er load. The da$abase can d#rec$l" loo+ u! $he s$a$emen$ con$e/$ #n #$s cache *#$hou$ e'en ha'#ng $o hash $he s$a$emen$. >n $urn, $he da$abase does no$ need $o $rans5er me$a?#n5orma$#on abou$ $he s$a$emen$ bac+ $o PHP.
15%

-uning S1, State7ent# in PHP 0""&ication#

igure ?2" #he second time a statement is issued7 the statement text is not sent to the database% The cache #s !er?Oracle sess#on so $h#s 5ea$ure #s more use5ul *hen !ers#s$en$ connec$#ons are used. .#+e man" $un#ng o!$#ons, $here #s a $#meImemor" $rade?o55 *hen $*ea+#ng $h#s !arame$er. The s$a$emen$ cache also means sl#gh$l" more load #s !u$ on $he PHP hos$. To $une $he s$a$emen$ cache s#1e, mon#$or general *eb ser'er load and $he da$abase s$a$#s$#c Bbytes sent via S@3LGet to clientB. Th#s can be seen, 5or e/am!le, #n &u$oma$#c 0or+load Re!os#$or" :A/R: re!or$s. 0hen cach#ng #s e55ec$#'e, $he s$a$#s$#c should sho* an #m!ro'emen$. &d8us$ $he 'alue o5 oc0M.+tatementGcacheG+0Qe $o "our sa$#s5ac$#on. O >8 clears $he cache #5 a s$a$emen$ re$urns a da$abase error.

,'in" the Ser.er and C%ient 3uer: Re'u%t Cache'


Oracle Da$abase 11g #n$roduces Mser'er?s#deOand Mcl#en$?s#deO resul$ caches. These s$ore $he 5#nal resul$ o5 =uer#es. The da$abase cache #s enabled *#$h $he R@3U.TJ & H@JMOD@ da$abase !arame$er, *h#ch has se'eral modes. 0#$h $he de5aul$ mode, =uer#es 5or *h#ch "ou *an$ resul$s $o be cached need a h#n$ added(
,+ - oc0Gpar+e:,c% .+elect /HD re+ultGcache H/ H from employeea;(

7o PHP changes are re=u#red Q a!!l#ca$#ons *#ll #mmed#a$el" bene5#$ 5rom $he ser'er cache. The cl#en$ cache #s #deal 5or small =uer#es 5rom #n5re=uen$l" mod#5#ed $ables, such as loo+u! $ables. >$ can reduce PHP s$a$emen$ !rocess#ng $#me and s#gn#5#can$l" reduce da$abase PU usage, allo*#ng $he da$abase $o handle more PHP !rocesses and users. The cl#en$?s#de cache #s !er PHP !rocess. & +e" 5ea$ure o5 $he caches #s $ha$ Oracle au$oma$#call" handles cache en$r" #n'al#da$#on *hen a da$abase change #n'al#da$es $he s$ored resul$s. Oracle *#ll chec+ $he cl#en$ cache en$r#es each $#me an" round trip $o $he da$abase occurs. >5 no round $r#! has ha!!ened *#$h a con5#gurable MlagO $#me, $he cl#en$ cache #s assumed s$ale. The OracleH (all )nter$ace ProgrammerIs Guide7 !!g Release ! 9!!%!: con$a#ns $he bes$ descr#!$#on o5 $he 5ea$ure and has more abou$ *hen $o use #$ and ho* $o manage #$.

151

'xecuting S1, State7ent# With O(I

To demons$ra$e cl#en$ cach#ng, $he da$abase !arame$er CK9ELTGRESUKTGCAC$EGS9]E can be se$ $o a non 1ero 'alue and $he Oracle da$abase res$ar$ed(
, +Flplu+ / a+ +y+d1a SUK" alter +y+tem +et cl0entGre+ultGcacheG+0Qe-S5O +cope-+pf0le( SUK" +tartup force

>n PHP, $he +e" $o us#ng $he cl#en$?cache #s $o !ass BC9GDE*AUKT $o oc0Ge<ecute:;( Script 22" crc%php
!php ,c - oc0Gpconnect:#hr#% #hrpwd#% #localho+t/orcl#;( for :,0 - ?( ,0 2???( DD,0; @ ,+ - oc0Gpar+e:,c% .+elect /HD re+ultGcache H/ H from employee+ where rownum oc0Ge<ecute:,+% "CI_DE%#7LT;( oc0GfetchGall:,+% ,re+;( A !"

7.;(

-e5ore e/ecu$#ng crc%php, run $h#s =uer" #n $he 3K.TPlus sess#on(


SUK" +elect par+eGcall+% e<ecut0on+% +FlGte<t 7 from /,+Fl ) where +FlGte<t l0Ne #=employee+=#( 8ARSEGCAKKS EIECUT9BLS SUKGTEIT ----------- ---------- --------------------------------------------------2 2 +elect par+eGcall+% e<ecut0on+% +FlGte<t from /,+Fl where +FlGte<t l0Ne #=employee+=#

Th#s sho*s $he da$abase be#ng accessed *hen $he =uer" #s e/ecu$ed. >n#$#all" #$ sho*s 8us$ $he mon#$or#ng =uer" #$sel5. >n ano$her $erm#nal *#ndo*, run crc%php 5rom $he command l#ne or run #$ #n a bro*ser Q #$ doesnG$ d#s!la" an" resul$s.
, php crc.php

Re?runn#ng $he mon#$or#ng =uer" sho*s $ha$ dur#ng $he 1000 loo! #$era$#ons, $he da$abase e/ecu$ed $he PHP =uer" 8us$ $*#ce, once 5or $he #n#$#al e/ecu$#on and $he second $#me b" a subse=uen$ cache 'al#da$#on chec+(
8ARSEGCAKKS EIECUT9BLS SUKGTEIT ----------- ---------- --------------------------------------------------7 7 +elect /HD re+ultGcache H/ H from employee+ where rownum 7 7 7 +elect par+eGcall+% e<ecut0on+% +FlGte<t from /,+Fl where +FlGte<t l0Ne #=employee+=#

152

-uning S1, State7ent# in PHP 0""&ication#

Th#s means $ha$ DD8 o5 $he $#mes $he s$a$emen$ *as !er5ormed, $he cl#en$ cache *as used 5or $he resul$s, *#$h no da$abase access re=u#red. 7o* ed#$ crc%php and remo'e BC9GDE*AUKT 5rom $he e/ecu$e call(
oc0Ge<ecute:,+;(

Re?run $he scr#!$(


, php crc.php

The mon#$or#ng =uer" no* sho*s $he mod#5#ed =uer" *as e/ecu$ed ano$her 1000 $#mes, or once !er loo! #$era$#on. Th#s means $he cl#en$ =uer" resul$ cache *as no$ used and each #$era$#on had $o be !rocessed #n $he da$abase(
8ARSEGCAKKS EIECUT9BLS SUKGTEIT ----------- ---------- --------------------------------------------------5 2??7 +elect /HD re+ultGcache H/ H from employee+ where rownum 7 ) ) +elect par+eGcall+% e<ecut0on+% +FlGte<t from /,+Fl where +FlGte<t l0Ne #=employee+=#

& ded#ca$ed '#e* CK9ELTGRESUKTGCAC$EGSTATS, #s !er#od#call" u!da$ed *#$h s$a$#s$#cs on cl#en$ cach#ng. 2or shor$ $es$s l#+e $h#s e/am!le *here $he !rocess =u#c+l" runs and $erm#na$es, #$ ma" no$ g#'e mean#ng5ul resul$s and V,SUK can be more use5ul.

Li4itin" Ro9' and Creatin" Pa"ed Data'et'


OracleLs 3K. does no$ ha'e a K9O9T +e"*ord. >$ #s no$ #n $he 3K. s$andard and $he 'endors $ha$ use #$ ha'e d#55eren$ #m!lemen$a$#ons. There are se'eral al$erna$#'e *a"s $o l#m#$ $he number o5 ro*s re$urned #n O >8. The oc0GfetchGall:; 5unc$#on has o!$#onal argumen$s $o s!ec#5" a range o5 resul$s $o 5e$ch. Th#s #s #m!lemen$ed b" $he e/$ens#on, no$ b" OracleLs na$#'e 5unc$#onal#$". &ll ro*s !reced#ng $hose "ou *an$ s$#ll ha'e $o be 5e$ched 5rom $he da$abase.
,f0r+trow - )( ,numrow+ - 6( oc0Ge<ecute:,+;( oc0GfetchGall:,+% ,re+% ,f0r+trow% ,numrow+;( /arGdump:,re+;(

>$ #s more e55#c#en$ $o le$ Oracle do $he ro* selec$#on and onl" re$urn $he e/ac$ number o5 ro*s re=u#red. The canon#cal !ag#ng =uer" 5or Oracle8i on*ards #s g#'en on http://a+Ntom.oracle.com(
+elect H from : +elect a.H% rownum a+ rnum from :JBURGUUERJGWBESG$ERE -- 0nclud0ng the order 1y; a where rownum - OAIGRB&S ; where rnum "- O9LGRB&S

Here, O9LGRB&S #s $he ro* number o5 5#rs$ ro* and OAIGRB&S #s $he ro* number o5 $he las$ ro* $o re$urn. >n PHP "ou m#gh$ do $h#s(
153

'xecuting S1, State7ent# With O(I

Script 2'" limit%php


!php ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( ,my+tmt - .+elect c0ty from locat0on+ order 1y c0ty.( ,m0nrow - 5( // row num1er of f0r+t row to return ,ma<row - M( // row num1er of la+t row to return ,page+Fl - .+elect H from : +elect a.H% rownum a+ rnum from : ,my+tmt ; a where rownum - :ma<row; where rnum "- :m0nrow.( ,+ - oc0Gpar+e:,c% ,page+Fl;( oc0G10ndG1yGname:,+% .:ma<row.% ,ma<row;( oc0G10ndG1yGname:,+% .:m0nrow.% ,m0nrow;( oc0Ge<ecute:,+;( oc0GfetchGall:,+% ,re+;( /arGdump:,re+;( !"

7o$e $ha$ ,my+tmt #s no$ bound. -#nd da$a #s no$ $rea$ed as code, so "ou canno$ b#nd $he $e/$ o5 $he s$a$emen$ and e/!ec$ #$ $o be e/ecu$ed. -e*are o5 3K. #n8ec$#on secur#$" #ssues #5 3K. s$a$emen$s are cons$ruc$ed or conca$ena$ed. The ou$!u$ o5 $he scr#!$ #s(
array:7; @ 3.C9TJ.4-" array:6; @ 3?4-" +tr0ng:S; .Wene/a. 324-" +tr0ng:>; .$0ro+h0ma. 374-" +tr0ng:S; .Kondon. 3)4-" +tr0ng:22; .Oe<0co C0ty. 354-" +tr0ng:S; .Oun0ch. A 3.RLUO.4-" array:6; @ 3?4-" +tr0ng:2; .5. 324-" +tr0ng:2; .6. 374-" +tr0ng:2; .S. 154

,i7iting !ow# and (reating Paged )ata#et# 3)4-" +tr0ng:2; .T. 354-" +tr0ng:2; .M. A A

&n al$erna$#'e and !re5erred =uer" s"n$a/ uses OracleLs anal"$#c RB&GLUOBER:; 5unc$#on. The =uer"(
+elect la+tGname% rowGnum1er:; o/er :order 1y la+tGname; a+ myr from employee+

re$urns $*o columns #den$#5"#ng $he las$ name *#$h #$s ro* number(
KASTGLAOE OJR ------------------------- ---------A1el 2 Ande 7 AtN0n+on ) . . .

-" $urn#ng $h#s #n$o a sub=uer" and us#ng a &$ERE cond#$#on an" range o5 names can be =uer#ed. 2or e/am!le $o ge$ $he 11$h $o 20$h names $he =uer" #s(
+elect la+tGname *RBO :+elect la+tGname% rowGnum1er:; o/er :order 1y la+tGname; a+ myr from employee+; where myr 1etween 22 and 7?

>n 3K.TPlus $he ou$!u$ #s(


KASTGLAOE ------------------------B0++ot Bloom Bull Ca1r0o Cam1rault Cam1rault Chen Chung Colmenare+ Da/0e+

AutoFIncre4ent Co%u4n'
&u$o?#ncremen$ columns #n Oracle can be crea$ed us#ng a se=uence genera$or and a $r#gger. 3e=uence genera$ors are de5#ned #n $he da$abase and re$urn Oracle numbers. 3e=uence numbers are genera$ed #nde!enden$l" o5 $ables. There5ore, $he same se=uence genera$or can be used 5or more $han one $able or an"*here $ha$ "ou *an$ $o use a un#=ue number. 3e=uence genera$#on #s use5ul $o genera$e un#=ue !r#mar" +e"s 5or "our da$a and $o coord#na$e +e"s across mul$#!le $ables. Hou can ge$ a ne* 'alue 5rom a
155

'xecuting S1, State7ent# With O(I

se=uence genera$or us#ng $he LEITVAK o!era$or #n a 3K. s$a$emen$. Th#s g#'es $he ne/$ a'a#lable number and #ncremen$s $he genera$or. The s#m#lar CURRVAK o!era$or re$urns $he curren$ 'alue o5 a se=uence *#$hou$ #ncremen$#ng $he genera$or. & $r#gger #s a P.I3K. !rocedure $ha$ #s au$oma$#call" #n'o+ed a$ a !rede$erm#ned !o#n$. >n $h#s e/am!le a $r#gger #s #n'o+ed *hene'er an #nser$ #s made $o a $able. >n 3K.TPlus an au$o #ncremen$ column my0d can be crea$ed l#+e( Script 20" autoinc%sAl
create +eFuence my+eF( create ta1le myta1le :my0d num1er pr0mary Ney% mydata /archar7:7?;;( create tr0gger mytr0gger 1efore 0n+ert on myta1le for each row 1eg0n +elect my+eF.ne<t/al 0nto :new.my0d from dual( end( /

>n Oracle Da$abase11g "ou can re!lace $he SEKECT *#$h(


:new.my0d :- my+eF.ne<t/al(

>n PHP #nser$ $*o ro*s( Script 2," autoinc%php


!php ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( ,+ - oc0Gpar+e:,c% .0n+ert 0nto myta1le :mydata; /alue+ :#$ello#;.;( oc0Ge<ecute:,+;( ,+ - oc0Gpar+e:,c% .0n+ert 0nto myta1le :mydata; /alue+ :#Bye#;.;( oc0Ge<ecute:,+;( !"

Kuer"#ng $he $able #n 3K.TPlus sho*s $he OJ9D 'alues *ere au$oma$#call" #nser$ed and #ncremen$ed(
SUK" +elect H from myta1le( OJ9D ---------2 7 OJDATA -------------------$ello Bye

The #den$#5#er numbers *#ll be un#=ue and #ncreas#ng bu$ ma" no$ be consecu$#'e. 2or e/am!le #5 someone rolls bac+ an #nser$, a se=uence number can be Mlos$O.

156

Getting the ,a#t In#ert I)

!ettin" the La't In'ert ID


O >8 does no$ ha'e an e/!l#c#$ M#nser$J#dO 5unc$#on. >ns$ead, use a RETURL 9LTB clause and a b#nd 'ar#able. Us#ng $he $able and $r#gger crea$ed abo'e #n autoinc%sAl, $he #nser$ *ould be( Script 24" insertid%php
!php ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( ,+ - oc0Gpar+e:,c% .insert 0nto myta1le :mydata; /alue+ :#$ello#; return myid into 8id.;( oc0G10ndG1yGname:,+% .:0d.% ,0d% 7?% SUKTG9LT;( oc0Ge<ecute:,+;( echo .Data 0n+erted w0th 0d: ,0dEn.( !"

Th#s re$urns $he 'alue o5 $he my0d column 5or $he ne* ro* #n$o $he PHP 'ar#able ,0d. The ou$!u$, assum#ng $he $*o #nser$s o5 autoinc%php *ere !re'#ousl" e/ecu$ed, #s(
Data 0n+erted w0th 0d: )

Hou could s#m#larl" re$urn $he RB&9D o5 $he ne* ro* #n$o a descr#!$or(
,r0d - oci_new_descriptor:,c% "CI_D_!"9ID;( ,+ - oc0Gpar+e:,c% .0n+ert 0nto myta1le :mydata; /alue+ :#$ello#; return rowid into 8rid.;( oc0G10ndG1yGname:,+% .:r0d.% ,r0d% -2% "CI_B_!"9ID;( oc0Ge<ecute:,+;(

E&p%orin" Orac%e
@/!lore $he 3K. and P.I3K. languages. Ma+e ma/#mum reuse o5 5unc$#onal#$" $ha$ alread" e/#s$s. Tom 6"$eLs !o!ular s#$e, http://a+Ntom.oracle.com, has a lo$ o5 use5ul #n5orma$#on. OracleLs general gu#del#ne #s $o le$ $he da$abase manage da$a and $o $rans5er $he m#n#mum amoun$ across $he ne$*or+. &'o#d sh#!!#ng da$a 5rom $he da$abase $o PHP 5or unnecessar" !os$ !rocess#ng. Da$a #s a core asse$ o5 "our bus#ness. >$ should be $rea$ed cons#s$en$l" across "our a!!l#ca$#ons. 6ee!#ng a $h#n #n$er5ace be$*een "our a!!l#ca$#on la"er and $he da$abase #s also good !rogramm#ng !rac$#ce. There are man" more use5ul 3K. and Da$abase 5ea$ures $han $hose descr#bed #n $h#s boo+. The" are le5$ 5or "ou $o e/!lore. & cou!le are men$#oned belo*.

Ca'e In'en'iti.e 3uerie'


>5 "ou *an$ $o do =uer#es $ha$ sor$ and ma$ch da$a #n a case #nsens#$#'e manner, change $he sess#on a$$r#bu$es LKSGSBRT and LKSGCBO8 5#rs$, e#$her *#$h en'#ronmen$ 'ar#ables, or !er sess#on(
alter +e++0on +et nl+G+ort - 10naryGc0( alter +e++0on +et nl+Gcomp - l0ngu0+t0c( 157

'xecuting S1, State7ent# With O(I

Ana%:tic =unction' in S3L


OracleLs &nal"$#c 5unc$#ons are a use5ul $ool $o com!u$e aggrega$e 'alues based on a grou! o5 ro*s. Here #s an e/am!le o5 a correla$#on. CBRR:; re$urns $he coe55#c#en$ o5 correla$#on o5 a se$ o5 number !a#rs. Hou mus$ be connec$ed as $he system user #n $h#s !ar$#cular e/am!le, s#nce #$ de!ends on $able da$a no$ a'a#lable $o hr(
+elect ma<Ge<tent+% corr:ma<Gtran+% 0n0t0alGe<tent; from allGta1le+ group 1y ma<Ge<tent+(

O$her anal"$#c 5unc$#ons allo* "ou $o ge$ bas#c #n5orma$#on l#+e s$andard de'#a$#ons or do $as+s such as ran+#ng :5or To!?7 or -o$$om?7 =uer#es; or do l#near regress#ons.

15

CHAPTER 11

,SIN! PL5S3L WITH OCI0


P.I3K. #s OracleLs !rocedural language e/$ens#on $o 3K.. >$ #s a ser'er?s#de, s$ored language $ha$ #s eas"? $o?use. P.I3K. enables "ou $o m#/ 3K. s$a$emen$s *#$h !rocedural cons$ruc$s. PHP can call P.I3K. bloc+s $o ma+e use o5 ad'anced da$abase 5unc$#onal#$", and can use #$ $o e55#c#en$l" #nser$ and 5e$ch da$a. &s *#$h 3K., $he P.I3K. language g#'es a!!l#ca$#ons eas" access $o OracleLs be$$er da$e and number handl#ng, 5or e/am!le $o ma+e sure "our 5#nanc#al da$a #s no$ a55ec$ed b" PHPLs 5loa$#ng !o#n$ seman$#cs. Hou can crea$e s$ored !rocedures, 5unc$#ons and !ac+ages so "our bus#ness log#c #s reusable #n all "our a!!l#ca$#ons. P.I3K. has an #nbu#l$ na$#'e com!#ler :#n Oracle Da$abase 11g;, o!$#m#1#ng and debugg#ng 5ea$ures, and a X*ra!L code ob5usca$#on 5ac#l#$" $o !ro$ec$ $he #n$ellec$ual !ro!er$" o5 "our a!!l#ca$#ons.

PL5S3L O.er.ie9
There are a number o5 !re?su!!l#ed P.I3K. !ac+ages $o ma+e a!!l#ca$#on de'elo!men$ eas#er. Pac+ages e/#s$ 5or 5ull $e/$ #nde/#ng, =ueu#ng, change no$#5#ca$#on, send#ng ema#ls, 8ob schedul#ng and T P access, 8us$ $o name a 5e*. 0hen dec#d#ng *he$her $o *r#$e PHP on $he cl#en$ or P.I3K. #n $he ser'er, cons#der "our s+#ll le'el #n $he languages, $he cos$ o5 da$a $rans5er across $he ne$*or+ and $he re?usab#l#$" o5 $he code. >5 "ou *r#$e #n P.I3K., all "our Oracle a!!l#ca$#ons #n an" $ool or cl#en$ language can reuse $he 5unc$#onal#$". 3ome 5unc$#onal#$" should onl" e'er be #n $he da$abase, such as da$a change $r#ggers. >n Oracle, "ou can crea$e $hese $o be 5#red *hen an e'en$ such as an #nser$ or a user logon occurs. & P.I3K. bloc+ has $hree bas#c !ar$s(

& declara$#'e !ar$ :DECKARE; &n e/ecu$able !ar$ :BEW9L ... ELD; &n e/ce!$#on?handl#ng :EICE8T9BL; !ar$ $ha$ handles error cond#$#ons

2or e/am!le(
declare +alGl pl+G0nteger( 1eg0n +elect +alary 0nto +alGl from employee+ where employeeG0d - 2>2( d1m+Goutput.putGl0ne:#Salary 0+ # XX +alGl;( e<cept0on when noGdataGfound then d1m+Goutput.putGl0ne:#Lo re+ult+ returned#;( end(

Hou can run $h#s #n man" $ools, #nclud#ng PHP. >n OracleLs 3K.TPlus #$ #s run b" en$er#ng $he $e/$ a$ $he !rom!$ and 5#n#sh#ng *#$h a s#ngle / $o $ell 3K.TPlus $o e/ecu$e $he code. >5 "ou $urn on SET SERVERBUT8UT be5orehand, $hen 3K.TPlus *#ll d#s!la" $he ou$!u$ messages a5$er e/ecu$#on(
SUK" +et +er/eroutput on SUK" declare 15$

/#ing P,;S1, With O(I 7 +alGl pl+G0nteger( ) 1eg0n 5 +elect +alary 0nto +alGl 6 from employee+ S where employeeG0d - 2>2( T d1m+Goutput.putGl0ne:#Salary 0+ # XX +alGl;( M e<cept0on > when noGdataGfound then 2? d1m+Goutput.putGl0ne:#Lo re+ult+ returned#;( 22 end( 27 / Salary 0+ 76??

O$her $ools ha'e d#55eren$ *a"s o5 #nd#ca$#ng $he end o5 $he s$a$emen$s and ho* $o s*#$ch ser'er ou$!u$ on. Onl" $he e/ecu$able !ar$ o5 a P.I3K. bloc+ #s re=u#red. The declara$#'e and e/ce!$#on hander !ar$s are o!$#onal. >5 an a!!l#ca$#on !er5orms se'eral 3K. s$a$emen$s a$ one $#me, #$ can be e55#c#en$ $o bundle $hem $oge$her #n a s#ngle P.I3K. !rocedure. >ns$ead o5 e/ecu$#ng mul$#!le 3K. s$a$emen$s, PHP onl" needs $o e/ecu$e one P.I3K. call. Th#s reduces $he number o5 round $r#!s be$*een PHP and $he da$abase, and can #m!ro'e o'erall !er5ormance.

<%oc>'B Procedure'B Pac>a"e' and Tri""er'


P.I3K. code can be ca$egor#1ed as one o5 $he 5ollo*#ng(

&non"mous bloc+ 3$ored !rocedure or 5unc$#on Pac+age Tr#gger

Anon:4ou' <%oc>'
&n anon"mous bloc+ #s a P.I3K. bloc+ #ncluded #n "our a!!l#ca$#on $ha$ #s no$ named or s$ored #n $he da$abase. The !re'#ous e/am!le #s an anon"mous bloc+. -ecause $hese bloc+s are no$ s$ored #n $he da$abase, $he" are generall" 5or one?$#me use #n a 3K. scr#!$, or 5or s#m!le code d"nam#call" subm#$$ed $o $he Oracle ser'er.

Stored or Standa%one Procedure' and =unction'


& s$ored !rocedure or 5unc$#on #s a P.I3K. bloc+ $ha$ Oracle s$ores #n $he da$abase and can be called b" name 5rom an a!!l#ca$#on. 2unc$#ons re$urn a 'alue *hen e/ecu$ed. Procedures and 5unc$#ons can be used 5rom o$her !rocedures or 5unc$#ons. The" can be enabled and d#sabled $o !re'en$ $hem be#ng used. The" ma" also ha'e an #n'al#d s$a$e, #5 an"$h#ng $he" re5erence #s no$ a'a#lable. 0hen "ou crea$e a s$ored !rocedure or 5unc$#on, Oracle s$ores #$s !arsed re!resen$a$#on #n $he da$abase 5or e55#c#en$ reuse. Procedures can be crea$ed #n 3K.TPlus l#+e(
16%

<&oc3#= Procedure#= Pac3age# and -rigger# SUK" create ta1le myta1 :mydata /archar7:5?;% my0d num1er;( SUK" create or replace procedure 7 myproc:dGp 0n /archar7% 0Gp 0n num1er; a+ ) 1eg0n 5 0n+ert 0nto myta1 :mydata% my0d; /alue+ :dGp% 0Gp;( 6 end( S /

The !rocedure #s onl" crea$ed, no$ run. Programs l#+e PHP can run #$ la$er. P.I3K. 5unc$#ons are crea$ed #n a s#m#lar *a" us#ng $he CREATE BR RE8KACE *ULCT9BL command. >5 "ou ha'e crea$#on errors, use $he 3K.TPlus S$B& ERRBRS command $o d#s!la" an" messages. 2or e/am!le, crea$#ng a !rocedure $ha$ re5erences an #n'al#d $able causes an error(
SUK" 7 ) 5 6 S create or replace procedure myproc:dGp 0n /archar7% 0Gp 0n num1er; a+ 1eg0n 0n+ert 0nto yourtab :mydata% my0d; /alue+ :dGp% 0Gp;( end( /

&arn0ng: 8rocedure created w0th comp0lat0on error+. SUK" show errors Error+ for 8RBCEDURE OJ8RBC: K9LE/CBK ERRBR -------- ----------------------------------------------------------------5/) 8K/SUK: SUK Statement 0gnored 5/26 8K/SUK: BRA-??>57: ta1le or /0ew doe+ not e<0+t

>5 "ou are runn#ng a 3K. scr#!$ 5#le #n 3K.TPlus, #$ #s hel!5ul $o $urn SET EC$B BL $o see $he l#ne numbers. 3ee belo* 5or handl#ng P.I3K. errors #n PHP.

Pac>a"e'
T"!#call", s$ored !rocedures and 5unc$#ons are enca!sula$ed #n$o !ac+ages. Th#s hel!s m#n#m#1es recom!#la$#on o5 de!enden$ ob8ec$s. The !ac+age s!ec#5#ca$#on de5#nes $he s#gna$ures o5 $he 5unc$#ons and !rocedures. >5 $ha$ de5#n#$#on #s unchanged, code $ha$ #n'o+es #$ *#ll no$ need $o be recom!#led e'en #5 $he #m!lemen$a$#on #n $he !ac+age bod" changes.
SUK" create or replace pacNage mypacN a+ 7 funct0on myfunc:0Gp 0n num1er; return /archar7( ) end mypacN( 5 / SUK" create or replace pacNage 1ody mypacN a+ 7 funct0on myfunc:0Gp 0n num1er; return /archar7 a+ ) dGl /archar7:7?;( 5 1eg0n 6 +elect mydata 0nto dGl from myta1 where my0d - 0Gp( S return dGl( T end( 161

/#ing P,;S1, With O(I M end mypacN( > /

Tri""er'
& da$abase $r#gger #s a s$ored !rocedure assoc#a$ed *#$h a da$abase $able, '#e*, or e'en$. The $r#gger can be called a5$er $he e'en$, $o record #$, or $a+e some 5ollo*?u! ac$#on. & $r#gger can also be called be5ore an e'en$, $o !re'en$ erroneous o!era$#ons or 5#/ ne* da$a so $ha$ #$ con5orms $o bus#ness rules. Tr#ggers *ere sho*n earl#er as a *a" $o o!$#m#1e se$$#ng da$e 5orma$s :see Do Got Set the Date ormat >nnecessarily #n $he cha!$er on onnec$#ng; and as a *a" o5 crea$#ng au$o?#ncremen$ columns :see Auto+)ncrement (olumns #n $he !re'#ous cha!$er:.

Creatin" PL5S3L Stored Procedure' in PHP


Procedures, 5unc$#ons and $r#ggers can be crea$ed us#ng PHP. 2or e/am!le, $o crea$e $he !rocedure myproc $he code #s( Script 2-" createmyproc%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,pl+Fl - .create or replace procedure . . .myproc:dGp 0n /archar7% 0Gp 0n num1er; a+ . . .1eg0n . . .0n+ert 0nto myta1 :mydata% my0d; /alue+ :dGp% 0Gp;(. . .end(.( ,+ - oc0Gpar+e:,c% ,pl+Fl;( ,r - oc0Ge<ecute:,+;( 0f :,r; @ echo #8rocedure created#( A !"

7o$e $he las$ charac$er o5 $he P.I3K. s$a$emen$ #s a sem#?colon :a5$er P.I3K. +e"*ord end;, *h#ch #s d#55eren$ $o $he *a" 3K. s$a$emen$s are $erm#na$ed. PHP s$r#ng conca$ena$#on *as used $o bu#ld $he s$a$emen$. >5 "ou donG$ do $h#s and are on 0#ndo*s, see $he end?o5?l#ne $erm#na$or #ssue men$#oned belo*. 3#m#lar $o $he !er5ormance ad'#ce on crea$#ng $ables, a'o#d crea$#ng !ac+ages and !rocedures a$ run$#me #n an a!!l#ca$#on. Pre?crea$e $hem as !ar$ o5 a!!l#ca$#on #ns$alla$#on.

End o2 Line Ter4inator' in PL5S3L 9ith Windo9' PHP


On 0#ndo*s, mul$#?l#ne P.I3K. bloc+s *onG$ run #5 $he l#ne $erm#na$ors are #ncorrec$. The !roblem ha!!ens *hen $he end o5 l#ne charac$ers #n a mul$#?l#ne P.I3K. s$r#ng are 0#ndo*s carr#age?re$urn l#ne? 5eeds(
162

(reating P,;S1, Stored Procedure# in PHP ,pl+Fl - .create or replace procedure myproc:dGp 0n /archar7% 0Gp 0n num1er; a+ 1eg0n 0n+ert 0nto myta1 :mydata% my0d; /alue+ :dGp% 0Gp;( end(.(

>5 +howcomp0lat0onerror+:; 5unc$#on, sho*n la$er, #s used, #$s add#$#onal messages *#ll sho*(
8KS-??2?): Encountered the +ym1ol .. when e<pect0ng one of the follow0ng:

Th#s error, *#$h #$s seem#ngl" em!$" $o+en re!resen$#ng $he une/!ec$ed end?o5?l#ne s"n$a/, #s 5ollo*ed b" a l#s$ o5 +e"*ords or $o+ens $he P.I3K. !arser *as e/!ec$#ng. Use one o5 $hese solu$#ons $o 5#/ $he !roblem(

0r#$e $he P.I3K. code on a s#ngle l#ne(


,pl+Fl - .create or replace procedure myproc . . ..(

Use PHP s$r#ng conca$ena$#on *#$h a!!ro!r#a$e *h#$e s!ace !add#ng be$*een $o+ens(
,pl+Fl . . . . .create or replace procedure . .myproc:dGp 0n /archar7% 0Gp 0n num1er; a+ . .1eg0n . .0n+ert 0nto myta1 :mydata% my0d; /alue+ :dGp% 0Gp;( . .end(.(

on'er$ $he 5#le $o use U7>N?s$"le l#ne?5eeds *#$h a con'ers#on u$#l#$" or ed#$or.

Ca%%in" PL5S3L Code


Ca%%in" PL5S3L Procedure'
To #n'o+e $he !re'#ousl" crea$ed P.I3K. !rocedure, use BEW9L and ELD $o crea$e an anon"mous bloc+( Script 2*" anonplsAl%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,+ - oc0Gpar+e:,c% .be,in myproc:#Al0+on#% 56S;( end(.;( oc0Ge<ecute:,+;( !"

The bloc+ con$a#ns a s#ngle !rocedure call, bu$ "ou could #nclude an" number o5 o$her P.I3K. s$a$emen$s. Hou can also use $he 3K. CAKK s$a$emen$ l#+e( Script 2?" callplsAl%php
!php 163

/#ing P,;S1, With O(I

,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,+ - oc0Gpar+e:,c% .ca(( myproc:#Chr0+#% 27);.;( oc0Ge<ecute:,+;( !"

The call command #s ac$uall" a 3K. command and does no$ ha'e a $ra#l#ng sem#?colon.

Ca%%in" PL5S3L =unction'


all#ng a P.I3K. 5unc$#on needs a b#nd 'ar#able $o hold $he re$urn 'alue. Us#ng $he 5unc$#on myfunc:; crea$ed !re'#ousl"( Script '2" plsAl$unc%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,+ - oc0Gpar+e:,c% .1eg0n 8ret :- mypacN.myfunc:27);( end(.;( oc0G10ndG1yGname:,+% #8ret#% 2r% 7?;( oc0Ge<ecute:,+;( echo .Lame 0+: ..2r( !"

The :- $o+en #s $he ass#gnmen$ o!era$or #n P.I3K.. Here #$ ass#gns $he re$urn 'alue o5 $he 5unc$#on $o $he b#nd 'ar#able. The b#nd call s!ec#5#es $ha$ 20 b"$es should be alloca$ed $o hold $he resul$. The scr#!$ ou$!u$ #s(
Lame 0+: Chr0+

Th#s e/am!le also sho*s ho* $o call a 5unc$#on or !rocedure #ns#de a !ac+age us#ng $he s"n$a/ pacNagename.funct0onname:;.

<indin" Para4eter' to Procedure' and =unction'


P.I3K. !rocedure and 5unc$#ons argumen$s can be mar+ed 9L, BUT or 9L BUT de!end#ng on *he$her da$a #s be#ng !assed #n$o or ou$ o5 P.I3K.. 3#ngle 'alue !arame$ers can be bound #n PHP *#$h oc0G10ndG1yGname:;. >n $he myproc:; e/am!le $he !arame$ers *ere 9L !arame$ers so $he code could be(
,+ - oc0Gpar+e:,c% .call myproc::data% :0d;.;( ,data - .Chr0+.( ,0d - 27) oc0G10ndG1yGname:,+% .:data.% ,data;( oc0G10ndG1yGname:,+% .:0d.% ,0d;( oc0Ge<ecute:,+;(

164

(a&&ing P,;S1, (ode

2or BUT and 9L BUT !arame$ers, ma+e sure $he leng$h #s s!ec#5#ed #n $he b#nd call. &s men$#oned #n $he !re'#ous cha!$er, s!ec#5"#ng $he leng$h 5or 9L b#nds #s o5$en a good #dea $oo, #5 $he one s$a$emen$ #s e/ecu$ed mul$#!le $#mes #n a loo!.

Arra: <indin" and PL5S3L <u%> Proce''in"


O >8 1.2 :PHP 5.1.2; #n$roduced a ne* 5unc$#on, oc0G10ndGarrayG1yGname:;. Used *#$h a P.I3K. !rocedure, $h#s can be 'er" e55#c#en$ 5or #nser$#on or re$r#e'al, re=u#r#ng 8us$ a s#ngle oc0Ge<ecute:; $o $rans5er mul$#!le 'alues. The 5ollo*#ng e/am!le crea$es a P.I3K. !ac+age *#$h $*o !rocedures. The 5#rs$, my0n+proc:;, *#ll be !assed a PHP arra" $o #nser$. >$ uses OracleLs Mbul+O *BRAKK s$a$emen$ 5or 5as$ #nser$#on. The second !rocedure, my+elproc:;, selec$s bac+ 5rom $he $able us#ng $he BUKC CBKKECT clause and re$urns $he arra" as $he BUT !arame$er pGarr. The pGcount !arame$er #s used $o ma+e sure P.I 3K. does no$ $r" $o re$urn more 'alues $han $he PHP arra" can handle. Script '!" arrayinsert%sAl
drop ta1le myta1( create ta1le myta1:name /archar7:7?;;( create or replace pacNage mypNg a+ type arrtype 0+ ta1le of /archar7:7?; 0nde< 1y pl+G0nteger( procedure my0n+proc:pGarr 0n arrtype;( procedure my+elproc:pGarr out arrtype% pGcount 0n num1er;( end mypNg( / +how error+ create or replace pacNage 1ody mypNg a+ procedure my0n+proc:pGarr 0n arrtype; 0+ 1eg0n )ora(( 0 0n 0nd0ce+ of pGarr 0n+ert 0nto myta1 /alue+ :pGarr:0;;( end my0n+proc( procedure my+elproc:pGarr out arrtype% pGcount 0n num1er; 0+ 1eg0n +elect name bu(4 co((ect 0nto pGarr from myta1 where rownum end my+elproc( end mypNg( / +how error+

- pGcount(

To #nser$ a PHP arra" ,a #n$o mytab, use( Script '2" arrayinsert%php


!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;(

165

/#ing P,;S1, With O(I ,a - array:#a1c#% #def#% #gh0#% #ZNl#;( ,+ - oc0Gpar+e:,c% .1eg0n mypNg.my0n+proc::a;( end(.;( oci_bind_array_by_name:,+% .:a.% ,a% count:,a;% -2% SUKTGC$R;( oc0Ge<ecute:,+;( !"

The oc0G10ndGarrayG1yGname:; 5unc$#on #s s#m#lar $o oc0G10ndG1yGname:;. &s *ell as $he u!!er da$a leng$h, #$ has an e/$ra !arame$er g#'#ng $he number o5 elemen$s #n $he arra". >n $h#s e/am!le, $he number o5 elemen$s #nser$ed #s count:,a;. The da$a leng$h [2 $ells PHP $o use $he ac$ual leng$h o5 $he charac$er da$a, *h#ch #s +no*n $o PHP. To =uer" $he $able #n PHP, $he my+elproc:; !rocedure can be called. The number o5 elemen$s ,numelem+ $o be 5e$ched #s !assed #n$o my+elproc:; b" be#ng bound $o :n. Th#s l#m#$s $he =uer" $o re$urn 5our ro*s. The 'alue #s also used #n $he oc0G10ndGarrayG1yGname:; call so $he ou$!u$ arra" ,r #s correc$l" s#1ed $o hold $he 5our ro*s re$urned. The 'alue 7? #s $he *#d$h o5 $he da$abase column. &n" lo*er 'alue could resul$ #n shor$er s$r#ngs be#ng re$urned $o PHP. Script ''" array$etch%php
!php ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( ,numelem+ - 5( ,+ - oc0Gpar+e:,c% .1eg0n mypNg.my+elproc::p2% :n;( end(.;( oci_bind_array_by_name:,+% .:p2.% ,r% ,numelem+% 7?% SUKTGC$R;( oc0G10ndG1yGname:,+% .:n.% ,numelem+;( oc0Ge<ecute:,+;( /arGdump:,r;( // pr0nt the array !"

The ou$!u$ #s(


array:5; @ 3?4-" +tr0ng:); 324-" +tr0ng:); 374-" +tr0ng:); 3)4-" +tr0ng:); A

.a1c. .def. .gh0. .ZNl.

& number o5 o$her Oracle $"!es can be bound *#$h oc0GarrayG10ndG1yGname:;, 5or e/am!le SUKTG*KT 5or 5loa$#ng !o#n$ numbers. There are more e/am!les o5 oc0G10ndGarrayG1yGname:; #n $he au$oma$ed O >8 $es$s bundled *#$h $he PHP source code, see ext<oci*<tests.

166

P,;S1, Succe## With In.or7ation Warning#

PL5S3L Succe'' With In2or4ation Warnin"'


& common P.I3K. error *hen crea$#ng !ac+ages, !rocedures or $r#ggers #s(
&arn0ng: oc0Ge<ecute:;: BC9GSUCCESSG&9T$G9L*B: BRA-75)55: +ucce++ w0th comp0lat0on error

Th#s error #s mos$ l#+el" $o be seen dur#ng de'elo!men$ o5 P.I3K. *h#ch #s commonl" done #n 3K.TPlus or 3K. De'elo!er. >$ can also be seen dur#ng a!!l#ca$#on #ns$alla$#on #5 P.I3K. !ac+ages, !rocedures or 5unc$#ons ha'e an unresol'ed de!endenc". PHP code $o chec+ 5or #n5orma$#onal errors and *arn#ngs #s sho*n #n plsAlerr%php. >$ crea$es a !rocedure re5erenc#ng a non?e/#s$en$ $able and $hen =uer#es $he >SER8ERRORS $able *hen $he OR&?2EPEE error occurs( Script '0" plsAlerr%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( 0n0G+et:#d0+playGerror+#% fal+e;( // do not automat0cally +how 8$8 error+

// 8K/SUK +tatement w0th del01erate error: notGmyta1 doe+ not e<0+t ,pl+Fl - .create or replace procedure myproc:dGp 0n /archar7% 0Gp 0n num1er; a+ 1eg0n 0n+ert 0nto notGmyta1 :mydata% my0d; /alue+ :dGp% 0Gp;( end(.( ,+ - oc0Gpar+e:,c% ,pl+Fl;( ,r - Poc0Ge<ecute:,+;( 0f :',r; @ ,m - oc0Gerror:,+;( 0f :,m3#code#4 -- 75)55; @ // A 8K/SUK .+ucce++ w0th comp0lat0on error. echo .&arn0ng 0+ .% ,m3#me++age#4% .En.( +howcomp0lat0onerror+:,c;( A el+e @ // A normal SUK-+tyle error echo .Error 0+ .% ,m3#me++age#4% .En.( A A // D0+play 8K/SUK error+ funct0on +howcomp0lat0onerror+:,c; @ ,+ - oc0Gpar+e:,c% .SEKECT LAOE XX #: # XX ATTR9BUTE XX # at character # XX 8BS9T9BL XX # of l0ne # XX K9LE XX # - # XX TEIT *RBO USERGERRBRS BRDER BJ LAOE%K9LE%8BS9T9BL%ATTR9BUTE%OESSAWEGLUOBER.;( oc0Ge<ecute:,+;( 167

/#ing P,;S1, With O(I pr0nt . pre"En.( wh0le :,row - oc0GfetchGarray:,+% BC9GASSBCDBC9GRETURLGLUKKS;; @ foreach :,row a+ ,0tem; @ pr0nt :,0tem!htmlent0t0e+:,0tem;:..;( A pr0nt .En.( A pr0nt . /pre".( A !"

Th#s d#s!la"s(
&arn0ng 0+ BRA-75)55: +ucce++ w0th comp0lat0on error OJ8RBC: ERRBR at character 2) of l0ne 5 - 8K/SUK: SUK Statement 0gnored OJ8RBC: ERRBR at character 76 of l0ne 5 - 8K/SUK: BRA-??>57: ta1le or /0ew doe+ not e<0+t

.oo+#ng a$ $he P.I3K. code crea$#ng $he !rocedure, charac$er 1P on l#ne E o5 $he P.I3K. code #s $he 9LSERT s$a$emen$. harac$er 25 #s $he $able name notGmyta1. Hour ou$!u$ ma" also #nclude errors 5rom crea$#ng earl#er bloc+s. Hou can #nser$ a &$ERE clause be5ore $he BRDER BJ $o res$r#c$ $he error messages(
where name - #OJ8RBC#

,'in" RE= C,RSORS 2or Re'u%t Set'


R@2 UR3OR3 le$ "ou re$urn a se$ o5 =uer" resul$s $o PHP ? $h#n+ o5 $hem l#+e a !o#n$er $o resul$s. >n PHP "ou b#nd an O >J-J UR3OR 'ar#able $o a P.I3K. R@2 UR3OR !rocedure !arame$er and re$r#e'e $he ro*s o5 $he resul$ se$ #n a normal 5e$ch loo!. &s an e/am!le, *e crea$e a P.I3K. !ac+age *#$h a !rocedure $ha$ =uer#es $he employees $able. The !rocedure re$urns a R@2 UR3OR con$a#n#ng $he em!lo"eesL las$ names. The P.I3K. !rocedure con$a#ns $he code( Script '," re$cur!%sAl
create or replace procedure myproc:p2 out +y+Grefcur+or; a+ 1eg0n open p2 for +elect la+tGname from employee+ where rownum - 6( end( / +how error+

>n PHP $he oc0GnewGcur+or:; 5unc$#on re$urns a R@2 UR3OR resource. Th#s #s bound $o :rc #n $he call $o myproc:;. The b#nd s#1e o5 -2 means M#gnore $he s#1e !assedO. >$ #s used because $he s#1e o5 $he R@2 UR3OR #s 5#/ed b" Oracle. Once $he P.I3K. !rocedure has com!le$ed $hen $he 'alue #n ,refcur #s $rea$ed l#+e a !re!ared s$a$emen$ #den$#5#er. >$ #s s#m!l" e/ecu$ed l#+e a normal =uer" and used #n a 5e$ch loo!.

16

/#ing !': (/!SO!S .or !e#u&t Set#

Script '4" re$cur!%php


!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( // E<cute the call to the 8K/SUK +tored procedure ,+ - oc0Gpar+e:,c% .call myproc::rc;.;( 2re)cur : oci_new_cursor$2c&' oc0G10ndG1yGname:,+% #8rc#% 2re)cur6 ;06 "CI_B_C7!S"!;( oc0Ge<ecute:,+;( // E<ecute and fetch from the cur+or oci_e<ecute$2re)cur&' // treat a+ a +tatement re+ource echo . ta1le 1order-#2#"En.( wh0le:,row - oc0GfetchGarray:2re)cur% BC9GASSBC;; @ echo . tr".( foreach :,row a+ ,c; @ echo . td",c /td".( A echo . /tr"En.( A echo . /ta1le"En.( !"

The ou$!u$ #s(


A1el Ande AtN0n+on Au+t0n Baer

Th#s ne/$ e/am!le uses a user?de5#ned $"!e 5or $he R@2 UR3OR, ma+#ng $he cursor Ms$rongl" $"!edO. The $"!e #s declared #n a !ac+age s!ec#5#ca$#on. Script '-" re$cur2%sAl
create or replace pacNage empGpacN a+ type contactG0nfoGtype 0+ record : fname employee+.f0r+tGname=type% lname employee+.la+tGname=type% phone employee+.phoneGnum1er=type% ema0l employee+.ema0l=type;( type contactG0nfoGcurGtype 0+ ref cur+or return contactG0nfoGtype( procedure getGcontactG0nfo: pGempG0d 0n num1er% pGcontactG0nfo out contactG0nfoGcurGtype;(

16$

/#ing P,;S1, With O(I end empGpacN( / +how error+ create or replace pacNage 1ody empGpacN a+ procedure getGcontactG0nfo: pGempG0d 0n num1er% pGcontactG0nfo out contactG0nfoGcurGtype; a+ 1eg0n open pGcontactG0nfo for +elect f0r+tGname% la+tGname% phoneGnum1er% ema0l from employee+ where employeeG0d - pGempG0d( end( end empGpacN( / +how error+

The PHP code #s 'er" s#m#lar $o re$cur!%php, e/ce!$ #n $he call $o $he !rocedure. The !rocedure name has changed and, 5or $h#s e/am!le, an em!lo"ee #den$#5#er o5 2MM #s used.
,+ - oc0Gpar+e:,c% .call emp_pac4.,et_contact_in)o$0==6 8rc&.;( ,refcur - oc0GnewGcur+or:,c;( oc0G10ndG1yGname:,+% #:rc#% ,refcur% -2% BC9GBGCURSBR;( oc0Ge<ecute:,+;( // E<ecute and fetch from the cur+or oc0Ge<ecute:,refcur;( // treat a+ a +tatement re+ource . . .

The ou$!u$ #s $he record 5or em!lo"ee 188. The 5our 'alues ma$ch $he contactG0nfoGtype(
Celly Chung S6?.6?6.2MTS CC$ULW

C%o'in" Cur'or'
To a'o#d runn#ng ou$ o5 Oracle cursors :*h#ch ha'e a da$abase?con5#gured, !er?sess#on l#m#$ openGcur+or+ se$ b" $he D-&;, ma+e sure $o e/!l#c#$l" 5ree cursors. The e/am!le #n re$cur'%php #s a scr#!$ $ha$ #m!l#c#$l" crea$es cursors. Script '*" re$cur'%php
!php // Create a ta1le w0th 5?? row+ funct0on 0n0t0al0Qe:,c; @ ,+tmtarray - array:.drop ta1le myta1.% .create ta1le myta1:col2 /archar7:2;;.;(

17%

/#ing !': (/!SO!S .or !e#u&t Set# foreach :,+tmtarray a+ ,+tmt; @ ,+ - oc0Gpar+e:,c% ,+tmt;( Poc0Ge<ecute:,+;( A ,+ - oc0Gpar+e:,c% .0n+ert 0nto myta1 /alue+ :#A#;.;( for :,0 - ?( ,0 5??( DD,0; @ oc0Ge<ecute:,+;( A

,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( 0n0t0al0Qe:,c;( ,+ - oc0Gpar+e:,c% #+elect cur+or:+elect H from dual; from myta1#;( oc0Ge<ecute:,+;( wh0le :,refcur - oc0GfetchGarray:,+% BC9GLUO;; @ // get each RE* CURSBR oc0Ge<ecute:,refcur3?4;( // e<ecute the RE* CURSBR wh0le :,row - oc0GfetchGarray:,refcur3?4% BC9GLUO;; @ foreach :,row a+ ,0tem; echo .,0tem .( echo .En.( A oc0GfreeG+tatement:,refcur3?4;( // free the ref cur+or A !"

The =uer" #s con$r#'ed. The ou$er selec$ 5rom 6B#AC re$urns no$ ro*s, bu$ a UR3OR !er ro* o5 6B#AC. Those cursors each re!resen$ $he resul$ 5rom $he #nner =uer". Tha$ #s, $here are E00 =uer#es 5rom $he D>A3 $able. The ou$er wh0le loo! 5e$ches each o5 $he E00 R@2 UR3ORs #n $urn. The #nner wh0le loo! 5e$ches $rom each R@2 UR3OR. The resul$ #s a s$ream o5 NGs :*h#ch #s $he s#ngle ro* o5 da$a #n D>A3; be#ng d#s!la"ed(
I I I . . .

Th#s scr#!$ *or+s, bu$ #5 $he oc0GfreeG+tatement:; l#ne #s commen$ed ou$(


// oc0GfreeG+tatement:,refcur3?4;( // free the ref cur+or

$hen $he scr#!$ can reach $he da$abase l#m#$ on $he number o5 cursors. &5$er some #$era$#ons $hrough $he loo!, an error #s d#s!la"ed(
8$8 &arn0ng:oc0GfetchGarray:;: BRA-??S?5: error occurred at recur+0/e SUK le/el 2 BRA-?2???: ma<0mum open cur+or+ e<ceeded

The number o5 #$era$#ons be5ore ge$$#ng $he messages de!ends on $he da$abase con5#gura$#on !arame$er openGcur+or+.

171

/#ing P,;S1, With O(I

>n Oracle Da$abase N@ "ou can mon#$or $he number o5 cursors $ha$ each PHP connec$#on has o!en b" logg#ng #n$o Oracle &!!l#ca$#on @/!ress as $he system user and na'#ga$#ng $o -.!,n, trat,on S Mon,tor S Se ,on S O$en C&r or .

Con.ertin" 2ro4 RE= C,RSOR to PIPELINED Re'u%t'


Records re$urned b" R@2 UR3OR3 canno$ curren$l" $a+e ad'an$age o5 ro* !re?5e$ch#ng. 2or !er5ormance cr#$#cal sec$#ons o5 code, e'alua$e al$erna$#'es such as do#ng d#rec$ =uer#es, *r#$#ng a *ra!!#ng 5unc$#on #n P.I3K. $ha$ has $"!es $ha$ can be bound *#$h oc0G10ndGarrayG1yGname:;, or *r#$#ng a *ra!!#ng 5unc$#on $ha$ !#!el#nes $he ou$!u$. & !#!el#ned P.I3K. 5unc$#on g#'es $he ab#l#$" $o selec$ 5rom $he 5unc$#on as #5 #$ *ere a $able. To con'er$ $he myproc:; !rocedure 5rom re$cur!%sAl $o re$urn !#!el#ned da$a, crea$e a !ac+age( Script '?" rc2pipeline%sAl
create or replace pacNage myplmap a+ type outtype 0+ record : -- +tructure of the ref cur+or 0n myproc la+tGname /archar7:76; ;( type outtypeG+et 0+ ta1le of outtype( funct0on maprctopl return outtypeG+et p0pel0ned( end( / +how error+ create or replace pacNage 1ody myplmap a+ funct0on maprctopl return outtypeG+et p0pel0ned 0+ outrow outtypeG+et( pGrc +y+Grefcur+or( 1atch+0Qe pl+G0nteger :- 7?( -- fetch 1atche+ of 7? row+ at a t0me 1eg0n myproc:pGrc;( -- call the or0g0nal procedure loop fetch pGrc 1ulN collect 0nto outrow l0m0t 1atch+0Qe( for 0 0n 2 .. outrow.count:; loop p0pe row :outrow:0;;( end loop( e<0t when outrow.count 1atch+0Qe( end loop( end maprctopl( end myplmap( / +how error+

Th#s calls myproc:; and !#!es each record. >$ can be called #n PHP us#ng a s#m!le =uer" *#$h $he ta1le o!era$or( Script 02" rc2pipeline%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( 172

/#ing !': (/!SO!S .or !e#u&t Set#

,+ - oc0Gpar+e:,c% .+elect H from tab(e:myplmap.maprctopl:;;.;( oc0Ge<ecute:,+;( oc0GfetchGall:,+% ,re+;( /arGdump:,re+;( !"

>5 $he R@2 UR3OR =uer" #n myproc:; selec$ed all columns, $hen outtypeG+et could s#m!l" be ha'e been declared as(
type outtypeG+et 0+ ta1le of employee+=rowtype(

Orac%e Co%%ection' in PHP


Man" !rogramm#ng $echn#=ues use collec$#on $"!es such as arra"s, bags, l#s$s, nes$ed $ables, se$s, and $rees. To su!!or$ $hese $echn#=ues #n da$abase a!!l#ca$#ons, P.I3K. !ro'#des $he da$a$"!es T&-.@ and ,&RR&H, *h#ch allo* "ou $o declare #nde/?b" $ables, nes$ed $ables, and 'ar#able?s#1e arra"s. & collec$#on #s an ordered grou! o5 elemen$s, all o5 $he same $"!e. @ach elemen$ has a un#=ue subscr#!$ $ha$ de$erm#nes #$s !os#$#on #n $he collec$#on. ollec$#ons *or+ l#+e $he arra"s 5ound #n mos$ $h#rd? genera$#on !rogramm#ng languages. &lso, collec$#ons can be !assed as !arame$ers. 3o, "ou can use $hem $o mo'e columns o5 da$a #n$o and ou$ o5 da$abase $ables or be$*een cl#en$?s#de a!!l#ca$#ons and s$ored sub!rograms. Oracle collec$#ons are man#!ula$ed #n PHP b" me$hods on a collec$#on resource, *h#ch #s alloca$ed *#$h oc0GnewGcollect0on:;. >n a s#m!le ema#l address boo+ demons$ra$#on, $*o ,&RR&Hs are crea$ed, one 5or an arra" o5 !eo!leLs names, and one 5or an arra" o5 ema#l addresses. ,&RR&Hs :shor$ 5or 'ar#able?s#1e arra"s; use se=uen$#al numbers as subscr#!$s $o access a 5#/ed number o5 elemen$s. Script 0!" addressboo5%sAl
drop ta1le ema0l+( create ta1le ema0l+ : u+erG0d /archar7:2?;% fr0endGname /archar7:7?;% ema0lGaddre++ /archar7:7?;;( create or replace type ema0lGarray a+ /array:2??; of /archar7:7?;( / +how error+ create or replace type fr0endGarray a+ /array:2??; of /archar7:7?;( / +how error+ create or replace procedure updateGaddre++G1ooN: pGu+erG0d 0n /archar7% pGfr0endGname fr0endGarray% pGema0lGaddre++e+ ema0lGarray; 173

/#ing P,;S1, With O(I 0+ 1eg0n delete from ema0l+ where u+erG0d - pGu+erG0d( forall 0 0n 0nd0ce+ of pGema0lGaddre++e+ 0n+ert 0nto ema0l+ :u+erG0d% fr0endGname% ema0lGaddre++; /alue+ :pGu+erG0d% pGfr0endGname:0;% pGema0lGaddre++e+:0;;( end updateGaddre++G1ooN( / +how error+

The updateGaddre++G1ooN:; !rocedure loo!s o'er all elemen$s o5 $he address collec$#on and #nser$s each one and #$s ma$ch#ng name. The updateaddresses%php code crea$es a collec$#on o5 names and a collec$#on o5 ema#l addresses us#ng $he append:; me$hod $o add elemen$s $o each arra". These collec$#ons are bound as BC9GBGLTJ :Mnamed $"!eO; $o $he argumen$s o5 $he P.I3K. addre++G1ooN:; call. The s#1e -2 #s used because Oracle #n$ernall" +no*s $he s#1e o5 $he $"!e. 0hen addre++G1ooN:; #s e/ecu$ed, $he names and ema#l addresses are #nser$ed #n$o $he da$abase. Script 02" updateaddresses%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,u+erGname - #cZone+#( ,fr0end+Gname+ - array:#al0+on#% #a+lam#;( ,fr0end+Gema0l+ - array:#al0+onPe<ample.com#% #a+lamPe<ample.com#;( ,fr0endGcoll - oci_new_co((ection:,c% #*R9ELDGARRAJ#;( ,ema0lGcoll - oci_new_co((ection:,c% #EOA9KGARRAJ#;( for :,0 - ?( ,0 count:,fr0end+Gname+;( DD,0; @ 2)riend_co((;>append:,fr0end+Gname+3,04;( 2emai(_co((;>append$,fr0end+Gema0l+3,04;( A ,+ - oc0Gpar+e:,c% .1eg0n updateGaddre++G1ooN::un% :fr0end+% :ema0l+;( end(.;( oc0G10ndG1yGname:,+% #:un#% ,u+erGname;( oc0G10ndG1yGname:,+% #:fr0end+#% ,fr0endGcoll% -2% BC9GBGLTJ;( oc0G10ndG1yGname:,+% #:ema0l+#% ,ema0lGcoll% -2% BC9GBGLTJ;( oc0Ge<ecute:,+;( !"

The ema#ls $able no* has $he #nser$ed da$a(


SUK" +elect H from ema0l+( USERG9D *R9ELDGLAOE EOA9KGADDRESS ---------- -------------------- -------------------cZone+ al0+on al0+onPe<ample.com 174

Orac&e (o&&ection# in PHP cZone+ a+lam a+lamPe<ample.com

O$her O >8 collec$#on me$hods allo* access#ng or co!"#ng da$a #n a collec$#on. 3ee $he PHP manual 5or more #n5orma$#on.

,'in" PL5S3L and S3L O)6ect T:pe' in PHP


3ome$#me "ou ha'e $o *or+ *#$h Oracle ob8ec$ $"!es or call P.I3K. !rocedures $ha$ are des#gned 5or #n$erac$#ng *#$h o$her P.I3K. code. Pre'#ous sec$#ons ha'e #n$roduced some me$hods. Th#s sec$#on g#'es more e/am!les. >$ #s a br#e5 gu#de, and no$ e/haus$#'e. &lso, some $echn#=ues *#ll *or+ be$$er #n some s#$ua$#ons $han #n o$hers. The 5#rs$ e/am!le s#mula$es $he Oracle Te/$ CTIGT$ES !ac+age !rocedures. These re$urn Oracle ob8ec$ $"!es. :Oracle Te/$ #s a da$abase com!onen$ $ha$ uses s$andard 3K. $o #nde/, search, and anal"1e $e/$ and documen$s s$ored #n $he da$abase, #n 5#les, and on $he *eb. >$ can !er5orm l#ngu#s$#c anal"s#s on documen$s, as *ell as search $e/$ us#ng a 'ar#e$" o5 s$ra$eg#es #nclud#ng +e"*ord search#ng, con$e/$ =uer#es;. Th#s e/am!le, ctx%sAl7 se$s u! an e/am!le !ac+age *#$h a s#m#lar #n$er5ace $o CTIGT$ES. Here #$ 8us$ re$urns random da$a #n $he !arame$er( Script 0'" ctx%sAl
-- 8acNage .Suppl0ed8Ng. +0mulate+ Bracle Te<t#+ CTIGT$ES. -- 9t ha+ a procedure that return+ a 8K/SUK type. create or replace pacNage Suppl0ed8Ng a+ type Suppl0edRec 0+ record : 0d num1er% data /archar7:2??; ;( type Suppl0edTa1Type 0+ ta1le of Suppl0edRec 0nde< 1y 10naryG0nteger( procedure Suppl0ed8roc:pGp 0n out nocopy Suppl0edTa1Type;( end Suppl0ed8Ng( / +how error+ create or replace pacNage 1ody Suppl0ed8Ng a+ procedure Suppl0ed8roc:pGp 0n out nocopy Suppl0edTa1Type; 0+ 1eg0n -- Create +ome random data pGp.delete( for 0 0n 2..6 loop pGp:0;.0d :- 0( pGp:0;.data :- #Random: # XX 0 XX :2DABS:OBD:d1m+Grandom.random%2?????;;;( end loop( end Suppl0ed8roc( end Suppl0ed8Ng( / +how error+

Run $he 5#le ctx%sAl #n 3K.TPlus(


, +Flplu+ hr/hrpwdPlocalho+t/IE Pct<.+Fl

175

/#ing P,;S1, With O(I

Th#s #s $he M5#/edO !ar$ o5 $he !roblem, re!resen$#ng $he !re?su!!l#ed 5unc$#onal#$" "ou need $o *or+ *#$h.

,'in" OCI0 Co%%ection =unction'


To call Suppl0ed8roc:; us#ng collec$#on 5unc$#ons, crea$e a *ra!!er 5unc$#on #n P.I3K. $o con'er$ $he P.I3K. $"!e 3u!!l#edTabT"!e $o a !a#r o5 3K. $"!es. Use 3K.TPlus $o run myproc%sAl( Script 00" myproc%sAl
-- Create a wrapper procedure that call+ the pre-+uppl0ed -- Suppl0ed8roc:; and con/ert+ 0t+ output to SUK type+. create or replace type Oy9dRec a+ ta1le of num1er( / +how error+ create or replace type OyDataRec a+ ta1le of /archar7:2??;( / +how error+ create or replace procedure Oy8roc :pG0d 0n out Oy9dRec% pGdata 0n out OyDataRec; a+ lGre+ult+ Suppl0ed8Ng.Suppl0edTa1Type( 1eg0n -- get re+ult+ from e<0+t0ng procedure Suppl0ed8Ng.Suppl0ed8roc:lGre+ult+;( -- copy to a type we can pa++ 1acN to 8$8 pG0d.delete( pGdata.delete( for 0 0n 2..lGre+ult+.count loop pG0d.e<tend( pG0d:0; :- lGre+ult+:0;.0d( pGdata.e<tend( pGdata:0; :- lGre+ult+:0;.data( end loop( end Oy8roc( / +how error+

7o* "ou can call Oy8roc:;( Script 0," ctx%php


!php ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( ,+ - oc0Gpar+e:,c% #1eg0n Oy8roc::re+G0d% :re+Gdata;( end(#;( ,re+G0d - oc0GnewGcollect0on:,c% #OJ9DREC#;( 176

/#ing P,;S1, and S1, O*>ect -y"e# in PHP ,re+Gdata - oc0GnewGcollect0on:,c% #OJDATAREC#;( oc0G10ndG1yGname:,+% #:re+G0d#% ,re+G0d% -2% BC9GBGLTJ;( oc0G10ndG1yGname:,+% #:re+Gdata#% ,re+Gdata% -2% BC9GBGLTJ;( oc0Ge<ecute:,+;( for :,0 - ?( ,0 ,re+G0d-"+0Qe:;( ,0DD; @ ,0d - ,re+G0d-"getElem:,0;( ,data - ,re+Gdata-"getElem:,0;( echo .9d: ,0d% Data: ,data 1r"En.( A !"

Th#s alloca$es $*o collec$#ons and b#nds $hem as $he !arame$ers $o Oy8roc:;. &5$er Oy8roc:; has been called, $he collec$#on me$hod getElem:; #s used $o access each 'alue re$urned. The ou$!u$ #s s#m#lar $o(
9d: 9d: 9d: 9d: 9d: 2% 7% )% 5% 6% Data: Data: Data: Data: Data: Random: Random: Random: Random: Random: 266>57 75TTM) )S666) 5MT66) 6M>MT>

,'in" a RE= C,RSOR


>5 $he da$a can be con'er$ed $o a R@2 UR3OR, a s$ra#gh$5or*ard =uer" can be used $o 5e$ch resul$s. Th#s e/am!le sho*s an ob8ec$ $"!e. The !rocedure mycreatedata:; crea$es $he or#g#nal da$a. 0hen #$ #s called, #$ re$urns an ob8ec$ $able con$a#n#ng 5#'e ro*s. Script 04" ob;ect%sAl
drop type myta1letype( drop type mytype( create or replace type mytype a+ o1Zect :my0d num1er% mydata /archar7:7?;;( / +how error+ create or replace type myta1letype a+ ta1le of mytype( / +how error+ create or replace procedure mycreatedata:outdata out myta1letype; a+ 1eg0n outdata :- myta1letype:;( for 0 0n 2..6 loop outdata.e<tend( outdata:0; :- mytype:0 H 7% #+ome name # XX 0;( end loop( end( / +how error+

177

/#ing P,;S1, With O(I

Da$a o5 $"!e myta1letype can be con'er$ed $o a R@2 UR3OR us#ng a *ra!!er 5unc$#on( Script 0-" ob;ectrc%sAl
create or replace procedure mywrapper2:rc out +y+Grefcur+or; a+ or0gdata myta1letype( 1eg0n mycreatedata:or0gdata;( -- create +ome /alue+ open rc for +elect H from ta1le:ca+t:or0gdata a+ myta1letype;;( end mywrapper2( / +how error+

Th#s can be called #n PHP l#+e( Script 0*" ob;ectrc%php


!php ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( ,+ - oc0Gpar+e:,c% .1eg0n mywrapper2::myrc;( end(.;( ,rc - oc0GnewGcur+or:,c;( oc0G10ndG1yGname:,+% #:myrc#% ,rc% -2% BC9GBGCURSBR;( oc0Ge<ecute:,+;( oc0Ge<ecute:,rc;( oc0GfetchGall:,rc% ,re+;( /arGdump:,re+;( !"

The ou$!u$ #s(


array:7; @ 3.OJ9D.4-" array:6; @ 3?4-" +tr0ng:2; .7. 324-" +tr0ng:2; .5. 374-" +tr0ng:2; .S. 3)4-" +tr0ng:2; .M. 354-" +tr0ng:7; .2?. A 3.OJDATA.4-" array:6; @ 3?4-" +tr0ng:22; .+ome name 2. 324-" +tr0ng:22; .+ome name 7. 374-" 17

/#ing P,;S1, and S1, O*>ect -y"e# in PHP +tr0ng:22; .+ome name ). 3)4-" +tr0ng:22; .+ome name 5. 354-" +tr0ng:22; .+ome name 6.

A A

OracleGs lac+ o5 !re?5e$ch#ng 5or R@2 UR3OR3 needs $o be cons#dered #5 !er5ormance #s #m!or$an$. Us#ng a P>P@.>7@D 5unc$#on or arra" b#nd can be much 5as$er.

<indin" an Arra:
&n al$erna$#'e solu$#on le$s "ou use $he 5as$ oc0G10ndGarrayG1yGname:; 5unc$#on. The *ra!!er !rocedure loo+s l#+e( Script 0?" ob;ectba%sAl
create or replace procedure mywrapper7: pempno out d1m+G+Fl.num1erGta1le% pename out d1m+G+Fl./archar7Gta1le; a+ or0gdata myta1letype( 1eg0n mycreatedata:or0gdata;( -- create +ome /alue+ +elect my0d% mydata 1ulN collect 0nto pempno% pename from ta1le:ca+t:or0gdata a+ myta1letype;;( end mywrapper7( / +how error+

Th#s can be called 5rom PHP *#$h( Script ,2" obe;ctba%php


!php ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( ,+ - oc0Gpar+e:,c% .1eg0n mywrapper7::my0d% :mydata;( end(.;( oc0G10ndGarrayG1yGname:,+% .:my0d.% ,my0d% 2?% -2% SUKTG9LT;( oc0G10ndGarrayG1yGname:,+% .:mydata.% ,mydata% 2?% 7?% SUKTGC$R;( oc0Ge<ecute:,+;( /arGdump:,my0d;( /arGdump:,mydata;( !"

Th#s $echn#=ue can be used *hen $he number o5 #$ems $o re$urn #s +no*n. The ou$!u$ #s(
array:6; @ 3?4-" 0nt:7; 324-" 17$

/#ing P,;S1, With O(I 0nt:5; 374-" 0nt:S; 3)4-" 0nt:M; 354-" 0nt:2?;

A array:6; @ 3?4-" +tr0ng:22; 324-" +tr0ng:22; 374-" +tr0ng:22; 3)4-" +tr0ng:22; 354-" +tr0ng:22; A

.+ome name 2. .+ome name 7. .+ome name ). .+ome name 5. .+ome name 6.

,'in" a PIPELINED =unction


The *ra!!er 5unc$#on $o con'er$ $o a P>P@.>7@D 5unc$#on #s( Script ,!" ob;ectpl%sAl
create or replace pacNage myplpNg a+ type plrow 0+ ta1le of mytype( funct0on mywrapper) return plrow p0pel0ned( end( / +how error+ create or replace pacNage 1ody myplpNg a+ funct0on mywrapper) return plrow p0pel0ned 0+ or0gdata myta1letype( 1eg0n mycreatedata:or0gdata;( -- create +ome /alue+ for 0 0n 2 .. or0gdata.count:; loop p0pe row :or0gdata:0;;( end loop( end mywrapper)( end myplpNg( / +how error+

Th#s can be called 5rom PHP *#$h( Script ,2" ob;ectpl%php


!php

1 %

/#ing P,;S1, and S1, O*>ect -y"e# in PHP ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( ,+ - oc0Gpar+e:,c% .+elect H from ta1le:myplpNg.mywrapper):;;.;( oc0Ge<ecute:,+;( oc0GfetchGall:,+% ,re+;( /arGdump:,re+;( !"

The ou$!u$ #s $he same as 5rom ob;ectrc%php%

!ettin" Output 9ith D<8SAO,TP,T


The DBOSGBUT8UT !ac+age #s $he s$andard *a" $o M!r#n$O ou$!u$ 5rom P.I3K.. The dra*bac+ #s $ha$ #$ #s no$ as"nchronous. The P.I3K. !rocedure or bloc+ $ha$ calls DBOSGBUT8UT runs $o com!le$#on be5ore an" ou$!u$ #s re$urned $o $he user. DBOSGBUT8UT #s l#+e a bu55er. Hour code $urns on bu55er#ng, calls some$h#ng $ha$ !u$s ou$!u$ #n $he bu55er, and $hen la$er 5e$ches 5rom $he bu55er. O$her da$abase connec$#ons canno$ access "our bu55er. & bas#c *a" $o 5e$ch DBOSGBUT8UT #n PHP #s $o b#nd an ou$!u$ s$r#ng $o $he P.I3K. d1m+Goutput.getGl0ne:; !rocedure(
,+ - oc0Gpar+e:,c% .1eg0n d1m+Goutput.getGl0ne::ln% :+t;( end(.;( oc0G10ndG1yGname:,+% .:ln.% ,ln% 766;( // output l0ne oc0G10ndG1yGname:,+% .:+t.% ,+t% -2% SUKTG9LT;( // +tatu+: 2 mean+ no more l0ne+ wh0le ::,+ucc - oc0Ge<ecute:,+;; `` ',+t; @ echo .,lnEn.( A

The 'ar#able :ln #s arb#$rar#l" bound as leng$h 255. Th#s *as $he DBOSGBUT8UT l#ne s#1e l#m#$ !r#or $o Oracle Da$abase 10g Release 10.2, *hen #$ *as $hen changed $o P26b. :The l#m#$a$#on on $he number o5 l#nes *as also ra#sed;. &'o#d b#nd#ng P26b, es!ec#all" #5 $he da$abase #s runn#ng #n OracleLs shared ser'er mode. >5 "ou b#nd $h#s s#1e, $hen #$ #s eas" $o slo* !er5ormance or ge$ memor" errors. Ho*e'er, #5 "ou b#nd less $han $he 5ull s#1e, ma+e sure "our a!!l#ca$#on does no$ !r#n$ *#der l#nes. &l$erna$#'el", "ou can use oc0G10ndGarrayG1yGname:; and call ano$her DBOSGBUT8UT !ac+age $ha$ re$urns mul$#!le l#nes, getGl0ne+:;. The !er5ormance o5 $h#s #s generall" be$$er bu$ #$ does de!end on ho* b#g $he b#nd arra" #s, ho* much da$a #s re$urned and ho* $he code #s s$ruc$ured. >n $he *ors$ case #$ m#gh$ be slo*er $han $he s#m!le code, so benchmar+ "our a!!l#ca$#on care5ull". & more cons#s$en$ and 5as$ DBOSGBUT8UT 5e$ch#ng #m!lemen$a$#on uses a cus$om !#!el#ned P.I3K. 5unc$#on. >n 3K.TPlus crea$e $he 5unc$#on( Script ,'" dbmsoutput%sAl
create or replace type dorow a+ ta1le of /archar7:5???;( / +how error+ create or replace funct0on mydofetch return dorow pipe(ined 0+ l0ne /archar7:5???;( +tatu+ 0nteger( 1eg0n 1 1

/#ing P,;S1, With O(I loop d1m+Goutput.getGl0ne:l0ne% +tatu+;( e<0t when +tatu+ - 2( pipe row $(ine&( end loop( return( end( / +how error+

-ecause *e *#ll 5e$ch $he da$a #n a =uer" as a 3K. s$r#ng, $he ma/#mum leng$h #s E000 b"$es. & 5unc$#on $o $urn on ou$!u$ bu55er#ng #s sho*n #n dbmsoutput%inc along *#$h getd1m+output:; *h#ch re$urns an arra" o5 $he ou$!u$ l#nes( Script ,0" dbmsoutput%inc
!php // Turn DBOSGBUT8UT on funct0on ena1led1m+output:,c; @ ,+ - oc0Gpar+e:,c% .1eg0n d1m+Goutput.ena1le:null;( end(.;( ,r - oc0Ge<ecute:,+;( return ,r( A // Return+ an array of DBOSGBUT8UT l0ne+ funct0on getd1m+output:,c; @ ,re+ - fal+e( ,+ - oc0Gpar+e:,c% .+elect H from ta1le:mydofetch:;;.;( oc0Ge<ecute:,+;( oc0GfetchGall:,+% ,re+;( return ,re+3#CBKUOLGVAKUE#4( A !"

The ne/$ scr#!$ uses $hese 5unc$#ons $o sho* M!r#n$#ngO ou$!u$ 5rom P.I3K. bloc+s( Script ,," dbmsoutput%php
!php 0nclude:.d1m+output.0nc.;( ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( // Turn output 1uffer0ng on ena1led1m+output:,c;( // Create +ome output ,+ - oc0Gpar+e:,c% .call d1m+Goutput.putGl0ne:#$ello% world'#;.;( oc0Ge<ecute:,+;( 1 2

Getting Out"ut with )<9S4O/-P/-

// Create more output // Any 8K/SUK code 1e0ng run can 0n+ert 0nto the output 1uffer ,+ - oc0Gpar+e:,c% .1eg0n d1m+Goutput.putGl0ne:#$ello aga0n#;( d1m+Goutput.putGl0ne:#$ello f0nally#;( end(.;( oc0Ge<ecute:,+;( // D0+play the output ,output - getd1m+output:,c;( 0f :,output; @ foreach :,output a+ ,l0ne; @ echo .,l0ne 1r"En.( A A !"

The ou$!u$ #s all $he d1m+Goutput.putGl0ne:; $e/$(


$ello% world' $ello aga0n $ello f0nally

>5 "ou e/!ec$ large amoun$s o5 ou$!u$, "ou ma" *an$ $o s$ream resul$s as $he" are 5e$ched 5rom $he da$abase #ns$ead o5 re$urn#ng $hem #n one arra" 5rom getd1m+output:;. >5 DBOSGBUT8UT does no$ su#$ "our a!!l#ca$#on, "ou can also ge$ ou$!u$ 5rom P.I3K. b" logg#ng #$ $o da$abase $ables or b" us#ng !ac+ages l#+e UTKG*9KE and DBOSG898E $o as"nchronousl" d#s!la" ou$!u$ $o a se!ara$e $erm#nal *#ndo*.

PL5S3L =unction Re'u%t Cache


Oracle Da$abase 11g #n$roduced a cache 5or P.I3K. 5unc$#on resul$s, #deal 5or re!ea$ed loo+u! o!era$#ons. The cache con$a#ns $he genera$ed resul$ o5 a !re'#ous 5unc$#on call. >5 $he 5unc$#on #s re?run *#$h $he same !arame$er 'alues, $he resul$ 5rom $he cache #s re$urned #mmed#a$el" *#$hou$ need#ng $o re?e/ecu$e $he code. The cached resul$s are a'a#lable $o an" user. The cache *#ll age ou$ resul$s #5 more memor" #s re=u#red. There are some res$r#c$#ons #nclud#ng $ha$ onl" bas#c $"!es can be used 5or !arame$ers, and $he" mus$ be >7 onl". Re$urn $"!es are s#m#lar res$r#c$ed, #n !ar$#cular no$ us#ng R@2 UR3OR or P>P@.>7@D resul$s. To use $he cache, a normal 5unc$#on #s crea$ed *#$h $he re+ultGcache o!$#on( 3cr#!$ 5F( 5rc.s=l
create or replace funct0on mycachefunc:pG0d 0n /archar7; return /archar7 resu(t_cache re(ies_on$mytab& a+ lGdata /archar7:5?;( 1eg0n +elect mydata 0nto lGdata from myta1 where my0d - pG0d( return lGdata( 1 3

/#ing P,;S1, With O(I end( / +how error+

The rel0e+Gon:; clause #s a comma se!ara$ed l#s$ o5 $ables. >5 an" o5 $hese $ables change, $han $he cache #s au$oma$#call" #n'al#da$ed b" Oracle. The ne/$ $#me $he 5unc$#on #s called, #$ *#ll e/ecu$e com!le$el" and u!da$e $he cache a!!ro!r#a$el". 3ee $he Oracle Database P3<S@3 3anguage Re$erence !!g Release ! 9!!%!: manual 5or more de$a#ls abou$ $he 5ea$ure.

,'in" Orac%e Locator 2or Spatia% 8appin"


Oracle .oca$or #s a subse$ o5 Oracle 3!a$#al, a com!rehens#'e ma!!#ng l#brar". Oracle .oca$or #s !o*er5ul #$sel5 and #s a'a#lable #n all Oracle Da$abase ed#$#ons. & grea$ #n$roduc$#on $o Oracle .oca$or #s #n $he Oracle Da$abase @/!ress @d#$#on 2 Day Plus 3ocator Developer Guide. Th#s sec$#on sho*s some $echn#=ues $o use .oca$or da$a #n PHP. Oracle .oca$or ma+es use o5 P.I3K. $"!es such as collec$#ons. These can no$ al*a"s be d#rec$l" 5e$ched #n$o PHP. The e/am!les use $he $ables sho*n #n $he Oracle Da$abase @/!ress @d#$#on 2 Day Plus 3ocator Developer Guide sam!le scenar#o. rea$e $hese $ables #n 3K.TPlus be5ore con$#nu#ng( http://download.oracle.com/doc+/cd/B76)7>G?2/doc/appde/.2?7/17M??5/<eGlocator.ht mRC9$EA9WY

In'ertin" Locator Data


>nser$#ng .oca$or da$a #s s#m!l" a ma$$er o5 e/ecu$#ng $he a!!ro!r#a$e 9LSERT s$a$emen$(
,+Fl - .0n+ert 0nto cu+tomer+ /alue+ :2??% #A#% #B#% #222 Ree+e A/e#% #Ch0cago#% #9K#% 27)56% SDBGWEBOETRJ:7??2% M)?T% SDBG8B9LTGTJ8E:-S>.7)2556%27.??2765%LUKK;% LUKK% LUKK;;.( ,+ - oc0Gpar+e:,c% ,+Fl;( oc0Ge<ecute:,+;(

3uerie' Returnin" Sca%ar @a%ue'


-e5ore 5e$ch#ng da$a, de$erm#ne #5 $h#s #s, #n 5ac$, necessar". O5$en $he da$a can be !rocessed #n Oracle 3K. or P.I3K. e55#c#en$l" and eas#l". Kuer#es re$urn#ng scalar 'alues 5rom .oca$or ob8ec$s are no d#55eren$ $o o$her PHP =uer#es. Th#s e/am!le 5#nds $he $hree closes$ cus$omers $o $he s$ore *#$h CUSTBOERG9D o5 2?2. The =uer" uses $he #n? bu#l$ 3!a$#al 5unc$#on SBDGLL:; $o de$erm#ne $he nearest neighbor rela$#onsh#!. Script ,-" loc!%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( 1 4

/#ing Orac&e ,ocator .or S"atia& 9a""ing

,+Fl - .+elect /HD ordered H/ c.cu+tomerG0d% c.f0r+tGname% c.la+tGname from +tore+ +% cu+tomer+ c where +.+toreG0d - :+0d and +doGnn:c.cu+tGgeoGlocat0on% +.+toreGgeoGlocat0on% :nre+; - #TRUE#.( ,+ - oc0Gpar+e:,c% ,+Fl;( ,+0d - 2?2( ,nre+ - #+doGnumGre+-)#( // return ) re+ult+

oc0G10ndG1yGname:,+% .:+0d.% ,+0d;( oc0G10ndG1yGname:,+% .:nre+.% ,nre+;( oc0Ge<ecute:,+;( oc0GfetchGall:,+% ,re+;( /arGdump:,re+;( !"

The ou$!u$ #s(


array:); @ 3.CUSTBOERG9D.4-" array:); @ 3?4-" +tr0ng:5; .2??2. 324-" +tr0ng:5; .2??). 374-" +tr0ng:5; .2??5. A 3.*9RSTGLAOE.4-" array:); @ 3?4-" +tr0ng:>; .Ale<andra. 324-" +tr0ng:S; .Oar0an. 374-" +tr0ng:S; .Thoma+. A 3.KASTGLAOE.4-" array:); @ 3?4-" +tr0ng:T; .L0chol+. 324-" +tr0ng:6; .Chang. 374-" +tr0ng:M; .&0ll0am+. A A

1 5

/#ing P,;S1, With O(I

The CUSTBOERG9D, *9RSTGLAOE and KASTGLAOE columns are scalar LUOBER and VARC$AR7 columns re$urned d#rec$l" #n$o a PHP arra".

Se%ectin" @ertice' ,'in" SDOA,TIL.!ET@ERTICES


2or some .oca$or $"!es, #n?bu#l$ 5unc$#ons *#ll con'er$ ob8ec$s $o scalar 'alues $ha$ can be re$urned $o PHP. 2or e/am!le, $o 5e$ch $he coord#na$es 5rom a geome$r" 5or cus$omer 1001, use $he #nbu#l$ SDBGUT9K.WETVERT9CES:; 5unc$#on( Script ,*" loc2%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,+Fl - .+elect t.<% t.y from cu+tomer+% ta1le:+doGut0l.get/ert0ce+:cu+tomer+.cu+tGgeoGlocat0on;; t where cu+tomerG0d - :c0d.( ,+ - oc0Gpar+e:,c% ,+Fl;( ,c0d - 2??2( oc0G10ndG1yGname:,+% .:c0d.% ,c0d;( oc0Ge<ecute:,+;( oc0GfetchGall:,+% ,re+;( /arGdump:,re+;( !"

The ou$!u$ #s(


array:7; @ 3.I.4-" array:2; @ 3?4-" +tr0ng:>; .-T2.5M>7). A 3.J.4-" array:2; @ 3?4-" +tr0ng:M; .57.T7)5T. A A

,'in" a Cu'to4 =unction


3ome$#mes "ou ma" need $o crea$e a P.I3K. 5unc$#on $o decom!ose s!a$#al da$a #n$o s#m!le $"!es $o re$urn $hem $o PHP. Th#s e/am!le uses $he CBKAGOARCETS $able 5rom e/am!le 1?8 o5 $he Oracle Da$abase @/!ress 2 Day Plus 3ocator Developer Guide" http://download.oracle.com/doc+/cd/B76)7>G?2/doc/appde/.2?7/17M??5/<eGlocator.ht mRC9$EB9DA
1 6

/#ing Orac&e ,ocator .or S"atia& 9a""ing

-e5ore con$#nu#ng, e/ecu$e $he $hree s$a$emen$s g#'en #n $he manual $o crea$e $he $able, #nser$ $he me$a?da$a #n$o u+erG+doGgeomGmetadata $able, and crea$e $he #nde/. 7e/$, #nser$ a sam!le ro*. The ro* descr#bes a !ol"gon o5 :/,"; ord#na$es *h#ch are g#'en as !a#rs #n $he SDBGBRD9LATEGARRAJ arra"( Script ,?" cm!%sAl
0n+ert 0nto colaGmarNet+ /alue+ : )?2% -- marNet 9D num1er #polygon#% +doGgeometry : 7??)% -- two-d0men+0onal polygon null% null% +doGelemG0nfoGarray:2%2??)%2;% -- one polygon :e<ter0or polygon r0ng; +doGord0nateGarray:6%2% M%2% M%S% 6%T% 6%2; -- l0+t of I%J coord0nate+ ; ;( comm0t(

& decom!os#$#on 5unc$#on hel!s =uer" $he coord#na$es #n PHP. 7o$e $he al#as cm :an al#as here #s also +no*n as a correlation name; 5or $he $able #n $he =uer". Th#s allo*s $he +doGord0nate+ collec$#on $o be #ncluded as a selec$ column( Script 42" cm2%sAl
create or replace procedure myproc:pG0d 0n num1er% pGo out +doGord0nateGarray; a+ 1eg0n +elect cm.+hape.+doGord0nate+ 0nto pGo from colaGmarNet+ cm where mNtG0d - pG0d( end( / +how error+

The coord#na$es can no* be re$r#e'ed #n PHP as a collec$#on( Script 4!" cm%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,+ - oc0Gpar+e:,c% .1eg0n myproc::0d% :ord+;( end(.;( ,0d - )?2( oc0G10ndG1yGname:,+% .:0d.% ,0d;( ,ord+ - oc0GnewGcollect0on:,c% .SDBGBRD9LATEGARRAJ.;( oc0G10ndG1yGname:,+% .:ord+.% ,ord+% -2% BC9GBGLTJ;( oc0Ge<ecute:,+;( for :,0 - ?( ,0 ,ord+-"+0Qe:;( ,0DD; @ 1 7

/#ing P,;S1, With O(I ,/ - ,ord+-"getElem:,0;( echo .Value: ,/En.( A !"

The ou$!u$ #s $he l#s$ o5 coord#na$es $ha$ *ere #nser$ed #n $he SDBGBRD9LATEGARRAJ(
Value: Value: Value: Value: Value: Value: Value: Value: Value: Value: 6 2 M 2 M S 6 T 6 2

3#m#lar $echn#=ues $o $hese e/am!le g#'en, or $hose $echn#=ues #n $he earl#er sec$#on >sing P3<S@3 and S@3 Ob;ect #ypes in PHP can be used $o 5e$ch o$her .oca$or da$a, #5 re=u#red.

Schedu%in" <ac>"round or Lon" Runnin" Operation'


3ome$#mes a *eb !age s$ar$s a da$abase o!era$#on $ha$ can run #n $he bac+ground *h#le $he user con$#nues o$her *or+. 2or e/am!le, $here m#gh$ be some da$abase cleanu! $o be run !er#od#call". &no$her e/am!le #s *hen a user o5 a !ho$o s#$e dec#des $o change $he name o5 a $ag assoc#a$ed *#$h #mages. The !ho$o s#$e a!!l#ca$#on m#gh$ #n#$#a$e $he name change, bu$ re$urn $he user an HTM. !age sa"#ng Jour reFue+t 0+ 1e0ng proce++ed and w0ll +oon complete. The user can con$#nue '#e*#ng !ho$os *#$hou$ ha'#ng $o *a#$ 5or $he renam#ng !rocess $o com!le$e. Th#s $echn#=ue can #m!ro'e user sa$#s5ac$#on. >$ can also 5ree u! an &!ache ser'er $ha$ *ould o$her*#se be bloc+ed, allo*#ng #$ $o be used b" ano$her !age re=ues$. The DBOSGSC$EDUKER !ac+age can be used $o s$ar$ bac+ground da$abase $as+s. >$ has a lo$ o5 5unc$#onal#$", #nclud#ng allo*#ng $as+s $o be re!ea$ed a$ #n$er'als, or s$ar$ed *hen e'en$s are rece#'ed. >$ can also be used $o #n'o+e o!era$#ng s"s$em !rograms. >n Oracle Di, $he DBOSGYBB !ac+age can be used #ns$ead o5 DBOSGSC$EDUKER. 2or $he !ho$o s#$e e/am!le, crea$e some da$a *#$h $he #ncorrec$ $ag &eeding( Script 42" dschedinit%sAl
connect +y+tem/+y+tempwd grant create Zo1 to hr( connect hr/hrpwd drop ta1le tagGta1le( create ta1le tagGta1le :tag /archar7:7?;% photoG0d num1er;( 0n+ert 0nto tagGta1le /alue+ :#weedin,#% 7?)5;( 1

Schedu&ing <ac3ground or ,ong !unning O"eration# 0n+ert 0nto tagGta1le /alue+ :#weedin,#% 7?)6;( 0n+ert 0nto tagGta1le /alue+ :#+anfranc0+co#% 565?;( comm0t(

To change $he $ag weed0ng $o wedd0ng, a !rocedure changetagname:; can be crea$ed( Script 4'" dbsched%sAl
create or replace procedure changetagname:old 0n /archar7% new 0n /archar7; a+ 1 num1er( 1eg0n for 0 0n 2..2???????? loop 1 :- 2( end loop( -- +0mulate +low tran+act0on update tagGta1le +et tag - new where tag - old( comm0t( end( / +how error+

Th#s scr#!$ crea$es a sam!le $able and $he !rocedure $o u!da$e $ags. The !rocedure #s ar$#5#c#all" slo*ed do*n $o s#mula$e a b#g, long runn#ng da$abase o!era$#on. The 5ollo*#ng PHP scr#!$ uses an anon"mous bloc+ $o crea$e a 8ob call#ng changetagname:;. Script 40" dsched%php
!php ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( funct0on doFuery:,c; @ ,+ - oc0Gpar+e:,c% .+elect tag from tagGta1le.;( oc0Ge<ecute:,+;( oc0GfetchGall:,+% ,re+;( /arGdump:,re+;( A // Schedule a ta+N to change a tag name from #weed0ng# to #wedd0ng# ,+tmt .1eg0n d1m+G+cheduler.createGZo1: Zo1Gname -" :Zo1name% Zo1Gtype -" #STBREDG8RBCEDURE#% Zo1Gact0on -" #chan,eta,name#% num1erGofGargument+ -" 7;( d1m+G+cheduler.+etGZo1GargumentG/alue : Zo1Gname -" :Zo1name% argumentGpo+0t0on -" 2% argumentG/alue -" :old/al;( d1m+G+cheduler.+etGZo1GargumentG/alue : Zo1Gname -" :Zo1name% argumentGpo+0t0on -" 7% argumentG/alue -" :new/al;( d1m+G+cheduler.ena1le::Zo1name;(

// procedure to call

1 $

/#ing P,;S1, With O(I end(.( ,+ - oc0Gpar+e:,c% ,+tmt;( ,Zo1name - un0F0d:#ut#;( ,old/al - #weedin,#( ,new/al - #weddin,#( oc0G10ndG1yGname:,+% .:Zo1name.% ,Zo1name;( oc0G10ndG1yGname:,+% .:old/al.% ,old/al;( oc0G10ndG1yGname:,+% .:new/al.% ,new/al;( oc0Ge<ecute:,+;( echo . pre"Jour reFue+t 0+ 1e0ng proce++ed and w0ll +oon completeEn.( doFuery:,c;( // g0/e+ old re+ult+ +leep:2?;( echo .Jour reFue+t ha+ pro1a1ly completedEn.( doFuery:,c;( // g0/e+ new re+ult+ !"

The PHP call $o $he anon"mous P.I3K. bloc+ re$urns =u#c+l". The bac+ground P.I3K. call $o changetagname:; *#ll $a+e se'eral more seconds $o com!le$e :because o5 #$s for loo!;, so $he 5#rs$ doFuery:; ou$!u$ sho*s $he or#g#nal, #ncorrec$ $ag 'alues. Then, a5$er PHP has g#'en $he 8ob $#me $o conclude, $he second doFuery:; call sho*s $he u!da$ed 'alues(
Jour reFue+t 0+ 1e0ng proce++ed and w0ll +oon complete array:2; @ 3.TAW.4-" array:); @ 3?4-" +tr0ng:T; .weedin,. 324-" +tr0ng:T; .weedin,. 374-" +tr0ng:27; .+anfranc0+co. A A Jour reFue+t ha+ pro1a1ly completed array:2; @ 3.TAW.4-" array:); @ 3?4-" +tr0ng:T; .weddin,. 324-" +tr0ng:T; .weddin,. 374-" +tr0ng:27; .+anfranc0+co. A A

1$%

!eu#ing Procedure# Written .or 9O)4P,S1,

Reu'in" Procedure' Written 2or 8ODAPLS3L


OracleGs mod8plsAl ga$e*a" allo*s a 0eb bro*ser $o #n'o+e a P.I3K. s$ored sub!rogram $hrough an HTTP l#s$ener. Th#s #s $he #n$er5ace used b" Oracle &!!l#ca$#on @/!ress. @/#s$#ng user?crea$ed P.I3K. !rocedures *r#$$en 5or $h#s ga$e*a" can be called 5rom PHP us#ng a *ra!!er 5unc$#on. 2or e/am!le, cons#der a s$ored !rocedure 5or mod8plsAl $ha$ *as crea$ed #n 3K.TPlus( Script 4," myo&a%sAl
create or replace procedure myowa a+ 1eg0n htp.htmlBpen( htp.headBpen( htp.t0tle:#Wreet0ng T0tle#;( htp.headClo+e( htp.1odyBpen( htp.header:2% #Salutat0on $ead0ng#;( htp.p:#$ello% world'#;( htp.1odyClo+e( htp.htmlClo+e( end( / +how error+

Th#s genera$es HTM. ou$!u$ $o $he ga$e*a"(


$TOK" $EAD" T9TKE"Wreet0ng T0tle /T9TKE" /$EAD" BBDJ" $2"Salutat0on $ead0ng /$2" $ello% world' /BBDJ" /$TOK"

To reuse $he !rocedure d#rec$l" #n PHP, use 3K.TPlus $o crea$e a ma!!#ng 5unc$#on $ha$ !#!es $he ou$!u$ 5rom $he $T8 calls( Script 44" mymodplsAl%sAl
create or replace type modp+row a+ ta1le of /archar7:627;( / +how error+ create or replace funct0on mymodpl+Fl:proc /archar7; return modp+row p0pel0ned 0+ paramG/al owa./cGarr( l0ne /archar7:76S;( 0row+ 0nteger( 1eg0n owa.0n0tGcg0Gen/:paramG/al;( htp.0n0t( e<ecute 0mmed0ate #1eg0n #XXprocXX#( end(#( loop 1$1

/#ing P,;S1, With O(I l0ne :- htp.getGl0ne:0row+;( e<0t when l0ne 0+ null( p0pe row :l0ne;( end loop( return( end( / +how error+

Th#s #s 5undamen$all" s#m#lar $o $he !re'#ous !#!el#ned e/am!les. >n modp+row:; "ou can o!$#onall" use paramG/al $o se$ A> 'alues. 3ee $he de5#n#$#on o5 0n0t.cg0Gen/:; #n EORA(3E8HO6E<rdbms<admin<privo&a%sAl 5or de$a#ls. >n PHP, $he ne* *ra!!er can be called l#+e( Script 4-" mymodplsAl%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,+tmt - oc0Gpar+e:,c% #+elect H from ta1le:mymodpl+Fl::proc;;#;( ,func - #myowa#( oc0G10ndG1yGname:,+tmt% #:proc#% ,func;( oc0Ge<ecute:,+tmt;( ,content - fal+e( wh0le :,row - oc0GfetchGarray:,+tmt% BC9GASSBC;; @ 0f :,content; @ pr0nt ,row3.CBKUOLGVAKUE.4( A el+e @ 0f :,row3.CBKUOLGVAKUE.4 -- .En.; ,content - true( el+e header:,row3.CBKUOLGVAKUE.4;( A A !"

0hen called #n a bro*ser, $he ou$!u$ #s $he e/!ec$ed rend#$#on o5 $he HTM. 5ragmen$ sho*n earl#er.

1$2

CHAPTER 1

,SIN! LAR!E O<JECTS IN OCI0


Oracle harac$er .arge Ob8ec$ : .O-; and -#nar" .arge Ob8ec$ :-.O-; $"!es can con$a#n 'er" large amoun$s o5 da$a. The" can be used 5or $able columns and 5or P.I3K. 'ar#ables. There are 'ar#ous crea$#on o!$#ons $o s!ec#5" o!$#mal Oracle $able s$orage. & !re?su!!l#ed DBOSGKBB !ac+age ma+es man#!ula$#on #n P.I3K. eas". Oracle also has a -2>.@ $"!e 5or large ob8ec$s s$ored ou$s#de $he da$abase.

Wor>in" 9ith LO<'


>n success#'e 'ers#ons, $he Oracle da$abase has made #$ eas#er $o *or+ *#$h .O-s. &long $he *a" MTem!orar" .O-sO *ere added, and some s$r#ng $o .O- con'ers#ons are no* $rans!aren$ so da$a can be handled d#rec$l" as s$r#ngs. De'elo! and $es$ "our .O- a!!l#ca$#on *#$h $he Oracle cl#en$ l#brar#es and da$abase $ha$ *#ll be used 5or de!lo"men$ so "ou can be sure all $he e/!ec$ed 5unc$#onal#$" #s a'a#lable. 0hen *or+#ng *#$h large amoun$s o5 da$a, se$ memoryGl0m0t a!!ro!r#a$el" #n php%ini o$her*#se PHP ma" $erm#na$e earl". 0hen read#ng or *r#$#ng 5#les $o d#s+, chec+ #5 openG1a+ed0r allo*s 5#le access. These e/am!le sho* -.O-s. Us#ng .O-s #s almos$ #den$#cal $o us#ng -.O-s( $he descr#!$or $"!e #s BC9GDGCKBB, $he b#nd $"!e becomes BC9GBGCKBB, and $ables mus$ ob'#ousl" con$a#n a CKBB column. The e/am!les use a $able crea$ed #n 3K.TPlus con$a#n#ng a -.O- column called 1lo1data(
SUK" create ta1le my1ta1 :1lo10d num1er pr0mary Ney% b(obdata b(ob;(

7o$e =uer"#ng -.O- columns #n 3K.TPlus #s no$ !oss#ble unless 3K.TPlus 11g #s used, *here #$ *#ll d#s!la" a he/adec#mal 'ers#on o5 $he da$a. Tables *#$h .O- columns can be =uer#ed #n all 'ers#ons o5 3K.TPlus. The ou$!u$ o5 -.O- and .O- da$a can be con$rolled #n 3K.TPlus *#$h $he SET KBLW command. The de5aul$ 'alue o5 M? means $ha$ onl" $he 5#rs$ 80 charac$ers o5 da$a *#ll be d#s!la"ed b" a =uer".

In'ertin" and ,pdatin" LO<'


>n PHP, .O-s are generall" man#!ula$ed us#ng a descr#!$or. PHP code $o #nser$ #n$o my1ta1 #s( Script 4*" blobinsert%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,my1lo10d - 27)( ,my/ - #a /ery large amount of 10nary data#( ,+ - oc0Gpar+e:,c% #0n+ert 0nto my1ta1 :1lo10d% 1lo1data; /alue+ ::my1lo10d% EO8TJGBKBB:;; return0ng 1lo1data 0nto :1lo1data#;( 1$3

/#ing ,arge O*>ect# in O(I ,lo1 - oc0GnewGde+cr0ptor:,c% BC9GDGKBB;( oc0G10ndG1yGname:,+% #:my1lo10d#% ,my1lo10d;( oc0G10ndG1yGname:,+% #:1lo1data#% ,lo1% -2% BC9GBGBKBB;( oc0Ge<ecute:,+% BC9GDE*AUKT;( // u+e BC9GDE*AUKT +o ,lo1-"+a/e:; worN+ ,lo1-"+a/e:,my/;( oc0Gcomm0t:,c;( ,lo1-"clo+e:;( !"

// clo+e KBB de+cr0ptor to free re+ource+

The RETURL9LW clause re$urns $he Oracle .O- loca$or o5 $he ne* ro*. -" b#nd#ng as BC9GBGBKBB, $he PHP descr#!$or #n ,lo1 re5erences $h#s loca$or. The ,lo1-"+a/e:; me$hod $hen s$ores $he da$a #n ,my/ #n$o $he -.O- column. The BC9GDE*AUKT 5lag #s used 5or oc0Ge<ecute:; so $he descr#!$or rema#ns 'al#d 5or $he +a/e:; me$hod. The comm#$ concludes $he #nser$ and ma+es $he da$a '#s#ble $o o$her da$abase users. >5 $he a!!l#ca$#on u!loads .O- da$a us#ng a *eb 5orm, #$ can be #nser$ed d#rec$l" 5rom $he u!load d#rec$or" *#$h ,lo1-"0mport:,f0lename;. PHPLs ma/#mum allo*ed s#1e 5or u!loaded 5#les #s se$ #n php%ini us#ng $he uploadGma<Gf0le+0Qe !arame$er. To u!da$e a .O-, use $he same code *#$h $h#s 3K. s$a$emen$(
,+ - oc0Gpar+e:,c% #update my1ta1 +et 1lo1data - emptyG1lo1:; return0ng 1lo1data 0nto :1lo1data#;(

=etchin" LO<'
0hen 5e$ch#ng a .O-, O >8 re$urns $he .O- descr#!$or and $he da$a can be re$r#e'ed b" us#ng a load:; or read:; me$hod( Script 4?" blob$etch%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,my1lo10d - 27)( ,Fuery - #+elect 1lo1data from my1ta1 where 1lo10d - :my1lo10d#( ,+ - oc0Gpar+e :,c% ,Fuery;( oc0G10ndG1yGname:,+% #:my1lo10d#% ,my1lo10d;( oc0Ge<ecute:,+;( ,arr - oc0GfetchGarray:,+% BC9GASSBC;( 0f :0+Go1Zect:,arr3#BKBBDATA#4;; @ // protect aga0n+t a LUKK KBB ,data - ,arr3#BKBBDATA#4-"load:;( ,arr3#BKBBDATA#4-"free:;( echo ,data( A !"

>$ #s #m!or$an$ $o 5ree all re$urned .O- loca$ors $o a'o#d lea+s(


1$4

Wor3ing with ,O<# wh0le ::,arr - oc0GfetchGarray:,+% BC9GASSBC;;; @ echo ,arr3#BKBBDATA#4-"load:;( // do +ometh0ng w0th the BKBB ,arr3#BKBBDATA#4-")ree:;( // cleanup 1efore ne<t fetch A

>5 .O-3 are no$ 5reed, $he ABSTRACTGKBBS column #n $he V,TEO8BRARJGKBBS $able *#ll sho* #ncreas#ng 'alues. >ns$ead o5 us#ng loca$ors, .O- da$a can al$erna$#'el" be re$urned as a s$r#ng(
,arr - oc0GfetchGarray:,+% BC9GASSBCDBC9GRETURLGKBBS;( echo ,arr3#BKBBDATA#4(

>5 $he re$urned da$a #s larger $han e/!ec$ed, PHP ma" no$ be able $o alloca$e enough memor". To !ro$ec$ aga#ns$ $h#s, use a loca$or *#$h $he read:; me$hod, *h#ch allo*s $he da$a s#1e $o be l#m#$ed.

Te4porar: LO<'
Tem!orar" .O-s ma+e some o!era$#ons eas#er. >nser$#ng da$a *#$h a Tem!orar" .O- does no$ use a RETURL9LW 9LTB clause( Script -2" tempblobinsert%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,my1lo10d - 275( ,my/ - #a /ery large amount of 10nary data#( ,+ - oc0Gpar+e:,c% #0n+ert 0nto my1ta1 :1lo10d% 1lo1data; /alue+ ::my1lo10d% :1lo1data;#;( ,lo1 - oc0GnewGde+cr0ptor:,c% BC9GDGKBB;( oc0G10ndG1yGname:,+% #:my1lo10d#% ,my1lo10d;( oc0G10ndG1yGname:,+% #:1lo1data#% ,lo1% -2% BC9GBGBKBB;( ,lo1-"wr0teTemporary:,my/% BC9GTEO8GBKBB;( oc0Ge<ecute:,+% BC9GDE*AUKT;( oc0Gcomm0t:,c;( ,lo1-"clo+e:;( // clo+e lo1 de+cr0ptor to free re+ource+ !"

Tem!orar" .O-s also s#m!l#5" u!da$#ng 'alues(


,+ - oc0Gpar+e:,c% #update my1ta1 +et 1lo1data - :1d where 1lo10d - :10d#;(

>5 "ou *an$ $o e#$her #nser$ a ne* ro* or u!da$e e/#s$#ng da$a #5 $he ro* $urns ou$ $o e/#s$ alread", $he 3K. s$a$emen$ can be changed $o use an anon"mous bloc+ (
,+ - oc0Gpar+e:,c% #1eg0n# . # 0n+ert 0nto my1ta1 :1lo1data% 1lo10d; /alue+::1lo1data% :my1lo10d;(# . # e<cept0on# . # when dupG/alGonG0nde< then# 1$5

/#ing ,arge O*>ect# in O(I . # update my1ta1 +et 1lo1data - :1lo1data where 1lo10d - :my1lo10d(# . #end(#;(

LO<' and PL5S3L procedure'


Tem!orar" .O-s can also be used $o !ass da$a $o P.I3K. 9L, and re$urned 5rom BUT !arame$ers. A#'en a P.I3K. !rocedure $ha$ acce!$s a -.O- and #nser$s #$ #n$o my1ta1( Script -!" inproc%sAl
create or replace procedure 0nproc:p0d 0n num1er% pdata 0n 1lo1; a+ 1eg0n 0n+ert 0nto my1ta1 :1lo10d% 1lo1data; /alue+ :p0d% pdata;( end( / +how error+

PHP code $o !ass a -.O- $o 9L8RBC *ould loo+ l#+e( Script -2" inproc%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,my1lo10d - 276( ,my/ - #a /ery large amount of 10nary data#( ,+ - oc0Gpar+e:,c% #1eg0n 0nproc::my1lo10d% :my1lo1data;( end(#;( ,lo1 - oc0GnewGde+cr0ptor:,c% BC9GDGKBB;( oc0G10ndG1yGname:,+% #:OJBKBB9D#% ,my1lo10d;( oc0G10ndG1yGname:,+% #:OJBKBBDATA#% ,lo1% -2% BC9GBGBKBB;( ,lo1-"wr0teTemporary:,my/% BC9GTEO8GBKBB;( oc0Ge<ecute:,+;( ,lo1-"clo+e:;( !"

>5 $he P.I3K. !rocedure re$urns a -.O- as an BUT !arame$er( Script -'" outproc%sAl
create or replace procedure outproc:p0d 0n num1er% pdata out 1lo1; a+ 1eg0n +elect 1lo1data 0nto pdata from my1ta1 where 1lo10d - p0d( end( / +how error+

PHP code $o 5e$ch and d#s!la" $he -.O- *ould loo+ l#+e(

1$6

Wor3ing with ,O<#

Script -0" outproc%php


!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,my1lo10d - 276( ,+ - oc0Gpar+e:,c% .1eg0n outproc::my1lo10d% :my1lo1data;( end(.;( ,lo1 - oc0GnewGde+cr0ptor:,c% BC9GDGKBB;( oc0G10ndG1yGname:,+% #:OJBKBB9D#% ,my1lo10d;( oc0G10ndG1yGname:,+% #:OJBKBBDATA#% ,lo1% -2% BC9GBGBKBB;( oc0Ge<ecute:,+% BC9GDE*AUKT;( 0f :0+Go1Zect:,lo1;; @ // protect aga0n+t a LUKK KBB ,data - ,lo1-"load:;( ,lo1-"free:;( echo ,data( A !"

Other LO< 8ethod'


& number o5 o$her me$hods on $he .O- descr#!$or allo* see+#ng $o a s!ec#5#ed o55se$, e/!or$#ng da$a d#rec$l" $o 5#le, eras#ng da$a, and co!"#ng or com!ar#ng a .O-. Th#s code sn#!!e$ sho*s see+#ng $o $he 10$h !os#$#on #n $he resul$ descr#!$or, and $hen s$or#ng $he ne/$ 50 b"$es #n ,re+ult"
,arr3#BKBBDATA#4-"+eeN:2?% BC9GSEECGSET;( ,re+ult - ,arr3#BKBBDATA#4-"read:6?;(

The 5ull l#s$ o5 .O- me$hods and 5unc$#ons #s sho*n #n Table 10. hec+ $he PHP manual 5or usage.

#able !2" 3OC methods and $unctions%


PHP =unction or 8ethod BC9-Ko1-"clo+e BC9-Ko1-"eof BC9-Ko1-"era+e BC9-Ko1-"e<port BC9-Ko1-"wr0teTo*0le BC9-Ko1-"flu+h BC9-Ko1-"free 2lushes bu55er o5 $he .O- $o $he ser'er 2rees da$abase resources assoc#a$ed *#$h $he .OAction lose a .O- descr#!$or Tes$ 5or .O- end?o5?5#le @rases a s!ec#5#ed !ar$ o5 $he .O0r#$e a .O- $o a 5#le

1$7

/#ing ,arge O*>ect# in O(I

PHP =unction or 8ethod BC9-Ko1-"getBuffer0ng BC9-Ko1-"0mport BC9-Ko1-"+a/e*0le BC9-Ko1-"load BC9-Ko1-"read BC9-Ko1-"rew0nd BC9-Ko1-"+a/e BC9-Ko1-"+eeN BC9-Ko1-"+etBuffer0ng BC9-Ko1-"+0Qe BC9-Ko1-"tell BC9-Ko1-"truncate BC9-Ko1-"wr0te BC9-Ko1-"wr0teTemporary oc0Glo1Gcopy oc0Glo1G0+GeFual

Action Re$urns curren$ s$a$e o5 bu55er#ng 5or $he .O.oads da$a 5rom a 5#le $o a .O-

Re$urns .O- con$en$s Re$urns !ar$ o5 $he .OMo'es $he .O-Ls #n$ernal !o#n$er bac+ $o $he beg#nn#ng 3a'es da$a $o $he .O3e$s $he .O-Gs #n$ernal !os#$#on !o#n$er hanges .O-Gs curren$ s$a$e o5 bu55er#ng Re$urns s#1e o5 .ORe$urns curren$ !o#n$er !os#$#on Trunca$es a .O0r#$es da$a $o $he .O0r#$es a $em!orar" .Oo!#es a .Oom!are $*o .O- loca$ors 5or e=ual#$"

Wor>in" 9ith <=ILE'


& -2>.@ #s an Oracle large ob8ec$ :.O-; da$a $"!e 5or 5#les s$ored ou$s#de $he da$abase. -2>.@s are a hand" *a" 5or us#ng rela$#'el" s$a$#c, e/$ernall" crea$ed con$en$. The" are also use5ul 5or load#ng $e/$ or b#nar" da$a #n$o Oracle $ables. >n 3K. and P.I3K., a -2>.@ #s accessed '#a a loca$or, *h#ch #s s#m!l" a !o#n$er $o $he e/$ernal 5#le. There are numerous !re?su!!l#ed 5unc$#ons $ha$ o!era$e on -2>.@ loca$ors. To sho* ho* -2>.@s *or+ #n PHP $h#s sec$#on crea$es a sam!le a!!l#ca$#on $ha$ accesses and d#s!la"s a s#ngle #mage. The #mage *#ll no$ be loaded #n$o $he da$abase bu$ $he !#c$ure descr#!$#on #s loaded so #$ can be =uer#ed. The -2>.@ allo*s $he #mage $o be rela$ed $o $he descr#!$#on. &lso $he a!!l#ca$#on could be e/$ended #n 5u$ure $o use P.I3K. !ac+ages $o read and man#!ula$e $he #mage.

1$

Wor3ing with <:I,'#

>n $h#s e/am!le, $he #mage da$a #s no$ loaded and !r#n$ed #n PHP. >ns$ead, $he bro*ser #s red#rec$ed $o $he #mage UR. o5 $he e/$ernal 5#le. Th#s s#gn#5#can$l" reduces $he amoun$ o5 da$a $ha$ needs $o be handled b" $he a!!l#ca$#on. To allo* &!ache $o ser'e $he #mage, ed#$ httpd%con$ and ma! a UR. $o $he d#rec$or" con$a#n#ng $he 5#le. 2or e/am!le #5 $he 5#le #s <tmp<c;%;pg add(
Al0a+ /tmp/ ./tmp/. D0rectory ./tmp/." Bpt0on+ Lone AllowB/err0de Lone Brder allow%deny Allow from all /D0rectory"

Us#ng <tmp l#+e $h#s #s no$ recommended 5or an"$h#ng e/ce!$ $es$#ngR Res$ar$ &!ache and use a bro*ser $o chec+ $ha$ http://localho+t/tmp/cZ.Zpg loads $he !#c$ure #n <tmp<c;%;pg% >n Oracle, crea$e a D>R@ TORH al#as 5or <tmp. Th#s #s OracleLs !o#n$er $o $he o!era$#ng s"s$em and 5orms !ar$ o5 each -2>.@. The d#rec$or" mus$ be on $he same mach#ne $ha$ $he da$abase ser'er runs on. 3$ar$ 3K.TPlus as(
, +Flplu+ +y+tem/+y+tempwdPlocalho+t/IE

Then run b$ile%sAl( Script -," b$ile%sAl create d0rectory Te+tD0r AS #/tmp#( grant read on d0rectory Te+tD0r to hr( connect hr/hrpwdPlocalho+t/IE create ta1le *0leTe+t : *0leLum num1er pr0mary Ney% *0leDe+c /archar7:)?;% 9mage 1f0le;( Th#s g#'es $he hr user access $o $he <tmp d#rec$or" and crea$es a $able ile#est con$a#n#ng a 5#le number #den$#5#er, a $e/$ descr#!$#on o5 $he 5#le, and $he -2>.@ #$sel5. The #mage da$a #s no$ loaded #n$o $h#s $able< $he -2>.@ #n $he $able #s a !o#n$er $o $he 5#le on "our 5#le s"s$em. PHP code $o #nser$ $he #mage name #n$o $he ile#est $able loo+s l#+e( Script -4" b$ileinsert%php
!php ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( ,fnum - 2( ,fd+c - .Some de+cr0pt0on to +earch.( ,name - .cZ.Zpg.( ,+ - oc0Gpar+e:,c% .0n+ert 0nto *0leTe+t :*0leLum% *0leDe+c% 9mage; . . ./alue+ ::fnum% :fd+c% 1f0lename:#TESTD9R#% :name;;.;( 1$$

/#ing ,arge O*>ect# in O(I oc0G10ndG1yGname:,+% .:fnum.% ,fnum% -2% SUKTG9LT;( oc0G10ndG1yGname:,+% .:fd+c.% ,fd+c% -2% SUKTGC$R;( oc0G10ndG1yGname:,+% .:name.% ,name% -2% SUKTGC$R;( oc0Ge<ecute:,+% BC9GDE*AUKT;( oc0Gcomm0t:,c;( !"

The 1f0lename:; cons$ruc$or #nser$s #n$o $he -2>.@?$"!e column us#ng $he TESTD9R d#rec$or" al#as crea$ed earl#er. -#nd 'ar#ables are used 5or e55#c#enc" and secur#$". Th#s ne* -2>.@ can be =uer#ed bac+ #n PHP( Script --" b$ileAuery!%php
!php ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( ,fnum - 2( ,+ - oc0Gpar+e:,c% .+elect 9mage from *0leTe+t where *0leLum - :fnum.;( oc0G10ndG1yGname:,+% .:fnum.% ,fnum;( oc0Ge<ecute:,+;( ,row - oc0GfetchGa++oc:,+;( ,1f - ,row3#9OAWE#4( // Th0+ 0+ a B*9KE de+cr0ptor echo . pre".( /arGdump:,1f;( echo . /pre".( !"

Th#s d#s!la"s $he -2>.@ descr#!$or(


o1Zect:BC9-Ko1;R2 :2; @ 3.de+cr0ptor.4-" re+ource:T; of type :oc0M de+cr0ptor; A

2or s#m!l#c#$", $he =uer" cond#$#on #s $he 5#le number o5 $he ne* record. >n real l#5e #$ m#gh$ use a regular e/!ress#on on $he *0leDe+c 5#eld l#+e(
+elect 9mage from *0leTe+t where rege<pGl0Ne:*0leDe+c% #+omepattern#;

7o* *ha$Y >n $h#s e/am!le $he 5#le name #s needed so $he bro*ser can red#rec$ $o a !age sho*#ng $he #mage. Un5or$una$el" $here #s no d#rec$ me$hod #n PHP $o ge$ $he 5#lename 5rom $he descr#!$or. Ho*e'er, an Oracle !rocedure can be crea$ed $o do $h#s. >ns$ead o5 e/ecu$#ng $he =uer" #n PHP and us#ng P.I3K. $o 5#nd $he 5#lename, a more e55#c#en$ me$hod #s $o do bo$h #n P.I3K.. Here an anon"mous bloc+ #s used. &l$erna$#'el", a !rocedure could be used. The !re'#ous =uer" code #n b$ileAuery!%php can be re!laced *#$h( Script -*" sho&pic%php
!php ,c - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;(

2%%

Wor3ing with <:I,'# ,+ - oc0Gpar+e:,c% #declare # . #1Gl 1f0le(# . #daGl /archar7:766;(# . #1eg0n # . #+elect 0mage 0nto 1Gl from f0lete+t where f0lenum - :fnum(# . #d1m+Glo1.f0legetname:1Gl% daGl% :name;(# . #end(#;( ,fnum - 2( oc0G10ndG1yGname:,+% .:fnum.% ,fnum;( oc0G10ndG1yGname:,+% .:name.% ,name% 766% SUKTGC$R;( oc0Ge<ecute:,+;( header:.Kocat0on: http://localho+t/tmp/,name.;( !"

The 5#lename c;%;pg #s re$urned #n ,name cour$es" o5 $he :name b#nd 'ar#able argumen$ $o $he DBOSGKBB.*9KEWETLAOE:; 5unc$#on. The header:; 5unc$#on red#rec$s $he user $o $he #mage. >5 an" $e/$ #s !r#n$ed be5ore $he header:; #s ou$!u$, $he HTTP headers *#ll no$ be correc$ and $he #mage *#ll no$ d#s!la". >5 "ou ha'e !roblems, commen$ ou$ $he header:; call and echo ,name $o chec+ #$ #s 'al#d. -2>.@s are eas" $o *or+ *#$h #n P.I3K. because $he !re?su!!l#ed DBOSGKBB !ac+age has a number o5 use5ul 5unc$#ons. 2or e/am!le DBOSGKBB.KBAD*RBO*9KE:; reads -2>.@ da$a 5rom $he 5#le s"s$em #n$o a P.I3K. -.O- or .O-. Th#s could be loaded #n$o a -.O- $able column, man#!ula$ed #n P.I3K., or e'en re$urned $o PHP us#ng O >8Ls .O- 5ea$ures. &no$her e/am!le #s DBOSGKBB.*9KEEI9STS:;, *h#ch can be used $o chec+ *he$her $he ile#est $able con$a#ns re5erences $o #mages $ha$ do no$ e/#s$. -2>.@s are 'er" use5ul 5or man" !ur!oses #nclud#ng load#ng #mages #n$o $he da$abase, bu$ -.O-s ma" be be$$er #n some c#rcums$ances. hanges $o -2>.@ loca$ors can be rolled bac+ or comm#$$ed bu$ s#nce $he 5#les $hemsel'es are ou$s#de $he da$abase, -2>.@ da$a does no$ !ar$#c#!a$e #n $ransac$#ons. Hou can ha'e dangl#ng re5erences $o -2>.@s because Oracle does no$ chec+ $he 'al#d#$" o5 -2>.@s un$#l $he da$a #s e/!l#c#$l" read :$h#s allo*s "ou $o !re?crea$e -2>.@s or $o change $he !h"s#cal da$a on d#s+;. -2>.@ da$a 5#les are read?onl" and canno$ be changed *#$h#n Oracle. 2#nall", -2>.@s need $o be bac+ed u! manuall". -ecause o5 $hese !o#n$s, $here m#gh$ be $#mes "ou should use -.O-s $o s$ore #mages #ns#de $he da$abase $o ensure da$a and a!!l#ca$#on cons#s$enc" bu$ -2>.@s are $here #5 "ou *an$ $hem.

2%1

/#ing ,arge O*>ect# in O(I

2%2

CHAPTER 1$

,SIN! 78L WITH ORACLE AND PHP


-o$h Oracle and PHP 5 ha'e e/cellen$ NM. ca!ab#l#$#es, allo*#ng lo$s o5 cho#ce 5or !rocess#ng #n5orma$#on. &ll ed#$#ons o5 Oracle con$a#n *ha$ #s +no*n as MNM. D-O, $he NM. ca!ab#l#$#es o5 $he da$abase. 0hen $ables are crea$ed, NM. can be s$ored #n l#near .O- 5orma$, or accord#ng $o $he s$ruc$ure o5 "our NM. schema. Th#s ha!$er co'ers $he bas#cs o5 re$urn#ng NM. da$a 5rom Oracle $o PHP. >$ also sho*s ho* $o access da$a o'er HTTP d#rec$l" 5rom $he da$abase.

=etchin" Re%ationa% Ro9' a' 78L


One use5ul 5ea$ure o5 NM. D- #s $ha$ e/#s$#ng rela$#onal 3K. $ables can au$oma$#call" be re$r#e'ed as NM.. The re$urned 'alues are NM. 5ragmen$s, and no$ 5ull" 5ormed NM. documen$s. Script -?" xml$rag%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,Fuery #+elect <mlelement:.Employee+.% <mlelement:.Lame.% employee+.la+tGname;% <mlelement:.9d.% employee+.employeeG0d;; a+ re+ult from employee+ where employeeG0d " 7??#( ,+ - oc0Gpar+e:,c% ,Fuery;( oc0Ge<ecute:,+;( wh0le :,row - oc0GfetchGarray:,+% BC9GLUO;; @ foreach :,row a+ ,0tem; @ echo htmlent0t0e+:,0tem;.. 1r"En.( A A !"

The ou$!u$ #s(


Employee+" Employee+" Employee+" Employee+" Employee+" Employee+" Lame"$art+te0n /Lame" 9d"7?2 /9d" /Employee+" Lame"*ay /Lame" 9d"7?7 /9d" /Employee+" Lame"Oa/r0+ /Lame" 9d"7?) /9d" /Employee+" Lame"Baer /Lame" 9d"7?5 /9d" /Employee+" Lame"$0gg0n+ /Lame" 9d"7?6 /9d" /Employee+" Lame"W0etQ /Lame" 9d"7?S /9d" /Employee+"

2%3

/#ing +9, with Orac&e and PHP

Tip: Watch out for the :uotin& of 0"9 :ueries. The 0"9 functions can have embedded double :uotes. This is the e>act opposite of standard ;@9 :ueries, which can have embedded sin&le :uotes.

There are a number o5 o$her NM. 5unc$#ons $ha$ can be s#m#larl" used. 3ee $he Oracle Database S@3 Re$erence.

=etchin" Ro9' a' =u%%: =or4ed 78L


&no$her *a" $o crea$e NM. 5rom rela$#onal da$a #s $o use $he P.I3K. !ac+age DBOSGIOKWEL:;. Th#s !ac+age re$urns a 5ull" 5ormed NM. documen$, *#$h $he NM. header. Kuer#es $ha$ use DBOSGIOKWEL:; re$urn a .O- column, so $he #n#$#al resul$ needs $o be $rea$ed #n PHP as a .O- descr#!$or. There #s e55ec$#'el" no leng$h l#m#$ 5or .O-s. The 5ollo*#ng e/am!le =uer#es $he 5#rs$ name o5 em!lo"ees #n de!ar$men$ P0 and s$ores $he NM. mar+ed?u! ou$!u$ #n ,mylo1( Script *2" getxml%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,Fuery - .+elect d1m+G<mlgen.get<ml:# +elect f0r+tGname from employee+ where departmentG0d - )?#; <ml from dual.( ,+ - oc0Gpar+e:,c% ,Fuery;( oc0Ge<ecute:,+;( ,row - oc0GfetchGarray:,+% BC9GLUO;( ,mylo1 - ,row3?4-"load:;( // treat re+ult a+ a KBB de+cr0ptor ,row3?4-"free:;( echo . pre"En.( echo htmlent0t0e+:,mylo1;( echo . /pre"En.( !"

The 'alue o5 ,mylo1 #s(


!<ml /er+0on-.2.?.!" RB&SET" RB&" *9RSTGLAOE"Den /*9RSTGLAOE" /RB&" RB&" *9RSTGLAOE"Ale<ander /*9RSTGLAOE" /RB&" RB&" 2%4

:etching !ow# a# :u&&y :or7ed +9, *9RSTGLAOE"Shell0 /*9RSTGLAOE" /RB&" RB&" *9RSTGLAOE"S0gal /*9RSTGLAOE" /RB&" RB&" *9RSTGLAOE"Wuy /*9RSTGLAOE" /RB&" RB&" *9RSTGLAOE"Caren /*9RSTGLAOE" /RB&" /RB&SET"

,'in" the Si4p%e78L E&ten'ion in PHP


Hou can use PHP 5Ls Simple=63 e/$ens#on $o con'er$ NM. $o a PHP ob8ec$. 2ollo*#ng on 5rom $he !re'#ous e/am!le $he =uer" resul$s can be con'er$ed *#$h(
,<o - +0mple<mlGloadG+tr0ng::10nary;,mylo1;( /arGdump:,<o;(

7o$e $he cas$ $o 10nary *h#ch ensures cons#s$en$ encod#ng. The ou$!u$ #s(
o1Zect:S0mpleIOKElement;R7 :2; 3.RB&.4-" array:S; @ 3?4-" o1Zect:S0mpleIOKElement;R) 3.*9RSTGLAOE.4-" +tr0ng:); .Den. A 324-" o1Zect:S0mpleIOKElement;R5 3.*9RSTGLAOE.4-" +tr0ng:>; .Ale<ander. A 374-" o1Zect:S0mpleIOKElement;R6 3.*9RSTGLAOE.4-" +tr0ng:S; .Shell0. A 3)4-" o1Zect:S0mpleIOKElement;RS 3.*9RSTGLAOE.4-" +tr0ng:6; .S0gal. A 354-" o1Zect:S0mpleIOKElement;RT 3.*9RSTGLAOE.4-" +tr0ng:); .Wuy. A 364-" o1Zect:S0mpleIOKElement;RM @

:2; @

:2; @

:2; @

:2; @

:2; @

:2; @ 2%5

/#ing +9, with Orac&e and PHP 3.*9RSTGLAOE.4-" +tr0ng:6; .Caren. A A A

Th#s ob8ec$ can be accessed *#$h arra" #$era$ors or !ro!er$#es(


foreach :,<o-"RB& a+ ,r; @ echo .Lame: . . ,r-"*9RSTGLAOE . . 1r"En.( A

Th#s ou$!u$ 5rom $he loo! #s(


Lame: Lame: Lame: Lame: Lame: Lame: Den Ale<ander Shell0 S0gal Wuy Caren

There are more e/am!les o5 us#ng Simple=63 *#$h NM. da$a #n PHPGs $es$ su#$e under $he ext<simplexml<tests d#rec$or" o5 $he PHP source code bundle. &s an al$erna$#'e $o Simple=63 "ou could use PHPLs older DO6 e/$ens#on. There #s an ar$#cle >sing PHP, &ith Oracle =63 DC b" Hul# ,as#l#e' #n $he )ul"I&ugus$ 2005 Oracle Maga1#ne $ha$ d#scusses $h#s.

=etchin" 78LT:pe Co%u4n'


Da$a #n NM.T"!e columns could be longer $han OracleGs E000 b"$e s$r#ng leng$h l#m#$. 0hen da$a #s 5e$ched as a s$r#ng, =uer#es ma" 5a#l #n some cases de!end#ng on $he da$a leng$h. 2or e/am!le, here #s a bas#c =uer" on $he RESBURCEGV9E& :*h#ch #s a *a" access $he Oracle NM. D- re!os#$or" 5rom 3K.;. The RES column #s an NM.T"!e(
,+ - oc0Gpar+e:,c% #+elect re+ from re+ourceG/0ew#;( oc0Ge<ecute:,+;( wh0le :,row - oc0GfetchGarray:,+% BC9GASSBC;; @ /arGdump:,row;( A

Th#s #s l#+el" $o success5ull" 5e$ch and d#s!la" some ro*s be5ore 5a#l#ng. The 5a#lure ha!!ens because $he da$abase does a con'ers#on 5rom NM.T"!e $o a s$r#ng be5ore re$urn#ng resul$s $o PHP. 0hen $he s$r#ng #s $oo long, an error #s $hro*n(
8$8 &arn0ng: oc0GfetchGarray:;: BRA-2>?22: Character +tr0ng 1uffer too +mall

0hen $he ro*s are shor$ $here #s no error. Dur#ng $es$#ng "ou could be $r#c+ed #n$o $h#n+#ng "our =uer" *#ll al*a"s re$urn a com!le$e se$ o5 ro*s. Use $he IOKTJ8E.WETCKBBVAK:; 5unc$#on $o 5orce NM.T"!e con'ers#on $o re$urn a .O-, a'o#d#ng $he s$r#ng s#1e l#m#$ !roblem. 3$andard O >8 .O- me$hods can be used on $he re$urned da$a(

2%6

:etching +9,-y"e (o&u7n#

Script *!" xmltype%php


!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,+ - oc0Gpar+e:,c% #+elect <m(type.,etc(ob.a(:re+; from re+ourceG/0ew#;( oc0Ge<ecute:,+;( wh0le :,row - oc0GfetchGarray:,+% BC9GLUO;; @ /arGdump:,row3?4-"load:;;( ,row3?4-"free:;( A !"

In'ertin" into 78LT:pe Co%u4n'


Hou can #nser$ or u!da$e NM.T"!e columns b" b#nd#ng as a .O-. Th#s e/am!le u!da$es a $able *#$hou$ an NM.3chema, and *h#ch s$ores $he NM.T"!e column as a .O-. Script *2" xmlinsert%sAl
create ta1le <warehou+e+ :warehou+eG0d num1er% warehou+eG+pec <mltype; <mltype warehou+eG+pec +tore a+ clo1( 0n+ert 0nto <warehou+e+ :warehou+eG0d% warehou+eG+pec; /alue+ :2% <mltype:# !<ml /er+0on-.2.?.!" &arehou+e" &arehou+e9d"2 /&arehou+e9d" &arehou+eLame"SouthlaNe% Te<a+ /&arehou+eLame" Bu0ld0ng"Bwned /Bu0ld0ng" Area"76??? /Area" DocN+"7 /DocN+" DocNType"Rear load /DocNType" &aterAcce++"true /&aterAcce++" Ra0lAcce++"L /Ra0lAcce++" 8arN0ng"Street /8arN0ng" VClearance"2? /VClearance" /&arehou+e"#;;( comm0t(

PHP code $o u!da$e $he number o5 a'a#lable *arehouse doc+s #s( Script *'" xmlinsert%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;(

2%7

/#ing +9, with Orac&e and PHP ,+ - oc0Gpar+e:,c% #+elect <mltype.getclo1/al:warehou+eG+pec; from <warehou+e+ where warehou+eG0d - :0d#;( ,0d - 2( ,r - oc0G10ndG1yGname:,+% #:0d#% ,0d;( oc0Ge<ecute:,+;( ,row - oc0GfetchGarray:,+% BC9GLUO;( // Oan0pulate the data u+0ng S0mpleIOK ,+< - +0mple<mlGloadG+tr0ng::10nary;,row3?4-"load:;;( ,row3?4-"free:;( ,+<-"DocN+ -- 2( // chan,e the data

// 9n+ert change+ u+0ng a temporary CKBB ,+ - oc0Gpar+e:,c% #update <warehou+e+ +et warehou+eG+pec - IOKType::clo1; where warehou+eG0d - :0d#;( oc0G10ndG1yGname:,+% #:0d#% ,0d;( ,lo1 - oc0GnewGde+cr0ptor:,c% BC9GDGKBB;( oc0G10ndG1yGname:,+% #:clo1#% ,lo1% -2% BC9GBGCKBB;( ,lo1-"wr0teTemporary:,+<-"a+Iml:;;( oc0Ge<ecute:,+;( ,lo1-"clo+e:;( !"

The ,+<-"a+Iml:; me$hod con'er$s $he 3#m!leNM. ob8ec$ $o $he $e/$ re!resen$a$#on used $o u!da$e $he $able. & $em!orar" .O- #s crea$ed $o !ass $he ne* NM. 'alue $o $he da$abase. &5$er runn#ng $he PHP scr#!$, =uer"#ng $he record sho*s $he number o5 doc+s has been decremen$ed 5rom 2 $o 1(
SUK" +et long 2??? page+0Qe 2?? SUK" +elect warehou+eG+pec from <warehou+e+( &ARE$BUSEGS8EC --------------------------------------------------!<ml /er+0on-.2.?.!" &arehou+e" &arehou+e9d"2 /&arehou+e9d" &arehou+eLame"SouthlaNe% Te<a+ /&arehou+eLame" Bu0ld0ng"Bwned /Bu0ld0ng" Area"76??? /Area" DocN+"2 /DocN+" DocNType"Rear load /DocNType" &aterAcce++"true /&aterAcce++" Ra0lAcce++"L /Ra0lAcce++" 8arN0ng"Street /8arN0ng" VClearance"2? /VClearance" /&arehou+e"

3ee >sing =63 in S@3 Statements #n Oracle Database S@3 Re$erence 5or more d#scuss#on o5 NM.T"!e.

2%

:etching an +9,-y"e .ro7 a P,;S1, :unction

=etchin" an 78LT:pe 2ro4 a PL5S3L =unction


The WETCKBBVAK:; 5unc$#on #s also use5ul *hen $r"#ng $o ge$ an NM.T"!e 5rom a s$ored 5unc$#on. 2#le xml$unc%sAl crea$es a s#m!le 5unc$#on re$urn#ng =uer" da$a 5or a g#'en #d 'alue. Script *0" xml$unc%sAl
drop ta1le myta1( create ta1le myta1 :0d num1er% data <mltype;( 0n+ert 0nto myta1 :0d% data; /alue+ :2% # +ometh0ng"mydata /+ometh0ng"#;( create or replace funct0on myf:pG0d num1er; return <mltype a+ loc <mltype( 1eg0n +elect data 0nto loc from myta1 where 0d - pG0d( return loc( end( /

To access $h#s 5unc$#on #n PHP, 5#rs$ crea$e a *ra!!er 5unc$#on $ha$ ma!s $he NM. da$a $o a .O-( Script *," xml$unc&rapper%sAl
create or replace funct0on myfGwrapper:pG0d num1er; return clo1 a+ 1eg0n return myf:pG0d;.getclo1/al:;( end( /

Th#s can be called #n PHP b" b#nd#ng a .O- descr#!$or $o $he re$urn 'alue o5 $he 5unc$#on. O >8 .Ome$hods l#+e load:; can be used on $he descr#!$or( Script *4" xml$unc%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( ,1d - oc0GnewGde+cr0ptor:,c% BC9GDGKBB;( ,+ - oc0Gpar+e:,c% .1eg0n :1/ :- myfGwrapper:2;( end(.;( oc0G10ndG1yGname:,+% .:1/.% ,1d% -2% BC9GBGCKBB;( oc0Ge<ecute:,+;( echo htmlent0t0e+:,1d-"load:;;( ,1d-"clo+e:;( !" // 8r0nt output

The ou$!u$ #s $he e/!ec$ed(


+ometh0ng"mydata /+ometh0ng"

2%$

/#ing +9, with Orac&e and PHP

73uer: 78L 3uer: Lan"ua"e


OracleLs su!!or$ 5or NKuer" *as #n$roduced #n Oracle Da$abase 10g Release 2. Un5or$una$el", $o +ee! $he 5oo$!r#n$ o5 Oracle Da$abase N@ small, NKuer" #s no$ a'a#lable #n $he Oracle Da$abase N@ release, bu$ #$ #s #n $he o$her ed#$#ons o5 Oracle. & bas#c NKuer" $o re$urn $he records #n $he employees $able #s(
for ,0 0n ora:/0ew:.employee+.; return ,0

Th#s NKuer" s"n$a/ #s embedded #n a s!ec#al SEKECT(


+elect columnG/alue from <mlta1le:#for ,0 0n ora:/0ew:.employee+.; return ,0#;

The d#55eren$ =uo$#ng s$"les used b" 3K. and NKuer" need care5ul a$$en$#on #n PHP. >$ can be coded( Script *-" xAuery%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/orcl#;( ,<F - #for ,0 0n ora:/0ew:.employee+.; return ,0#( ,Fuery - #+elect columnG/alue from <mlta1le:E##.,<F.#E#;#( ,+ - oc0Gpar+e:,c% ,Fuery;( oc0Ge<ecute:,+;( wh0le :,row - oc0GfetchGarray:,+% BC9GLUO;; @ foreach :,row a+ ,0tem; @ echo htmlent0t0e+:,0tem;.. .( A A !"

The =uer" could also be #n a s#ngle PHP $EREDBC *#$h NKuer" 'ar#ables esca!ed. 7o$e $here canno$ be *h#$es!ace on $he las$ l#ne be5ore $he $o+en ELD(
,Fuery ELD +elect columnG/alue from <mlta1le:#for E,0 0n ora:/0ew:.employee+.; return E,0#; ELD(

>n PHP 5.P, use $he LB&DBC s"n$a/ so $he NKuer" 'ar#able ,0 does no$ need esca!#ng. 7o$e $he s#ngle =uo$es around $he *ord ELD $o d#55eren$#a$e #$ 5rom a $EREDBC.
,Fuery #ELD# +elect columnG/alue from <mlta1le:#for ,0 0n ora:/0ew:.employee+.; return ,0#; ELD(

Table ro*s are au$oma$#call" *ra!!ed #n $ags and re$urned(


RB&" EO8KBJEEG9D"2?? /EO8KBJEEG9D" *9RSTGLAOE"Ste/en /*9RSTGLAOE" KASTGLAOE"C0ng /KASTGLAOE"

21%

+1uery +9, 1uery ,anguage EOA9K"SC9LW /EOA9K" 8$BLEGLUOBER"626.27).56ST /8$BLEGLUOBER" $9REGDATE"2>MT-?S-2T /$9REGDATE" YBBG9D"ADG8RES /YBBG9D" SAKARJ"75??? /SAKARJ" DE8ARTOELTG9D">? /DE8ARTOELTG9D" b /RB&" RB&" EO8KBJEEG9D"7?S /EO8KBJEEG9D" *9RSTGLAOE"&0ll0am /*9RSTGLAOE" KASTGLAOE"W0etQ /KASTGLAOE" EOA9K"&W9ET] /EOA9K" 8$BLEGLUOBER"626.27).M2M2 /8$BLEGLUOBER" $9REGDATE"2>>5-?S-?T /$9REGDATE" YBBG9D"ACGACCBULT /YBBG9D" SAKARJ"M)?? /SAKARJ" OALAWERG9D"7?6 /OALAWERG9D" DE8ARTOELTG9D"22? /DE8ARTOELTG9D"

/RB&"

Hou can also use RETURL9LW CBLTELT $o re$urn a s#ngle documen$ node(
,Fuery ELD +elect <mlFuery:#for E,0 0n ora:/0ew:.hr.% .locat0on+.;/RB& return E,0/C9TJ# return0ng content; from dual ELD(

2or bo$h IOKTABKE:; and IOKUUERJ:; "ou m#gh$ *an$ $o use $he IOKTJ8E.WETCKBBVAK:; 5unc$#on $o a'o#d s$r#ng s#1e l#m#$ #ssues(
,Fuery ELD +elect <mltype.getclo1/al:columnG/alue; from <mlta1le:#for E,0 0n ora:/0ew:.employee+.; return E,0#; ELD(

and
,Fuery ELD +elect <mltype.getclo1/al:<mlFuery:#for E,0 0n ora:/0ew:.hr.% .locat0on+.;/RB& return E,0/C9TJ# return0ng content;; from dual ELD(

The re$urned column $"!e #s a .O- loca$or and so $he 5e$ch uses .O- me$hods, such as load:;(
,+ - oc0Gpar+e:,c% ,Fuery;( oc0Ge<ecute:,+;( wh0le :,row - oc0GfetchGarray:,+% BC9GLUO;; @ /arGdump:,row3?4-"load:;;( ,row3?4-"free:;( A

211

/#ing +9, with Orac&e and PHP

Acce''in" Data o.er HTTP 9ith 78L D<


NM. D- allo*s "ou $o access da$a d#rec$l" '#a HTTP, 2TP or 0ebD&,. The Oracle 7e$*or+ l#s$ener *#ll handle all $hese re=ues$s. &s an e/am!le o5 HTTP access, use $he P.I3K. DBOSGIDB !ac+age $o crea$e a resource, *h#ch here #s some s#m!le $e/$(
SUK" 7 ) 5 6 S T M > 2? 22 27 2) 25 26 2S declare re+ 1oolean( 1eg0n 1eg0n -- delete 0f 0t already e<0+t+ d1m+G<d1.deleteRe+ource:#/pu1l0c/te+t2.t<t#;( e<cept0on when other+ then null( end( -- create the f0le re+ :- d1m+G<d1.createRe+ource:#/pu1l0c/te+t2.t<t#% #the te<t to +tore#;( comm0t( -- don_t forget to comm0t end( /

2or $es$#ng, remo'e access con$rol on $he !ubl#c resource(


SUK" connect +y+tem/+y+tempwd SUK" alter u+er anonymou+ 0dent0f0ed 1y anonymou+ account unlocN(

The 5#le can no* be accessed 5rom a bro*ser :or PHP a!!l#ca$#on; us#ng( http://localho+t:M?M?/pu1l0c/te+t2.t<t >5 "ou are access#ng Oracle Da$abase N@ 5rom a remo$e bro*ser, "ou ma" need $o enable remo$e cl#en$ connec$#on as descr#bed #n $he cha!$er )nstalling Oracle Database !2g Express Edition. There #s e/$ens#'e Oracle documen$a$#on on NM. D- on $he Oracle Technolog" ne$*or+ a$ http://otn.oracle.com/tech/<ml/<mld1.

212

CHAPTER 1(

PHP SCALA<ILITY AND HI!H A@AILA<ILITY


Th#s cha!$er d#scusses $*o 5ea$ures su!!or$ed b" PHP O >8 1.P $ha$ #m!ro'e scalab#l#$" and h#gh a'a#lab#l#$"(

Oracle Da$abase 11g Da$abase Res#den$ onnec$#on Pool#ng :DR P; Oracle Da$abase 10g Release 2 or 11g 2as$ &!!l#ca$#on 7o$#5#ca$#on :2&7;

The Oracle 5ea$ures are usable se!ara$el" or $oge$her. Da$abase Res#den$ onnec$#on Pool#ng :DR P; #s a ne* 5ea$ure o5 Oracle Da$abase 11g $ha$ addresses scalab#l#$" re=u#remen$s #n en'#ronmen$s re=u#r#ng large numbers o5 connec$#ons *#$h m#n#mal da$abase resource usage. l#en$s $ha$ run #n h#gh a'a#lab#l#$" con5#gura$#ons such as *#$h Oracle Real &!!l#ca$#on lus$ers :R& ; or Da$a Auard Ph"s#cal 3$and?-" can $a+e ad'an$age o5 2as$ &!!l#ca$#on 7o$#5#ca$#on :2&7; e'en$s #n PHP $o allo* a!!l#ca$#ons $o res!ond =u#c+l" $o da$abase node 5a#lures.

Data)a'e Re'ident Connection Poo%in"


Oracle Da$abase 11g DR P addresses scalab#l#$" re=u#remen$s #n en'#ronmen$s re=u#r#ng large numbers o5 connec$#ons *#$h m#n#mal da$abase resource usage. DR P !ools a se$ o5 ded#ca$ed da$abase ser'er !rocesses :+no*n as pooled servers;, *h#ch can be shared across mul$#!le a!!l#ca$#ons runn#ng on $he same or se'eral hos$s. & connec$#on bro+er !rocess manages $he !ooled ser'ers a$ $he da$abase #ns$ance le'el. 0#$hou$ DR P, each PHP !rocess crea$es and des$ro"s da$abase ser'ers *hen connec$#ons are o!ened and closed. Th#s can be e/!ens#'e and cr#!!l#ng $o h#gh scalab#l#$". Or al$erna$#'el", each !rocess +ee!s connec$#ons o!en :M!ers#s$en$ connec$#onsO; e'en *hen $he" are no$ !rocess#ng an" user scr#!$s. Th#s remo'es connec$#on crea$#on and des$ruc$#on cos$s bu$ #ncurs unnecessar" memor" o'erhead #n $he da$abase, as sho*n #n 2#gure DP.

213

PHP Sca&a*i&ity and High 0vai&a*i&ity

igure ?'" /ithout DR(P7 idle persistent connections $rom PHP still consume database resources%

Ho9 DRCP Wor>'


The arch#$ec$ure o5 DR P #s sho*n #n 2#gure DE. & connec$#on bro+er acce!$s #ncom#ng connec$#on re=ues$s 5rom PHP !rocesses and ass#gns each a 5ree ser'er #n $he !ool. @ach PHP !rocess $ha$ #s e/ecu$#ng a PHP scr#!$ commun#ca$es *#$h $h#s Oracle ser'er un$#l $he connec$#on #s released. Th#s release can be e/!l#c#$ *#$h oc0Gclo+e:; or #$ *#ll ha!!en au$oma$#call" a$ $he end o5 $he scr#!$. 0hen $he connec$#on #s released, $he ser'er !rocess #s re$urned $o $he !ool and $he PHP !rocess +ee!s a l#n+ onl" $o $he connec$#on bro+er. &c$#'e !ooled ser'ers con$a#n $he Process Alobal &rea :PA&; and $he user sess#on da$a. >dle ser'ers #n $he !ool re$a#n $he user sess#on 5or reuse b" subse=uen$ !ers#s$en$ PHP connec$#ons. 0hen $he number o5 !ers#s$en$ connec$#ons #s less $han $he number o5 !ooled ser'ers, a Mded#ca$ed o!$#m#1a$#onO a'o#ds unnecessar#l" re$urn#ng ser'ers $o $he !ool *hen a PHP connec$#on #s closed. >ns$ead, $he ded#ca$ed assoc#a$#on be$*een $he PHP !rocess and $he ser'er #s +e!$ #n an$#c#!a$#on $ha$ $he PHP !rocess *#ll =u#c+l" become ac$#'e aga#n. >5 PHP scr#!$s are e/ecu$ed b" numerous *eb ser'ers, $he DR P !ool can gro* $o #$s ma/#mum s#1e :albe#$ $"!#call" a rela$#'el" small s#1e;, e'en #5 $he ra$e o5 #ncom#ng user re=ues$s #s lo*. @ach PHP !rocess, e#$her bus" or no* #dle, *#ll be a$$ached $o #$s o*n !ooled ser'er. 0hen $he !ool reaches #$s ma/#mum s#1e, ano$her PHP !rocess $ha$ needs a !ooled ser'er *#ll $a+e o'er one o5 $he #dle ser'ers.

214

)ata*a#e !e#ident (onnection Poo&ing

igure ?0" DR(P Architecture% The !ool s#1e and number o5 connec$#on bro+ers are con5#gurable. There #s al*a"s a$ leas$ one connec$#on bro+er !er da$abase #ns$ance *hen DR P #s enabled. &lso, a$ an" $#me, around 5Z o5 $he curren$ !ooled ser'ers are reser'ed 5or au$hen$#ca$#ng ne* PHP connec$#ons. &u$hen$#ca$#on #s !er5ormed *hen a PHP !rocess es$abl#shes a connec$#on $o $he connec$#on bro+er. DR P boos$s $he scalab#l#$" o5 $he da$abase and $he *eb ser'er $#er because connec$#ons $o $he da$abase are held a$ m#n#mal cos$. Da$abase memor" #s onl" used b" $he !ooled ser'ers, and scal#ng can be e/!l#c#$l" con$rolled b" DR P $un#ng o!$#ons. 0#$h $he #n$roduc$#on o5 !ooled ser'ers used b" DR P, $here are no* $hree $"!es o5 da$abase ser'er !rocess models $ha$ Oracle a!!l#ca$#ons can use( ded#ca$ed ser'ers, shared ser'ers and !ooled ser'ers.

#able !!" Di$$erences bet&een dedicated servers7 shared servers7 and pooled servers%
Dedicated Ser.er' 0hen $he PHP connec$#on #s crea$ed, a ne$*or+ connec$#on $o a ded#ca$ed ser'er !rocess and assoc#a$ed sess#on are crea$ed. Shared Ser.er' Poo%ed Ser.er' 0hen $he PHP connec$#on #s 0hen $he PHP connec$#on #s crea$ed, crea$ed, a ne$*or+ connec$#on a ne$*or+ connec$#on $o $he $o $he d#s!a$cher !rocess #s connec$#on bro+er #s es$abl#shed. es$abl#shed. & sess#on #s crea$ed #n $he 3A&.

215

PHP Sca&a*i&ity and High 0vai&a*i&ity

Dedicated Ser.er'

Shared Ser.er'

Poo%ed Ser.er' &c$#'#$" on a connec$#on *a+es $he bro+er, *h#ch hands $he ne$*or+ connec$#on $o a !ooled ser'er. The ser'er $hen handles subse=uen$ re=ues$s d#rec$l", 8us$ l#+e a ded#ca$ed ser'er. >dle PHP connec$#ons hold a ser'er !rocess and sess#on resources. los#ng a PHP connec$#on causes $he sess#on $o be des$ro"ed and $he !ooled ser'er $o be released $o $he !ool. & ne$*or+ connec$#on $o $he connec$#on bro+er #s re$a#ned. Memor" usage #s !ro!or$#onal $o $he number o5 !ooled ser'ers and $he#r sess#ons. There #s one sess#on 5or each !ooled ser'er.

&c$#'#$" on a connec$#on #s @ach ac$#on on a connec$#on handled b" $he ded#ca$ed ser'er. goes $hrough $he d#s!a$cher, *h#ch hands $he *or+ $o a shared ser'er.

>dle PHP connec$#ons hold a ser'er !rocess and sess#on resources.

>dle PHP connec$#ons hold sess#on resources bu$ no$ a ser'er !rocess.

los#ng a PHP connec$#on causes los#ng a PHP connec$#on $he sess#on $o be 5reed and $he causes $he sess#on $o be 5reed. ser'er !rocess $o be $erm#na$ed.

Memor" usage #s !ro!or$#onal $o $he number o5 ser'er !rocesses and sess#ons. There #s one ser'er and one sess#on 5or each PHP connec$#on.

Memor" usage #s !ro!or$#onal $o $he sum o5 $he shared ser'ers and sess#ons. There #s one sess#on 5or each PHP connec$#on.

Pooled ser'ers #n use b" PHP are s#m#lar #n beha'#or $o ded#ca$ed ser'ers. &5$er connec$#on, PHP d#rec$l" commun#ca$es *#$h $he !ooled ser'er 5or all da$abase o!era$#ons.

PHP OCI0 Connection' and DRCP


The PHP O >8 e/$ens#on has $hree 5unc$#ons 5or connec$#ng $o a da$abase( oc0Gconnect:;, oc0GnewGconnect:;, and oc0Gpconnect:;. The #m!lemen$a$#on o5 $hese 5unc$#ons *as re*or+ed #n O >8 1.P and all bene5#$ 5rom us#ng DR P. Table 12 com!ares ded#ca$ed and !ooled ser'ers. 3hared ser'ers are no$ sho*n bu$ beha'e s#m#larl" $o ded#ca$ed ser'ers *#$h $he e/ce!$#on $ha$ onl" $he sess#on and no$ $he ser'er #s des$ro"ed *hen a connec$#on #s closed.

216

)ata*a#e !e#ident (onnection Poo&ing

#able !2" Cehavior o$ O()* connection $unctions $or Dedicated and Pooled Servers%
OCI0 =unction oc0Gconnect:; With Dedicated Ser.er' rea$es a PHP connec$#on $o $he da$abase us#ng a ded#ca$ed ser'er. The connec$#on #s cached #n $he PHP !rocess 5or reuse b" subse=uen$ oc0Gconnect:; calls #n $he same scr#!$. &$ $he end o5 $he scr#!$ or *#$h oc0Gclo+e:;% $he connec$#on #s closed and $he ser'er !rocess and sess#on are des$ro"ed. 3#m#lar $o oc0Gconnect:; abo'e, bu$ an #nde!enden$ ne* PHP connec$#on and ser'er !rocess #s crea$ed e'er" $#me $h#s 5unc$#on #s called, e'en *#$h#n $he same scr#!$. &ll PHP connec$#ons and $he da$abase ser'ers are closed *hen $he scr#!$ ends or *#$h oc0Gclo+e:;. 3ess#ons are des$ro"ed a$ $ha$ $#me. rea$es a !ers#s$en$ PHP connec$#on *h#ch #s cached #n $he PHP !rocess. The connec$#on #s no$ closed a$ $he end o5 $he scr#!$, and $he ser'er and sess#on are a'a#lable 5or reuse b" an" subse=uen$ oc0Gpconnect:; call !ass#ng $he same creden$#als. With Poo%ed Ser.er' Ae$s a !ooled ser'er 5rom $he DR P !ool and crea$es a brand ne* sess#on. 3ubse=uen$ oc0Gconnect:; calls #n $he same scr#!$ use $he same connec$#on. 0hen $he scr#!$ com!le$es, or oc0Gclo+e:; #s called, $he sess#on #s des$ro"ed and $he !ooled ser'er #s a'a#lable 5or o$her PHP connec$#ons $o use. 3#m#lar $o oc0Gconnect:; abo'e, bu$ an #nde!enden$ ser'er #n $he !ool #s used and a ne* sess#on #s crea$ed each $#me $h#s 5unc$#on #s called #n $he same scr#!$. &ll sess#ons are des$ro"ed a$ $he end o5 $he scr#!$ or *#$h oc0Gclo+e:;. The !ooled ser'ers are made a'a#lable 5or o$her connec$#ons $o use. rea$es a !ers#s$en$ PHP connec$#on. all#ng oc0Gclo+e:; releases $he connec$#on and re$urns $he ser'er *#$h #$s sess#on #n$ac$ $o $he !ool 5or reuse b" o$her PHP !rocesses. >5 oc0Gclo+e:; #s no$ called, $hen $h#s connec$#on release ha!!ens a$ $he end o5 $he scr#!$. 3ubse=uen$ calls $o oc0Gpconnect:; reuse $he e/#s$#ng ne$*or+ connec$#on $o =u#c+l" ge$ a ser'er and sess#on 5rom $he !ool.

oc0GnewGconnect:;

oc0Gpconnect:;

0#$h DR P, all $hree connec$#on 5unc$#ons sa'e on $he cos$ o5 au$hen$#ca$#on and bene5#$ 5rom $he ne$*or+ connec$#on $o $he connec$#on bro+er be#ng ma#n$a#ned, e'en 5or connec$#ons $ha$ are MclosedO 5rom PHPLs !o#n$ o5 '#e*. The" also bene5#$ 5rom ha'#ng !re?s!a*ned ser'er !rocesses #n $he DR P !ool. The oc0Gpconnect:; 5unc$#on reuses sess#ons, allo*#ng e'en grea$er scalab#l#$". The non?!ers#s$en$ connec$#on 5unc$#ons crea$e and des$ro" ne* sess#ons each $#me $he" are used, allo*#ng less shar#ng a$ $he cos$ o5 reduced !er5ormance.

217

PHP Sca&a*i&ity and High 0vai&a*i&ity

O'erall, a5$er a br#e5 *arm?u! !er#od 5or $he !ool, DR P allo*s reduced connec$#on $#mes #n add#$#on $o $he reuse bene5#$s o5 !ool#ng.

When to u'e DRCP


DR P #s $"!#call" !re5erred 5or a!!l#ca$#ons *#$h a large number o5 connec$#ons. 3hared ser'ers are use5ul 5or a med#um number o5 connec$#ons and ded#ca$ed sess#ons are !re5erred 5or small numbers o5 connec$#ons. The $hreshold s#1es are rela$#'e $o $he amoun$ o5 memor" a'a#lable on $he da$abase hos$. DR P can be use5ul *hen an" o5 $he 5ollo*#ng a!!l"(

& large number o5 connec$#ons need $o be su!!or$ed *#$h m#n#mum memor" usage on $he da$abase hos$. PHP a!!l#ca$#ons mos$l" use $he same da$abase creden$#als 5or all connec$#ons. The a!!l#ca$#ons ac=u#re a da$abase connec$#on, *or+ on #$ 5or a rela$#'el" shor$ dura$#on, and $hen release #$. There are mul$#!le *eb ser'ers and *eb ser'er hos$s. onnec$#ons loo+ #den$#cal #n $erms o5 sess#on se$$#ngs, 5or e/am!le da$e 5orma$ se$$#ngs and P.I3K. !ac+age s$a$e. >$ enables resource shar#ng among mul$#!le m#ddle?$#er cl#en$ a!!l#ca$#ons. >$ #m!ro'es scalab#l#$" o5 da$abases and a!!l#ca$#ons b" reduc#ng resource usage.

DR P !ro'#des $he 5ollo*#ng ad'an$ages(


2or !ers#s$en$ PHP connec$#ons, normal ded#ca$ed ser'ers can be 5as$es$. There #s no bro+er or d#s!a$cher o'erhead. The ser'er #s al*a"s connec$ed and a'a#lable *hene'er $he PHP !rocess needs #$. -u$ as $he number o5 connec$#ons #ncreases, $he memor" cos$ o5 +ee!#ng connec$#ons o!en =u#c+l" reduces e55#c#enc" o5 $he da$abase s"s$em. 2or non?!ers#s$en$ PHP connec$#ons, DR P can be 5as$es$ because $he use o5 !ooled ser'er !rocesses remo'es $he need 5or PHP connec$#ons $o crea$e and des$ro" !rocesses, and remo'es $he need $o re? au$hen$#ca$e 5or each connec$ call. ons#der an a!!l#ca$#on #n *h#ch $he memor" re=u#red 5or each sess#on #s E00 6-. On a P2 b#$ o!era$#ng s"s$em $he memor" re=u#red 5or each ser'er !rocess could be E M-, and DR P could use P5 6- !er connec$#on :mos$l" #n $he connec$#on bro+er;. >5 $he number o5 !ooled ser'ers #s con5#gured a$ 100, $he number o5 shared ser'ers #s con5#gured a$ 100, and $he de!lo"ed a!!l#ca$#on crea$es 5000 PHP connec$#ons, $hen $he memor" used b" each $"!e o5 ser'er #s es$#ma$ed #n Table 1P.

#able !'" Example database host memory use $or dedicated7 shared and pooled servers%
Dedicated Ser.er' Da$abase 3er'er Memor" 5000 T E MShared Ser.er' 100 T E MPoo%ed Ser.er' 100 T E M-

21

)ata*a#e !e#ident (onnection Poo&ing

Dedicated Ser.er' 3ess#on Memor" 5000 T E00 6-

Shared Ser.er' 5000 T E00 67o$e( 2or 3hared 3er'ers, sess#on memor" #s alloca$ed 5rom $he 3A&.

Poo%ed Ser.er' 100 T E00 6-

DR P onnec$#on -ro+er O'erhead

5000 T P5 6-

To$al Memor"

21 A-

2.P A-

F10 M-

There #s a s#gn#5#can$ memor" sa'#ng *hen us#ng DR P. @'en #5 su55#c#en$ memor" #s a'a#lable $o run #n ded#ca$ed mode, DR P can s$#ll be a '#able o!$#on #5 $he PHP a!!l#ca$#on needs da$abase connec$#ons 5or onl" shor$ !er#ods o5 $#me. >n $h#s case $he memor" sa'ed b" us#ng DR P can be used $o*ards #ncreas#ng $he 3A&, $hereb" #m!ro'#ng o'erall !er5ormance. Pool#ng #s a'a#lable *hen connec$#ng o'er T PI>P *#$h user#dI!ass*ord based da$abase au$hen$#ca$#on. >$ #s no$ a'a#lable us#ng be=uea$h connec$#ons. 0#$h Oracle 11.1, $he cl#en$ resul$ cache 5ea$ure does no$ *or+ *#$h DR P.

Sharin" the Ser.er Poo%


DR P guaran$ees $ha$ !ooled ser'ers and sess#ons #n#$#all" used b" one da$abase user are onl" e'er reusable b" connec$#ons *#$h $ha$ same user#d. DR P also 5ur$her !ar$#$#ons $he !ool #n$o log#cal grou!s or Mconnec$#on classesO. & connec$#on class #s a user chosen name se$ #n $he php%ini con5#gura$#on 5#le. 3ess#on?s!ec#5#c a$$r#bu$es, l#+e $he da$e 5orma$ or an e/!l#c#$ role, ma" be re?usable b" an" connec$#on #n a !ar$#cular a!!l#ca$#on. 3ubse=uen$ !ers#s$en$ connec$#ons *#ll reuse $he sess#on and #nher#$ $hose se$$#ngs #5 $he username and connec$#on class are $he same as $he !re'#ous connec$#on. &!!l#ca$#ons $ha$ need d#55eren$ s$a$e #n $he sess#on memor" should use d#55eren$ usernames andIor connec$#on classes. 2or e/am!le, a!!l#ca$#ons #n a su#$e called RPT ma" be *#ll#ng $o share !ooled ser'ers be$*een $hemsel'es bu$ no$ *#$h an a!!l#ca$#on su#$e called HR. The d#55eren$ connec$#on classes and resul$#ng log#cal !ar$#$#on#ng o5 $he DR P ser'er !ool #s sho*n #n 2#gure D5. onnec$#ons *#$h $he same username and connec$#on class 5rom an" hos$ *#ll share $he same sub?!ool o5 ser'ers.

21$

PHP Sca&a*i&ity and High 0vai&a*i&ity

igure ?," #he DR(P pool is logically partitioned by username and connection class% >5 $here are no 5ree !ooled ser'ers ma$ch#ng a re=ues$ 5or a user#d #n $he s!ec#5#ed connec$#on class, and #5 $he !ool #s alread" a$ #$s ma/#mum s#1e, $hen an #dle ser'er #n $he !ool *#ll be used and a ne* sess#on crea$ed 5or #$. >5 $he ser'er or#g#nall" belonged $o a d#55eren$ connec$#on class, $he ser'er *#ll m#gra$e $o $he ne* class. >5 $here are no !ooled ser'ers a'a#lable, $he connec$#on re=ues$ *a#$s 5or one $o become a'a#lable. Th#s allo*s $he da$abase $o con$#nue *#$hou$ becom#ng o'erloaded. The connec$#on class should be se$ $o $he same 'alue 5or each #ns$ance o5 PHP runn#ng $he same a!!l#ca$#on *here shar#ng o5 !ooled connec$#ons #s des#red. >5 no connec$#on class #s s!ec#5#ed, each *eb ser'er !rocess *#ll ha'e a un#=ue, s"s$em genera$ed class name, l#m#$#ng shar#ng o5 connec$#ons $o each !rocess. >5 DR P #s used bu$ sess#on shar#ng #s no$ des#rable under an" cond#$#on, use oc0Gconnect:; or oc0GnewGconnect:; *h#ch recrea$e $he sess#on each $#me. &l$hough sess#on da$a ma" be reused b" subse=uen$ !ers#s$en$ connec$#ons, $ransac$#ons do no$ s!an connec$#ons across scr#!$s. Uncomm#$$ed da$a *#ll be rolled bac+ a$ $he end o5 a PHP scr#!$.

,'in" DRCP in PHP


Us#ng DR P *#$h PHP a!!l#ca$#ons #n'ol'es $he 5ollo*#ng s$e!s( 1. 2. P.
22%

on5#gur#ng and enabl#ng $he !ool. on5#gur#ng PHP. De!lo"#ng $he a!!l#ca$#on.

/#ing )!(P in PHP

PHP a!!l#ca$#ons de!lo"ed as &!ache modules, 2as$ A>, A> and s$andalone a!!l#ca$#ons can bene5#$ 5rom DR P. PHP a!!l#ca$#ons de!lo"ed as &!ache modules or *#$h 2as$ A> ga#n mos$, s#nce $he" rema#n connec$ed $o $he connec$#on bro+er o'er mul$#!le scr#!$ e/ecu$#ons and can $a+e ad'an$age o5 o$her o!$#m#1a$#ons, such as s$a$emen$ cach#ng.

Con2i"urin" and Ena)%in" the Poo%


@'er" #ns$ance o5 Oracle Da$abase 11g uses a s#ngle, de5aul$ connec$#on !ool. User de5#ned !ools are curren$l" no$ su!!or$ed. The de5aul$ !ool can be con5#gured and adm#n#s$ered b" a D-& us#ng $he DBOSGCBLLECT9BLG8BBK !ac+age(
SUK" e<ecute d1m+Gconnect0onGpool.conf0gureGpool: poolGname -" #SJSGDE*AUKTGCBLLECT9BLG8BBK#% m0n+0Qe -" 5% ma<+0Qe -" 5?% 0ncr+0Qe -" 7% +e++0onGcachedGcur+or+ -" 7?% 0nact0/0tyGt0meout -" )??% ma<Gth0nNGt0me -" S??% ma<Gu+eG+e++0on -" 6?????% ma<Gl0fet0meG+e++0on -" MS5??;(

&l$erna$#'el" $he me$hod d1m+Gconnect0onGpool.alterGparam:; can be used $o se$ a s#ngle !arame$er(


SUK" e<ecute d1m+Gconnect0onGpool.alterGparam: poolGname -" #SJSGDE*AUKTGCBLLECT9BLG8BBK#% paramGname -" #OAIGT$9LCGT9OE#% paramG/alue -" #27??#;(

There #s a d1m+Gconnect0onGpool.re+toreGdefault+:; !rocedure $o rese$ all 'alues. 0hen DR P #s used *#$h R& , each da$abase #ns$ance has #$s o*n connec$#on bro+er and !ool o5 ser'ers. @ach !ool has $he #den$#cal con5#gura$#on. 2or e/am!le all !ools *#ll ha'e ma<+0Qe ser'er !rocesses. & s#ngle d1m+Gconnect0onGpool command *#ll al$er $he !ool o5 each #ns$ance a$ $he same $#me. The !ool needs $o be s$ar$ed be5ore connec$#on re=ues$s beg#n. The command belo* does $h#s b" br#ng#ng u! $he bro+er, *h#ch reg#s$ers #$sel5 *#$h $he da$abase l#s$ener(
SUK" e<ecute d1m+Gconnect0onGpool.+tartGpool:;(

Once enabled $h#s *a", $he !ool au$oma$#call" res$ar$s *hen $he #ns$ance res$ar$s, unless e/!l#c#$l" s$o!!ed *#$h $he d1m+Gconnect0onGpool.+topGpool:; command(
SUK" e<ecute d1m+Gconnect0onGpool.+topGpool:;(

The DR P con5#gura$#on o!$#ons are descr#bed #n Table 11.

221

PHP Sca&a*i&ity and High 0vai&a*i&ity

#able !0" DR(P (on$iguration Options%


DRCP Option
poolGname

De'cription The !ool $o be con5#gured. urren$l" $he onl" su!!or$ed name #s $he de5aul$ 'alue SJSGDE*AUKTGCBLLECT9BLG8BBK. M#n#mum number o5 !ooled ser'ers #n $he !ool. The de5aul$ #s E. Ma/#mum number o5 !ooled ser'ers #n $he !ool. >5 $h#s l#m#$ #s reached and all $he !ooled ser'ers are bus", $hen connec$#on re=ues$s *a#$ un$#l a ser'er becomes 5ree. The de5aul$ #s E0. The number o5 !ooled ser'ers #s #ncreased b" $h#s 'alue *hen ser'ers are una'a#lable 5or PHP connec$#ons and #5 $he !ool #s no$ "e$ a$ #$s ma/#mum s#1e. The de5aul$ #s 2. >nd#ca$es $o $urn on SESS9BLGCAC$EDGCURSBRS 5or all connec$#ons #n $he !ool. Th#s 'alue #s $"!#call" se$ $o $he s#1e o5 $he *or+#ng se$ o5 5re=uen$l" used s$a$emen$s. The cache uses cursor resources on $he ser'er. The de5aul$ #s 20. 7o$e( $here #s also an init%ora !arame$er 5or se$$#ng $he 'alue 5or $he *hole da$abase #ns$ance. The !ool o!$#on allo*s a DR P?based a!!l#ca$#on $o o'err#de $he #ns$ance se$$#ng. T#me $o l#'e 5or an #dle ser'er #n $he !ool. >5 a ser'er rema#ns #dle #n $he !ool 5or $h#s $#me, #$ #s +#lled. Th#s !arame$er hel!s $o shr#n+ $he !ool *hen #$ #s no$ used $o #$s ma/#mum ca!ac#$". The de5aul$ #s P00 seconds. Ma/#mum $#me o5 #nac$#'#$" $he PHP scr#!$ #s allo*ed a5$er connec$#ng. >5 $he scr#!$ does no$ #ssue a da$abase call 5or $h#s amoun$ o5 $#me, $he !ooled ser'er ma" be re$urned $o $he !ool 5or reuse. The PHP scr#!$ *#ll ge$ an OR& error #5 #$ la$er $r#es $o use $he connec$#on. The de5aul$ #s 120 seconds. Ma/#mum number o5 $#mes a ser'er can be $a+en and released $o $he !ool be5ore #$ #s 5lagged 5or res$ar$#ng. The de5aul$ #s 500000. T#me $o l#'e 5or a !ooled ser'er be5ore #$ #s res$ar$ed. The de5aul$ #s 8FE00 seconds. The number o5 connec$#on bro+ers $ha$ are crea$ed $o handle connec$#on re=ues$s. 7o$e( $h#s can onl" be se$ *#$h alterGparam:;. The de5aul$ #s 1. The ma/#mum number o5 connec$#ons $ha$ each connec$#on bro+er can handle. 3e$ $he !er?!rocess 5#le descr#!$or l#m#$ o5 $he o!era$#ng s"s$em su55#c#en$l" h#gh so $ha$ #$ su!!or$s $he number o5 connec$#ons s!ec#5#ed. 7o$e( $h#s can onl" be se$ *#$h alterGparam:;. The de5aul$ #s E0000.

m0n+0Qe ma<+0Qe

0ncr+0Qe

+e++0onGcachedGcur+or+

0nact0/0tyGt0meout

ma<Gth0nNGt0me

ma<Gu+eG+e++0on

ma<Gl0fet0meG+e++0on

numGc1roN

ma<connGc1roN

222

/#ing )!(P in PHP

Note: The parameters have been described here relative to their use in #)# but it is worth rememberin& that the ,/C# pool is usable concurrently by other pro&rams.

>n general, #5 !ool !arame$ers are changed, $he !ool should be res$ar$ed, o$her*#se ser'er !rocesses *#ll con$#nue $o use old se$$#ngs. The 0nact0/0tyGt0meout se$$#ng $erm#na$es #dle !ooled ser'ers, hel!#ng o!$#m#1e da$abase resources. To a'o#d !ooled ser'ers !ermanen$l" be#ng held on$o b" a dead *eb ser'er !rocess or a sel5#sh PHP scr#!$, $he ma<Gth0nNGt0me !arame$er can be se$. The !arame$ers numGc1roN and ma<connGc1roN can be used $o d#s$r#bu$e $he !ers#s$en$ connec$#ons 5rom $he cl#en$s across mul$#!le bro+ers. Th#s ma" be needed #n cases *here $he o!era$#ng s"s$em !er?!rocess descr#!$or l#m#$ #s small. The ma<Gu+eG+e++0on and ma<Gl0fet0meG+e++0on !arame$ers hel! !ro$ec$ aga#ns$ an" un5oreseen !roblems a55ec$#ng ser'er !rocesses. The de5aul$ 'alues *#ll be su#$able 5or mos$ users. Users o5 Oracle Da$abase 11.1.0.F mus$ a!!l" $he da$abase !a$ch 5or bug FECEEE1. Th#s #s necessar" *#$h O >8 1.P $o a'o#d =uer" errors. >$ also enables KBWBL $r#gger su!!or$. Th#s !a$ch #s no$ needed *#$h 11.1.0.C on*ards.

Con2i"urin" PHP 2or DRCP


PHP mus$ be bu#l$ *#$h $he DR P?enabled O >8 e/$ens#on. Do*nload O >8 1.P 5rom P@ ., e/$rac$ #$ and use #$ $o re!lace $he e/#s$#ng ext<oci* d#rec$or" #n PHP. on5#gure, bu#ld and #ns$all PHP as normal. &l$erna$#'el" use $he pecl 0n+tall command $o bu#ld and #ns$all $he O >8 as a shared module. PHP 5.P and PHP F, ne#$her o5 *h#ch has been released, con$a#n $he ne* e/$ens#on b" de5aul$. The O >8 1.P e/$ens#on can be used *#$h Oracle cl#en$ l#brar#es 'ers#on D.2 and h#gher, ho*e'er DR P 5unc$#onal#$" #s onl" a'a#lable *hen PHP #s l#n+ed *#$h Oracle Da$abase 11g cl#en$ l#brar#es and connec$ed $o Oracle Da$abase 11g. Once #ns$alled, use PHPLs php0nfo:; 5unc$#on $o 'er#5" $ha$ O >8 1.P has been loaded. -e5ore us#ng DR P, $he ne* php%ini !arame$er oc0M.connect0onGcla++ should be se$ $o s!ec#5" $he connec$#on class used b" all $he re=ues$s 5or !ooled ser'ers b" $he PHP a!!l#ca$#on.
oc0M.connect0onGcla++ - OJ8$8A88

The !arame$er can be se$ #n php%ini, %htaccess or httpd%con$ 5#les. >$ can also be se$ and re$r#e'ed !rogramma$#call" us#ng $he PHP 5unc$#ons 0n0G+et:; and 0n0Gget:;. The O >8 e/$ens#on has se'eral legac" php%ini con5#gura$#on !arame$ers 5or $un#ng !ers#s$en$ connec$#ons. These *ere ma#nl" used $o l#m#$ #dle resource usage. 0#$h DR P, $he !arame$ers s$#ll ha'e an e55ec$ bu$ #$ ma" be eas#er $o use $he DR P !ool con5#gura$#on o!$#ons.

#able !," Existing php%ini parameters $or persistent connections%


Php.ini Para4eter oc0M.per+0+tentGt0meout <eha.ior 9ith DRCP &$ $he $#meou$ o5 an #dle PHP connec$#on, PHP *#ll close $he Oracle connec$#on $o $he bro+er. The de5aul$ #s no $#meou$.

223

PHP Sca&a*i&ity and High 0vai&a*i&ity

Php.ini Para4eter oc0M.ma<Gper+0+tent

<eha.ior 9ith DRCP The ma/#mum number o5 un#=ue !ers#s$en$ connec$#ons $ha$ PHP *#ll ma#n$a#n $o $he bro+er. 0hen $he l#m#$ #s reached a ne* !ers#s$en$ connec$#on beha'es l#+e oc0Gconnect:; and releases $he connec$#on a$ $he end o5 $he scr#!$. The de5aul$ #s no l#m#$. 7o$e( The DR P ma<+0Qe se$$#ng *#ll s$#ll be en5orced b" $he da$abase #nde!enden$l" 5rom oc0M.ma<Gper+0+tent. The e/#s$#ng recommenda$#on $o se$ #$ $o -2 $o d#sable !#ng#ng and $o use a!!ro!r#a$e error chec+#ng s$#ll holds $rue *#$h DR P. &lso, $he use o5 2&7 :see la$er; reduces $he chance o5 #dle connec$#ons becom#ng unusable. The de5aul$ #s F0 seconds.

oc0M.p0ngG0nter/al

0#$h O >8 1.P, oc0M.p0ngG0nter/al #s also used 5or non?!ers#s$en$ connec$#ons *hen !ooled ser'ers are used. Th#s hel!s !ro$ec$ aga#ns$ $he !oss#b#l#$" o5 unusable connec$#ons be#ng re$urned $o PHP *hen $here has been a da$abase #ns$ance or node 5a#lure. 0eb ser'ers and $he ne$*or+ should bene5#$ 5rom oc0M.+tatementGcacheG+0Qe be#ng se$. 2or bes$ !er5ormance #$ should generall" be larger $han $he s#1e o5 $he *or+#ng se$ o5 3K. s$a$emen$s. To $une #$, mon#$or general *eb ser'er load and $he &0R )yte ent 0,a S3%CNet to #l,ent 'alues. The la$$er s$a$#s$#c should bene5#$ 5rom no$ sh#!!#ng s$a$emen$ me$a?da$a $o PHP. &d8us$ $he s$a$emen$ cache s#1e $o "our sa$#s5ac$#on. Once "ou are ha!!" *#$h $he s$a$emen$ cache s#1e, $hen $une $he DR P !ool +e++0onGcachedGcur+or+ 'alue. Mon#$or &0R re!or$s *#$h $he goal $o ma+e $he e ,on #&r or #a#'e ',t close $o $he number o5 so5$ !arses. 3o5$ !arses can be calcula$ed 5rom $ar e #o&nt DtotalE m#nus $ar e #o&nt D'ar.E.

App%ication Dep%o:4ent 2or DRCP


PHP a!!l#ca$#ons mus$ s!ec#5" $he ser'er $"!e POO.@D #n $he connec$ s$r#ng. Us#ng OracleLs @as" onnec$ s"n$a/, $he PHP call $o connec$ $o $he +ale+ da$abase on myho+t *ould loo+ l#+e(
,c - oc0Gpconnect:#myu+er#% #mypa++word#% #myho+t/+ale+8?""LED#;(

Or #5 PHP uses an Oracle 7e$*or+ connec$ name $ha$ loo+s l#+e(


,c - oc0Gpconnect:#myu+er#% #mypa++word#% #+ale+pool#;(

Then onl" $he Oracle 7e$*or+ con5#gura$#on 5#le tn+name+.ora needs $o be mod#5#ed(
+ale+pool-:DESCR98T9BL-:ADDRESS-:8RBTBCBK-tcp; :$BST-myho+t.dom.com; :8BRT-2672;;:CBLLECTGDATA-:SERV9CEGLAOE-+ale+; $SE!@E!:?""LED&;;

>5 $hese changes are made and $he da$abase #s no$ ac$uall" con5#gured 5or DR P, connec$#ons *#ll no$ succeed and an error *#ll be re$urned $o PHP.

224

/#ing )!(P in PHP

"losing "onnections
0#$h $he ne* 'ers#on o5 $he O >8 e/$ens#on, clos#ng a !ers#s$en$ connec$#on no* releases $he ser'er $o $he !ool allo*#ng o!$#mal !ool usage. PHP scr#!$s $ha$ do no$ curren$l" use oc0Gclo+e:; should be e/am#ned $o see #5 $he" can use #$ $o re$urn connec$#ons $o $he !ool, allo*#ng ma/#mum use o5 !ooled ser'ers(
!php // 2. Do +ome data1a+e operat0on+ ,conn - oc0Gpconnect:#myu+er#% #mypa++word#% #//myho+t/+ale+:8BBKED#;( . . . oc0Gcomm0t:,conn;( oci_c(ose$2conn&' // !e(ease the connection to the D!C? poo( // 7. Do +ome non-data1a+e worN . . . // ). Do +ome more data1a+e operat0on+ ,conn - oc0Gpconnect:#myu+er#% #mypa++word#% #//myho+t/+ale+:8BBKED#;( . . . oc0Gcomm0t:,conn;( oc0Gclo+e:,conn;( !"

Remember $o 5ree s$a$emen$ and o$her resources $ha$ #ncrease $he re5erence coun$ on $he PHP connec$#on and *#ll s$o! a D- connec$#on clos#ng. Pr#or $o O >8 1.P, clos#ng oc0Gconnect:; and oc0GnewGconnect:; connec$#ons had an e55ec$ bu$ clos#ng an oc0Gpconnect:; connec$#on *as a no?o!. 0#$h O >8 1.P, oc0Gclo+e:; on a !ers#s$en$ connec$#on rolls bac+ an" uncomm#$$ed $ransac$#on. &lso $he e/$ens#on *#ll do a roll bac+ *hen all PHP 'ar#ables re5erenc#ng a !ers#s$en$ connec$#on go ou$ o5 sco!e, 5or e/am!le #5 $he connec$#on *as o!ened #n a 5unc$#on and $he 5unc$#on has no* 5#n#shed. 2or DR P, #n add#$#on $o $he rollbac+, $he connec$#on #s also released< a subse=uen$ oc0Gpconnect:; ma" ge$ a d#55eren$ connec$#on. 2or DR P, $he bene5#$ #s $ha$ scr#!$s $a+#ng ad'an$age o5 !ers#s$en$ connec$#ons can e/!l#c#$l" re$urn a ser'er $o $he !ool *hen non? da$abase !rocess#ng occurs, allo*#ng o$her concurren$ scr#!$s $o ma+e use o5 $he !ooled ser'er. 0#$h !ooled ser'ers, $he recommenda$#on #s $o release $he connec$#on *hen $he scr#!$ does a s#gn#5#can$ amoun$ o5 !rocess#ng $ha$ #s no$ da$abase rela$ed. @/!l#c#$l" con$rol comm#$s and rollbac+s so $here #s no une/!ec$edl" o!en $ransac$#on *hen $he close or end?o5?sco!e occurs. 3cr#!$s coded l#+e $h#s can use oc0Gclo+e:; $o $a+e ad'an$age o5 DR P bu$ s$#ll be !or$able $o older 'ers#ons o5 $he O >8 e/$ens#on. >5 beha'#or *here oc0Gclo+e:; #s a no?o! 5or all connec$#on $"!es #s !re5erred, se$ $he e/#s$#ng php%ini !arame$er oc0M.oldGoc0Gclo+eG+emant0c+ $o Bn.

225

PHP Sca&a*i&ity and High 0vai&a*i&ity

L$G$0 and L$G$(( Triggers with D "P


KBWBL $r#ggers are e55#c#en$ 5or se$$#ng common sess#on a$$r#bu$es such as da$e 5orma$s. KBWBL $r#ggers are

also use5ul 5or se$$#ng sess#on a$$r#bu$es needed b" each PHP connec$#on. 2or e/am!le a $r#gger could be used $o e/ecu$e an AKTER SESS9BL s$a$emen$ $o se$ a da$e 5orma$. The KBWBL $r#gger *#ll e/ecu$e *hen oc0Gpconnect:; crea$es $he sess#on, and $he sess#on *#ll be reused b" subse=uen$ !ers#s$en$ connec$#ons. The sugges$ed !rac$#ce #s $o use KBWBL $r#ggers onl" 5or se$$#ng sess#on a$$r#bu$es and no$ 5or e/ecu$#ng !er PHP?connec$#on log#c such as cus$om logon aud#$#ng. Th#s recommenda$#on #s also $rue 5or !ers#s$en$ connec$#ons *#$h ded#ca$ed or shared ser'ers, and #s $he e/#s$#ng recommenda$#on 5or earl#er releases o5 $he O >8 e/$ens#on. Da$abase ac$#ons $ha$ mus$ be !er5ormed e/ac$l" once !er O >8 connec$#on call should be e/!l#c#$l" e/ecu$ed #n $he PHP scr#!$. >$ #s no$ !oss#ble $o de!end on KBWBL $r#ggers 5or $rac+#ng PHP O >8 connec$ calls. The cach#ng, !ool#ng, $#m#ng ou$ and recrea$#on o5 sess#ons and connec$#ons *#$h or *#$hou$ DR P or $he ne* e/$ens#on can d#s$or$ an" record. 0#$h !ooled ser'ers, KBWBL $r#ggers can 5#re a$ au$hen$#ca$#on and *hen $he sess#on #s crea$ed, #n e55ec$ 5#r#ng $*#ce 5or $he #n#$#al connec$#on. KBWB** $r#ggers do no$ 5#re 5or !ooled ser'ers. 2or non?!ooled ser'ers $here #s no change #n O > 1.P( KBWB** $r#ggers 5#re *hen connec$#ons are closed. 2or oc0Gconnect:; and oc0GnewGconnect:; connec$#ons $h#s #s *#$h oc0Gclo+e:; or a$ $he end o5 $he scr#!$. 2or oc0Gpconnect:; connec$#ons, #$ #s *hen $he *eb ser'er !rocess $erm#na$es or res$ar$s.

"hanging Passwords with D "P "onnections


>n general, PHP a!!l#ca$#ons $ha$ change !ass*ords should a'o#d us#ng !ers#s$en$ connec$#ons because connec$#ons s$ored !ers#s$en$l" #n PHP ma" allo* a!!l#ca$#on connec$#ons $o succeed us#ng $he old !ass*ord. 0#$h DR P, $here #s a 5ur$her l#m#$a$#on ? connec$#ons canno$ be used $o change !ass*ords !rogramma$#call". PHP scr#!$s $ha$ use oc0Gpa++wordGchange:; should con$#nue $o use ded#ca$ed or shared ser'ers.

8onitorin" DRCP
Da$a d#c$#onar" '#e*s are a'a#lable $o mon#$or $he !er5ormance o5 DR P. Da$abase adm#n#s$ra$ors can chec+ s$a$#s$#cs such as $he number o5 bus" and 5ree ser'ers, and $he number o5 h#$s and m#sses #n $he !ool aga#ns$ $he $o$al number o5 re=ues$s 5rom cl#en$s. The '#e*s are(

V,8RBCESS V,SESS9BL DBAGC8BBKG9L*B V,C8BBKGSTATS V,C8BBKGCCGSTATS

The DR P s$a$#s$#cs are rese$ each $#me $he !ool #s s$ar$ed. 2or R& , $here are WV,C8BBKGSTATS and WV,C8BBKGCCGSTATS '#e*s corres!ond#ng $o $he #ns$ance? le'el '#e*s. These record DR P s$a$#s$#cs across clus$ered #ns$ances. >5 a da$abase #ns$ance #n a clus$er #s shu$ do*n, $he s$a$#s$#cs 5or $ha$ #ns$ance are !urged 5rom $he WV, '#e*s.
226

9onitoring )!(P

@GPROCESS and @GSESSION @ie9'


The number o5 con5#gured bro+ers !er #ns$ance can be 5ound 5rom $he V,8RBCESS '#e*. 2or e/am!le, on .#nu/ $h#s =uer" sho*s one bro+er has been enabled(
SUK" +elect program from /,proce++ where program l0Ne #oracle=:L=;#( 8RBWRAO -----------------------------------------------oraclePlocalho+t :L??2;

The V,SESS9BL '#e* *#ll sho* #n5orma$#on abou$ $he curren$l" ac$#'e DR P sess#ons. >$ can also be 8o#ned *#$h V,8RBCESS '#a V,SESS9BL.8ADDR - V,8RBCESS.ADDR $o correla$e $he '#e*s.

D<AACPOOLAIN=O @ie9
DBAGC8BBKG9L*B d#s!la"s con5#gura$#on #n5orma$#on abou$ all DR P !ools #n $he da$abase. The columns are e=u#'alen$ $o $he d1m+Gconnect0onGpool.conf0gureGpool:; se$$#ngs descr#bed #n Table 11, *#$h $he add#$#on o5 a STATUS column. The s$a$us #s ACT9VE #5 $he !ool has been s$ar$ed and 9LACT9VE o$her*#se. 7o$e $he !ool name column #s called CBLLECT9BLG8BBK. 2or e/am!le, $o chec+ *he$her $he

!ool has been s$ar$ed and *ha$ $he ma/#mum number o5 !ooled ser'ers #s se$ $o(
SUK" +elect connect0onGpool% +tatu+% ma<+0Qe from d1aGcpoolG0nfo( CBLLECT9BLG8BBK STATUS OAIS9]E ---------------------------- ---------------- ---------SJSGDE*AUKTGCBLLECT9BLG8BBK ACT9VE 5?

@GCPOOLASTATS @ie9
V,C8BBKGSTATS d#s!la"s #n5orma$#on abou$ $he DR P s$a$#s$#cs 5or an #ns$ance.

#able !4" DE(POO38S#A#S Die&%


Co%u4n
8BBKGLAOE LUOGB8ELGSERVERS

De'cription 7ame o5 $he Da$abase Res#den$ onnec$#on Pool. To$al number o5 bus" and 5ree ser'ers #n $he !ool :#nclud#ng $he au$hen$#ca$#on ser'ers;. To$al number o5 bus" ser'ers #n $he !ool :no$ #nclud#ng $he au$hen$#ca$#on ser'ers;. 7umber o5 au$hen$#ca$#on ser'ers #n $he !ool. 7umber o5 cl#en$ re=ues$s.

LUOGBUSJGSERVERS

LUOGAUT$GSERVERS LUOGREUUESTS

227

PHP Sca&a*i&ity and High 0vai&a*i&ity

Co%u4n
LUOG$9TS

De'cription To$al number o5 $#mes cl#en$ re=ues$s 5ound ma$ch#ng !ooled ser'ers and sess#ons #n $he !ool. To$al number o5 $#mes cl#en$ re=ues$s could no$ 5#nd a ma$ch#ng !ooled ser'er and sess#on #n $he !ool. To$al number o5 cl#en$ re=ues$s $ha$ had $o *a#$ due $o non? a'a#lab#l#$" o5 5ree !ooled ser'ers. Reser'ed 5or 5u$ure use. Reser'ed 5or 5u$ure use. To$al number o5 au$hen$#ca$#ons o5 cl#en$s done b" $he !ool. To$al number o5 sess#ons !urged b" $he !ool. Ma/#mum s#1e $ha$ $he !ool has e'er reached.

LUOGO9SSES

LUOG&A9TS

&A9TGT9OE CK9ELTGREUGT9OEBUTS LUOGAUT$ELT9CAT9BLS LUOG8URWED $9STBR9CGOAI

The V,C8BBKGSTATS '#e* can be used $o assess ho* e55#c#en$ $he !ool se$$#ngs are. Th#s e/am!le =uer" sho*s an a!!l#ca$#on us#ng $he !ool e55ec$#'el". The lo* number o5 m#sses #nd#ca$es $ha$ ser'ers and sess#ons *ere reused. The *a#$ coun$ sho*s 8us$ o'er 1Z o5 re=ues$s had $o *a#$ 5or a !ooled ser'er $o become a'a#lable. >ncreas#ng $he number o5 !ooled ser'ers *ould reduce $he number o5 *a#$s(
SUK" +elect numGreFue+t+%numGh0t+%numGm0++e+%numGwa0t+ from /,cpoolG+tat+ where poolGname - #SJSGDE*AUKTGCBLLECT9BLG8BBK#( LUOGREUUESTS LUOG$9TS LUOGO9SSES LUOG&A9TS ------------ ---------- ---------- ---------2???)2 >>>>) )M 2?65

>5 oc0M.connect0onGcla++ #s se$ :allo*#ng !ooled ser'ers and sess#ons $o be reused; $hen LUOGO9SSES #s lo*. >5 $he !ool ma<+0Qe #s $oo small 5or $he connec$#on load $hen LUOG&A9TS #s h#gh(
LUOGREUUESTS LUOG$9TS LUOGO9SSES LUOG&A9TS ------------ ---------- ---------- ---------6?)67 6?)5M 5 6?25>

>5 $he connec$#on class #s le5$ unse$, $he shar#ng o5 !ooled ser'ers #s res$r#c$ed $o *#$h#n each *eb ser'er !rocess. @'en #5 $he !ool s#1e #s large, sess#on shar#ng #s l#m#$ed caus#ng !oor u$#l#1a$#on o5 !ooled ser'ers and con$en$#on 5or $hem(
LUOGREUUESTS LUOG$9TS LUOGO9SSES LUOG&A9TS ------------ ---------- ---------- ---------S5267 2T>52 5S722 2622M

22

9onitoring )!(P

@GCPOOLACCASTATS @ie9
V,C8BBKGCCGSTATS d#s!la"s #n5orma$#on abou$ $he connec$#on class le'el s$a$#s$#cs 5or $he !ool !er #ns$ance. The columns are s#m#lar $o $hose o5 V,C8BBKGSTATS descr#bed #n Table F, *#$h a CCKASSGLAOE

column g#'#ng $he name o5 $he connec$#on sub?!ool $he resul$s are 5or(
SUK" +elect ccla++Gname%numGreFue+t+%numGh0t+%numGm0++e+ from /,cpoolGccG+tat+( CCKASSGLAOE LUOGREUUESTS LUOG$9TS LUOGO9SSES -------------------- ------------ ---------- ---------$R.OJ8$8A88 2???)2 >>>>) )M SCBTT.S$ARED 2? ? 2? $R.BC9:S8:SdZ<9<2UfQ 2 ? 2

2or PHP, $he CCKASSGLAOE 'alue #s com!osed o5 $he 'alue o5 $he username and o5 $he oc0M.connect0onGcla++ 'alue used b" $he connec$#ng PHP !rocesses. Th#s '#e* sho*s an a!!l#ca$#on +no*n as 6BPHPAPP us#ng $he !ool e55ec$#'el". The las$ l#ne o5 $he e/am!le ou$!u$ sho*s a s"s$em genera$ed class name 5or an a!!l#ca$#on $ha$ d#d no$ e/!l#c#$l" se$ oc0M.connect0onGcla++. Pool#ng #s no$ e55ec$#'el" used #n $h#s case. 3uch an en$r" could be an #nd#ca$#on $ha$ a php%ini 5#le #s m#s?con5#gured. 2or !rograms l#+e 3K.TPlus $ha$ *ere no$ bu#l$ us#ng OracleLs 3ess#on Pool#ng &P>s, $he class name *#ll be S$ARED. The e/am!le sho*s $ha$ $en such connec$#ons *ere made as $he user SCBTT. &l$hough $hese !rograms share $he same connec$#on class, ne* sess#ons are crea$ed 5or each connec$#on, +ee!#ng each cleanl" #sola$ed 5rom an" un*an$ed sess#on changes.

Hi"h A.ai%a)i%it: 9ith =AN and RAC


l#en$s $ha$ run #n h#gh a'a#lab#l#$" con5#gura$#ons such as *#$h Oracle R& or Da$a Auard Ph"s#cal 3$and? -" can $a+e ad'an$age o5 2as$ &!!l#ca$#on 7o$#5#ca$#on :2&7; e'en$s $o allo* a!!l#ca$#ons $o res!ond =u#c+l" $o da$abase node 5a#lures. 2&7 su!!or$ #n PHP ma" be used *#$h or *#$hou$ DR P Q $he $*o 5ea$ures are #nde!enden$. 0#$hou$ 2&7, *hen a da$abase #ns$ance or mach#ne node 5a#ls une/!ec$edl", PHP a!!l#ca$#ons ma" be bloc+ed *a#$#ng 5or a da$abase res!onse un$#l a T P $#meou$ e/!#res. @rrors are $here5ore dela"ed, some$#mes u! $o se'eral m#nu$es, b" *h#ch $#me $he a!!l#ca$#on ma" ha'e e/ceeded PHPLs ma/#mum allo*ed e/ecu$#on $#me. -" le'erag#ng 2&7 e'en$s, PHP a!!l#ca$#ons are =u#c+l" no$#5#ed o5 5a#lures $ha$ a55ec$ $he#r es$abl#shed da$abase connec$#ons. onnec$#ons $o a 5a#led da$abase #ns$ance are !ro?ac$#'el" $erm#na$ed *#$hou$ *a#$#ng 5or a !o$en$#all" leng$h" T P $#meou$. Th#s allo*s PHP scr#!$s $o reco'er =u#c+l" 5rom a node or ne$*or+ 5a#lure. The a!!l#ca$#on can reconnec$ and con$#nue !rocess#ng *#$hou$ $he user be#ng a*are o5 a !roblem. &lso, all #nac$#'e ne$*or+ connec$#ons cached #n PHP $o $he connec$#on bro+er #n case o5 DR P, and !ers#s$en$ connec$#ons $o $he ser'er !rocesses or d#s!a$cher #n case o5 ded#ca$ed or shared ser'er connec$#ons on $he 5a#led #ns$ances, are au$oma$#call" cleaned u!. & subse=uen$ PHP connec$#on call *#ll crea$e a ne* connec$#on $o a sur'#'#ng R& node, ac$#'a$ed s$and?b" da$abase, or e'en $he res$ar$ed s#ngle?#ns$ance da$abase.
22$

PHP Sca&a*i&ity and High 0vai&a*i&ity

Con2i"urin" =AN E.ent' in the Data)a'e


To ge$ $he bene5#$ o5 h#gh a'a#lab#l#$", $he da$abase ser'#ce $o *h#ch $he a!!l#ca$#ons connec$ mus$ be enabled $o !os$ 2&7 e'en$s(
SUK" e<ecute d1m+G+er/0ce.mod0fyG+er/0ce: +er/0ceGname -" #SAKES#% aFGhaGnot0f0cat0on+ -" TRUE;(

Con2i"urin" PHP 2or =AN


0#$h $he O >8 1.P e/$ens#on, a php%ini con5#gura$#on !arame$er oc0M.e/ent+ allo*s PHP $o be no$#5#ed o5 2&7 e'en$s(
oc0M.e/ent+ - Bn

2&7 su!!or$ #s onl" a'a#lable *hen PHP #s l#n+ed *#$h Oracle Da$abase 10g Release 2 or 11g l#brar#es and connec$#ng $o Oracle Da$abase 10g Release 2 or 11g.

App%ication Dep%o:4ent 2or =AN


The error codes re$urned $o PHP *#ll generall" be $he same as *#$hou$ 2&7 enabled, so a!!l#ca$#on error handl#ng can rema#n unchanged. &l$erna$#'el", a!!l#ca$#ons can be enhanced $o reconnec$ and re$r" ac$#ons, $a+#ng ad'an$age o5 $he h#gher le'el o5 ser'#ce g#'en b" 2&7. &s an e/am!le, $he code belo* does some *or+ :!erha!s a ser#es o5 u!da$e s$a$emen$s;. >5 $here #s a connec$#on 5a#lure, #$ reconnec$s, chec+s $he $ransac$#on s$a$e and re$r#es $he *or+. The O >8 1.P e/$ens#on *#ll de$ec$ $he connec$#on 5a#lure and be able reconnec$ on re=ues$, bu$ $he user scr#!$ mus$ also de$erm#ne $ha$ *or+ 5a#led, *h" #$ 5a#led, and be able $o con$#nue $ha$ *or+. The e/am!le code de$ec$s connec$#ons errors so #$ can #den$#5" #$ needs $o con$#nue or re$r" *or+. >$ #s generall" #m!or$an$ no$ $o redo o!era$#ons $ha$ alread" comm#$$ed u!da$ed da$a. T"!#cal errors re$urned a5$er an #ns$ance 5a#lure are BRA-2726): TLS:not connected or BRA-?)22): end-of-f0le on commun0cat0on channel. O$her connec$#on rela$ed errors are sho*n #n $he e/am!le, bu$ errors #nclud#ng s$andard da$abase errors ma" be re$urned, de!end#ng on $#m#ng.
funct0on 0+Connect0onError:,err; @ +w0tch:,err; @ ca+e )TM: /H 1uffer pool param 0ncorrect H/ ca+e S?7: /H core dump H/ ca+e S?): /H fatal error H/ ca+e S?>: /H attach fa0led H/ ca+e 2?27: /H not logged 0n H/ ca+e 2?)): /H 0n0t or +hutdown 0n progre++ H/ ca+e 2?5): /H Bracle not a/a0la1le H/ ca+e 2?M>: /H 0mmed0ate +hutdown 0n progre++ H/ ca+e 2?>?: /H +hutdown 0n progre++ H/ ca+e 2?>7: /H 0n+tance term0nated H/ ca+e )22): /H d0+connect H/ 23%

High 0vai&a*i&ity with :06 and !0( ca+e )225: /H ca+e )277: /H ca+e )2)6: /H ca+e 2726): /H ca+e 7T25S: /H ca+e 7M622: /H return true( not connected H/ clo+0ng w0ndow H/ lo+t contact H/ TLS: not connected H/ fatal or 0n+tance term0nated H/ Ko+t R8C H/

A return fal+e(

,conn - doConnect:;( ,err - doSome&orN:,conn;( 0f :0+Connect0onError:,err;; @ // reconnect% f0nd what wa+ comm0tted% and retry ,conn - doConnect:;( ,err - checNAppl0cat0onStateAndCont0nue&orN:,conn;( A 0f :,err; @ // end the appl0cat0on handleError:,err;( A

RAC Connection Load <a%ancin" 9ith PHP


PHP O >8 1.P *#ll au$oma$#call" balance ne* connec$#ons across R& #ns$ances *#$h OracleGs onnec$#on .oad -alanc#ng : .-; $o use resources e55#c#en$l". >$ #s recommended $o use 2&7 and .- $oge$her. To enable .-, $he da$abase ser'#ce mus$ be mod#5#ed us#ng d1m+G+er/0ce $o send load e'en$s. 3e$ $he cl1Ggoal !arame$er $o CKBGWBAKGS$BRT or CKBGWBAKGKBLW. 2or e/am!le(
SUK" e<ecute d1m+G+er/0ce.mod0fyG+er/0ce: +er/0ceGname -" #SAKES#% cl1Ggoal -" d1m+G+er/0ce.cl1GgoalGlong;(

#able !-" (3C clb8goal parameter values%


Para4eter @a%ue CKBGWBAKGS$BRT Para4eter De'cription Use 5or connec$#on load balanc#ng me$hod 5or a!!l#ca$#ons $ha$ ha'e shor$?l#'ed connec$#ons. Th#s uses load balanc#ng ad'#sor#es #5 $he goal !arame$er #s no$ WBAKGLBLE, o$her*#se a PU u$#l#1a$#on #s used. Use 5or a!!l#ca$#ons $ha$ ha'e long?l#'ed connec$#ons. Th#s uses a s#m!le me$r#c $o d#s$r#bu$e load. Th#s #s $he de5aul$.

CKBGWBAKGKBLW

7o PHP scr#!$ changes are needed. The connec$#on balanc#ng #s handled $rans!aren$l" b" $he Oracle 7e$ l#s$ener.

231

PHP Sca&a*i&ity and High 0vai&a*i&ity

232

CHAPTER 1#

!LO<ALI1ATION
Th#s ha!$er d#scusses global a!!l#ca$#on de'elo!men$ #n a PHP and Oracle Da$abase en'#ronmen$. >$ addresses $he bas#c $as+s assoc#a$ed *#$h de'elo!#ng and de!lo"#ng global >n$erne$ a!!l#ca$#ons, #nclud#ng de'elo!#ng locale a*areness, cons$ruc$#ng HTM. con$en$ #n $he user?!re5erred language, and !resen$#ng da$a 5ollo*#ng $he cul$ural con'en$#ons o5 $he locale o5 $he user. -u#ld#ng a global >n$erne$ a!!l#ca$#on $ha$ su!!or$s d#55eren$ locales re=u#res good de'elo!men$ !rac$#ces. & locale re5ers $o a na$#onal language and $he reg#on #n *h#ch $he language #s s!o+en. The a!!l#ca$#on #$sel5 mus$ be a*are o5 $he locale !re5erence o5 $he user and be able $o !resen$ con$en$ 5ollo*#ng $he cul$ural con'en$#ons e/!ec$ed b" $he user. >$ #s #m!or$an$ $o !resen$ da$a *#$h a!!ro!r#a$e locale charac$er#s$#cs, such as $he correc$ da$e and number 5orma$s. Oracle Da$abase #s 5ull" #n$erna$#onal#1ed $o !ro'#de a global !la$5orm 5or de'elo!#ng and de!lo"#ng global a!!l#ca$#ons.

E'ta)%i'hin" the En.iron4ent <et9een Orac%e and PHP


orrec$l" se$$#ng u! $he connec$#'#$" be$*een $he PHP eng#ne and $he Oracle da$abase #s $he 5#rs$ s$e! #n bu#ld#ng a global a!!l#ca$#on. >$ guaran$ees da$a #n$egr#$" across all $#ers. Mos$ >n$erne$ based s$andards su!!or$ Un#code as a charac$er encod#ng. Th#s cha!$er 5ocuses on us#ng Un#code as $he charac$er se$ 5or da$a e/change. O >8 #s an Oracle O > a!!l#ca$#on, and rules $ha$ a!!l" $o O > also a!!l" $o PHP. Oracle locale beha'#or :#nclud#ng $he cl#en$ charac$er se$ used #n O > a!!l#ca$#ons; #s de5#ned b" OracleLs national language support LKSGKALW en'#ronmen$ 'ar#able. Th#s en'#ronmen$ 'ar#able has $he 5orm(
language"G terr0tory". character +et"

2or e/am!le, 5or a Aerman user #n Aerman" runn#ng an a!!l#ca$#on #n Un#code, LKSGKALW should be se$ $o
WEROALGWEROALJ.AK)7UT*M

The language and $err#$or" se$$#ngs con$rol Oracle beha'#ors such as $he Oracle da$e 5orma$, error message language, and $he rules used 5or sor$ order. The charac$er se$ &.P2UT28 #s $he Oracle name 5or UT2?8. The charac$er se$ can also be !assed as a !arame$er $o $he O >8 connec$#on 5unc$#ons. Do#ng $h#s #s recommended 5or !er5ormance reasons, e'en #5 7.3J.&7A #s also se$. >5 $he charac$er se$ used b" PHP does no$ ma$ch $he charac$er se$ used b" $he da$abase, Oracle *#ll $r" $o con'er$ *hen da$a #s #nser$ed and =uer#ed. Th#s ma" reduce !er5ormance. &lso an accura$e ma!!#ng #s no$ al*a"s be !oss#ble, resul$#ng #n da$a be#ng con'er$ed $o =ues$#on mar+s. There are o$her en'#ronmen$ 'ar#ables $ha$ can be used $o se$ !ar$#cular as!ec$s o5 global#1a$#on. 2or #n5orma$#on on LKSGKALW and o$her Oracle language en'#ronmen$ 'ar#ables, see $he Oracle documen$a$#on. The sec$#on (on$iguring Apache H##P Server on 3inux #n $he &!ache cha!$er d#scusses ho* en'#ronmen$ 'ar#ables can be se$ 5or &!ache. 0hen 9end ore 5or Oracle #s #ns$alled on &!ache, "ou can se$ LKSGKALW #n /etc/prof0le(
233

G&o*a&i?ation e<port LKSGKALW WEROALGWEROALJ.AK)7UT*M

>5 $he global#1a$#on se$$#ngs are #n'al#d, PHP ma" 5a#l $o connec$ $o Oracle and g#'e an error l#+e
BRA-27T?6: Cannot acce++ LKS data f0le+ or 0n/al0d en/0ronment +pec0f0ed

3ome global#1a$#on 'alues can be changed !er connec$#on.


,+ - oc0Gpar+e:,c%.alter +e++0on +et nl+Gterr0tory-germany nl+Glanguage-german.;( oc0Ge<ecute:,+;(

&5$er e/ecu$#ng $h#s, Oracle error messages *#ll be #n Aerman and $he de5aul$ da$e 5orma$ *#ll ha'e changed.

Caution: When chan&in& the &lobali2ation settin&s for a persistent connection, the ne>t time the connection is used, the altered values will still be in effect.

>5 PHP #s #ns$alled on Oracle HTTP 3er'er, "ou mus$ se$ LKSGKALW as an en'#ronmen$ 'ar#able #n ,BRACKEG$BOE/opmn/conf/opmn.<ml(
0a+-component 0d-.$TT8GSer/er." proce++-type 0d-.$TT8GSer/er. module-0d-.B$S." en/0ronment" /ar0a1le 0d-.8ERK6K9B. /alue-.D:EoracleE2?27Y7EEEApacheEApacheE modGperlE+0teE6.S.2El01./" /ar0a1le 0d-.8$8RC. /alue-.D:EoracleE2?27Y7EEEApacheEApacheEconf./" /ar0a1le 0d-.LKSGKALW. /alue-.germanGgermany.al)7utfM./" /en/0ronment" module-data" category 0d-.+tart-parameter+." data 0d-.+tart-mode. /alue-.++l-d0+a1led./" /category" /module-data" proce++-+et 0d-.$TT8GSer/er. numproc+-.2./" /proce++-type" /0a+-component"

Hou mus$ res$ar$ $he 0eb l#s$ener $o #m!lemen$ $he change. To 5#nd $he language and $err#$or" curren$l" used b" PHP, and $he charac$er se$ *#$h *h#ch $he da$abase s$ores da$a, e/ecu$e(
,+ - oc0Gpar+e:,c% .+elect +y+Gconte<t:#u+eren/#% #language#; a+ nl+Glang from dual.;( oc0Ge<ecute:,+;( ,re+ - oc0GfetchGarray:,+% BC9GASSBC;( echo ,re+3#LKSGKALW#4 . .En.(

Ou$!u$ #s o5 $he 5orm(


AOER9CALGAOER9CA.&EMOS&9L2767

234

9ani"u&ating String#

8anipu%atin" Strin"'
PHP *as des#gned $o *or+ *#$h $he >3O?885D?1 charac$er se$. To handle o$her charac$er se$s, $here are se'eral e/$ens#ons $ha$ can be used. The common e/$ens#ons are mbs$r#ng and #con'. Recen$l" a ne* #n$l !ac+age has been added $o P@ .. >$ #m!lemen$s some >n$erna$#onal om!onen$s 5or Un#code :> U; 5unc$#onal#$" and #s l#+el" $o become !o!ular. To enable $he mbs$r#ng e/$ens#on #n PHP us#ng $he 9end ore 5or Oracle &dm#n#s$ra$#on onsole( 1. 2. P. E. 5. O!en $he 9end ore 5or Oracle &dm#n#s$ra$#on onsole b" en$er#ng $he UR. #n a *eb bro*ser(
http://machine_name/]endCore

.og#n and na'#ga$e $o Con5,*&rat,on S E"ten ,on S =en. Core E"ten ,on . @nable $he !) tr,n* e/$ens#on. 3a'e $he con5#gura$#on. Res$ar$ &!ache.

0hen "ou ha'e enabled $he mbstring e/$ens#on and res$ar$ed $he *eb ser'er, se'eral con5#gura$#on o!$#ons become a'a#lable #n $he 9end ore 5or Oracle &dm#n#s$ra$#on onsole. Re5resh $he bro*ser on $he @/$ens#on on5#gura$#on !age $o see $hese mbstring con5#gura$#on o!$#ons. Hou can change $he beha'#or o5 $he s$andard PHP s$r#ng 5unc$#ons b" se$$#ng m1+tr0ng.funcGo/erload $o one o5 $he Overload se$$#ngs. 2or more #n5orma$#on, see $he PHP mbstring re5erence manual a$ http://www.php.net/m1+tr0ng. Hour a!!l#ca$#on code should use 5unc$#ons such as m1G+trlen:; $o calcula$e $he number o5 charac$ers #n s$r#ngs. Th#s ma" re$urn d#55eren$ 'alues $han +trlen:;, *h#ch re$urns $he number o5 b"$es #n a s$r#ng.

Deter4inin" the Loca%e o2 the ,'er


>n a global en'#ronmen$, "our a!!l#ca$#on should accommoda$e users *#$h d#55eren$ locale !re5erences. Once #$ has de$erm#ned $he !re5erred locale o5 $he user, $he a!!l#ca$#on should cons$ruc$ HTM. con$en$ #n $he language o5 $he locale and 5ollo* $he cul$ural con'en$#ons #m!l#ed b" $he locale. & common me$hod $o de$erm#ne $he locale o5 a user #s 5rom $he de5aul$ >3O locale se$$#ng o5 $he bro*ser. Usuall" a bro*ser sends #$s locale !re5erence se$$#ng $o $he HTTP ser'er *#$h $he Accept Kanguage HTTP header. >5 $he Accept Kanguage header #s LUKK, $hen $here #s no locale !re5erence #n5orma$#on a'a#lable, and $he a!!l#ca$#on should 5all bac+ $o a !rede5#ned de5aul$ locale. The 5ollo*#ng PHP code re$r#e'es $he >3O locale 5rom $he Accept-Kanguage HTTP header $hrough $he ,GSERVER 3er'er 'ar#able.
,+ - ,GSERVER3.$TT8GACCE8TGKALWUAWE.4

De#eloping Locale 'wareness


Once $he locale !re5erence o5 $he user has been de$erm#ned, $he a!!l#ca$#on can call locale?sens#$#'e 5unc$#ons, such as da$e, $#me, and mone$ar" 5orma$$#ng $o 5orma$ $he HTM. !ages accord#ng $o $he cul$ural con'en$#ons o5 $he locale.

235

G&o*a&i?ation

0hen "ou *r#$e global a!!l#ca$#ons #m!lemen$ed #n d#55eren$ !rogramm#ng en'#ronmen$s, "ou should enable $he s"nchron#1a$#on o5 user locale se$$#ngs be$*een $he d#55eren$ en'#ronmen$s. 2or e/am!le, PHP a!!l#ca$#ons $ha$ call P.I3K. !rocedures should ma! $he >3O locales $o $he corres!ond#ng LKSGKALWUAWE and LKSGTERR9TBRJ 'alues and change $he !arame$er 'alues $o ma$ch $he locale o5 $he user be5ore call#ng $he P.I3K. !rocedures. The P.I3K. UTKG92ML !ac+age con$a#ns ma!!#ng 5unc$#ons $ha$ can ma! be$*een >3O and Oracle locales. Table 18 sho*s ho* some commonl" used locales are de5#ned #n >3O and Oracle en'#ronmen$s.

#able !*" 3ocale representations in )SO7 S@3 and P3<S@3 programming environments%
Loca%e h#nese :P.R. .; h#nese :Ta#*an; @ngl#sh :U.3.&; @ngl#sh :Un#$ed 6#ngdom; 2rench : anada; 2rench :2rance; Aerman >$al#an )a!anese 6orean Por$uguese :-ra1#l; Por$uguese 3!an#sh Loca%e ID Qh-CL Qh-T& en-US en-WB fr-CA fr-*R de 0t Za No pt-BR pt e+ NLSALAN!,A!E S9O8K9*9ED C$9LESE TRAD9T9BLAK C$9LESE AOER9CAL ELWK9S$ CALAD9AL *RELC$ *RELC$ WEROAL 9TAK9AL YA8ALESE CBREAL BRA]9K9AL 8BRTUWUESE 8BRTUWUESE S8AL9S$ NLSATERRITORY C$9LA TA9&AL AOER9CA UL9TED C9LWDBO CALADA *RALCE WEROALJ 9TAKJ YA8AL CBREA BRA]9K 8BRTUWAK S8A9L

Encodin" HT8L Pa"e'


The encod#ng o5 an HTM. !age #s #m!or$an$ #n5orma$#on 5or a bro*ser and an >n$erne$ a!!l#ca$#on. Hou can $h#n+ o5 $he !age encod#ng as $he charac$er se$ used 5or $he locale $ha$ an >n$erne$ a!!l#ca$#on #s ser'#ng. The bro*ser mus$ +no* abou$ $he !age encod#ng so $ha$ #$ can use $he correc$ 5on$s and charac$er se$ ma!!#ng $ables $o d#s!la" $he HTM. !ages. >n$erne$ a!!l#ca$#ons mus$ +no* abou$ $he HTM. !age encod#ng so $he" can !rocess #n!u$ da$a 5rom an HTM. 5orm. >ns$ead o5 us#ng d#55eren$ na$#'e encod#ngs 5or $he d#55eren$ locales, Oracle recommends $ha$ "ou use UT2?8 :Un#code encod#ng; 5or all !age encod#ngs. Th#s encod#ng no$ onl" s#m!l#5#es $he cod#ng 5or global a!!l#ca$#ons, bu$ #$ also enables mul$#l#ngual con$en$ on a s#ngle !age.

Speci2:in" the Pa"e Encodin" 2or HT8L Pa"e'


Hou can s!ec#5" $he encod#ng o5 an HTM. !age e#$her #n $he HTTP header, or #n HTM. !age header.
236

'ncoding H-9, Page#

Speci2:in" the Encodin" in the HTTP Header


To s!ec#5" HTM. !age encod#ng #n $he HTTP header, #nclude $he Content-Type HTTP header #n $he HTTP s!ec#5#ca$#on. >$ s!ec#5#es $he con$en$ $"!e and charac$er se$. The Content-Type HTTP header has $he 5ollo*#ng 5orm(
Content-Type: te<t/html( char+et-utf-M

The char+et !arame$er s!ec#5#es $he encod#ng 5or $he HTM. !age. The !oss#ble 'alues 5or $he char+et !arame$er are $he >&7& names 5or $he charac$er encod#ngs $ha$ $he bro*ser su!!or$s.

Speci2:in" the Encodin" in the HT8L Pa"e Header


Use $h#s me$hod !r#mar#l" 5or s$a$#c HTM. !ages. To s!ec#5" HTM. !age encod#ng #n $he HTM. !age header, s!ec#5" $he charac$er encod#ng #n $he HTM. header as 5ollo*s(
meta http-eFu0/-.Content-Type. content-.te<t/html(char+et-utf-M."

The char+et !arame$er s!ec#5#es $he encod#ng 5or $he HTM. !age. &s *#$h $he Content-Type $TT8 $eader, $he !oss#ble 'alues 5or $he char+et !arame$er are $he >&7& :>n$erne$ &ss#gned 7umbers &u$hor#$"; names 5or $he charac$er encod#ngs $ha$ $he bro*ser su!!or$s.

Speci2:in" the Pa"e Encodin" in PHP


Hou can s!ec#5" $he encod#ng o5 an HTM. !age #n $he Content-Type HTTP header #n PHP b" se$$#ng $he defaultGchar+et con5#gura$#on 'ar#able #n php%ini(
defaultGchar+et - UT*-M

To enable charac$er se$ encod#ng o5 HTM. !ages us#ng $he 9end ore 5or Oracle &dm#n#s$ra$#on onsole. 1. 2. P. E. 5. O!en $he 9end ore 5or Oracle &dm#n#s$ra$#on onsole b" en$er#ng $he UR. #n a *eb bro*ser(
http:// machine_name"/]endCore

.og#n and na'#ga$e $o Con5,*&rat,on S PHP S (ata Han.l,n*. @n$er UT*-M 5or $he .e5a&ltA#'ar et !arame$er. 3a'e $he con5#gura$#on. Res$ar$ &!ache.

Th#s se$$#ng does no$ #m!l" an" con'ers#on o5 ou$go#ng !ages. Hour a!!l#ca$#on mus$ ensure $ha$ $he ser'er?genera$ed !ages are encoded #n UT2?8.

Or"ani;in" the Content o2 HT8L Pa"e' 2or Tran'%ation


Ma+#ng $he user #n$er5ace a'a#lable #n $he local language o5 $he user #s a 5undamen$al $as+ #n global#1#ng an a!!l#ca$#on. Transla$able sources 5or $he con$en$ o5 an HTM. !age belong $o $he 5ollo*#ng ca$egor#es(

237

G&o*a&i?ation

Te/$ s$r#ngs #ncluded #n $he a!!l#ca$#on code 3$a$#c HTM. 5#les, #mages 5#les, and $em!la$e 5#les such as 33 D"nam#c da$a s$ored #n $he da$abase

Strin"' in PHP
Hou should e/$ernal#1e $ransla$able s$r#ngs *#$h#n "our PHP a!!l#ca$#on log#c, so $ha$ $he $e/$ #s read#l" a'a#lable 5or $ransla$#on. These $e/$ messages can be s$ored #n 5la$ 5#les or da$abase $ables de!end#ng on $he $"!e and $he 'olume o5 $he da$a be#ng $ransla$ed. PHPGs gettext e/$ens#on #s o5$en used 5or $h#s !ur!ose.

Static =i%e'
3$a$#c 5#les such as HTM. and $e/$ s$ored as #mages are read#l" $ransla$able. 0hen $hese 5#les are $ransla$ed, $he" should be $ransla$ed #n$o $he corres!ond#ng language *#$h UT2?8 as $he 5#le encod#ng. To d#55eren$#a$e $he languages o5 $he $ransla$ed 5#les, s$age $he s$a$#c 5#les o5 d#55eren$ languages #n d#55eren$ d#rec$or#es or *#$h d#55eren$ 5#le names.

Data 2ro4 the Data)a'e


D"nam#c #n5orma$#on such as !roduc$ names and !roduc$ descr#!$#ons #s $"!#call" s$ored #n $he da$abase. To d#55eren$#a$e 'ar#ous $ransla$#ons, $he da$abase schema hold#ng $h#s #n5orma$#on should #nclude a column $o #nd#ca$e $he language. To selec$ $he des#red language, "ou mus$ #nclude a &$ERE clause #n "our =uer".

Pre'entin" Data ,'in" Con.ention' E&pected ): the ,'er


Da$a #n $he a!!l#ca$#on mus$ be !resen$ed #n a *a" $ha$ con5orms $o $he e/!ec$a$#on o5 $he user. O$her*#se, $he mean#ng o5 $he da$a can be m#s#n$er!re$ed. 2or e/am!le, $he da$e X12I11I05L #m!l#es X11$h December 2005L #n $he Un#$ed 3$a$es, *hereas #n $he Un#$ed 6#ngdom #$ means X12$h 7o'ember 2005L. 3#m#lar con5us#on e/#s$s 5or number and mone$ar" 5orma$s o5 $he users. 2or e/am!le, $he s"mbol X.L #s a dec#mal se!ara$or #n $he Un#$ed 3$a$es< #n Aerman" $h#s s"mbol #s a $housand se!ara$or. Th#s can be a !ar$#cular !roblem #n PHP *hen da$abase numbers are 5e$ched as PHP s$r#ngs. D#55eren$ languages ha'e $he#r o*n sor$#ng rules. 3ome languages are colla$ed accord#ng $o $he le$$er se=uence #n $he al!habe$, some accord#ng $o $he number o5 s$ro+e coun$s #n $he le$$er, and some languages are ordered b" $he !ronunc#a$#on o5 $he *ords. Presen$#ng da$a no$ sor$ed #n $he l#ngu#s$#c se=uence $ha$ "our users are accus$omed $o can ma+e search#ng 5or #n5orma$#on d#55#cul$ and $#me consum#ng. De!end#ng on $he a!!l#ca$#on log#c and $he 'olume o5 da$a re$r#e'ed 5rom $he da$abase, #$ ma" be more a!!ro!r#a$e $o 5orma$ $he da$a a$ $he da$abase le'el ra$her $han a$ $he a!!l#ca$#on le'el. Oracle o55ers man" 5ea$ures $ha$ hel! $o re5#ne $he !resen$a$#on o5 da$a *hen $he locale !re5erence o5 $he user #s +no*n. The 5ollo*#ng sec$#ons !ro'#de e/am!les o5 locale?sens#$#'e o!era$#ons #n 3K..

23

Pre#enting )ata /#ing (onvention# 'x"ected *y the /#er

Orac%e Nu4)er =or4at'


O >8 5e$ches numbers as PHP s$r#ngs. The con'ers#on #s done b" Oracle and can be cus$om#1ed. >$ #s !oss#ble $o lose dec#mal !laces or ge$ errors #n PHP *hen #$ #n$ernall" con'er$s s$r#ngs no$ us#ng $he U3 5orma$$#ng con'en$#ons. The 5ollo*#ng e/am!les #llus$ra$e $he d#55erences #n $he dec#mal charac$er and grou! se!ara$or be$*een $he Un#$ed 3$a$es and Aerman" *hen numbers are con'er$ed $o s$r#ngs b" Oracle.
SUK" alter +e++0on +et nl+Gterr0tory - amer0ca( Se++0on altered. SUK" 7 ) 5 6 +elect employeeG0d Emp9D% +u1+tr:f0r+tGname%2%2;XX#.#XXla+tGname .EmpLame.% toGchar:+alary% #>>W>>>D>>#; .Salary. from employee+ where employeeG0d 2?6( EmpLame Salary --------------------------- ---------S.C0ng 75%???.?? L.Cochhar 2T%???.?? K.De $aan 2T%???.?? A.$unold >%???.?? B.Ern+t S%???.??

EO89D ---------2?? 2?2 2?7 2?) 2?5

SUK" alter +e++0on +et nl+Gterr0tory - germany( Se++0on altered. SUK" 7 ) 5 6 +elect employeeG0d Emp9D% +u1+tr:f0r+tGname%2%2;XX#.#XXla+tGname .EmpLame.% toGchar:+alary% #>>W>>>D>>#; .Salary. from employee+ where employeeG0d 2?6( EmpLame Salary --------------------------- ---------S.C0ng 75.???%?? L.Cochhar 2T.???%?? K.De $aan 2T.???%?? A.$unold >.???%?? B.Ern+t S.???%??

EO89D ---------2?? 2?2 2?7 2?) 2?5

The 5orma$ GDDADDDDDDG con$a#ns $he GAG $housands se!ara$or and GDG dec#mal se!ara$or a$ $he a!!ro!r#a$e !laces #n $he des#red ou$!u$ number. >n $he $*o $err#$or#es, $he ac$ual charac$er d#s!la"ed #s d#55eren$. The e=u#'alen$ PHP e/am!le #s( Script **" num$ormat%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( 23$

G&o*a&i?ation

,+ - oc0Gpar+e:,c% .alter +e++0on +et nl+Gterr0tory - germany.;( oc0Ge<ecute:,+;( ,+ - oc0Gpar+e:,c% .+elect 27).6ST a+ num from dual.;( oc0Ge<ecute:,+;( ,r - oc0GfetchGarray:,+% BC9GASSBC;( ,n2 - ,r3#LUO#4( /arGdump:,n2;( ,n7 - :float;,n2( /arGdump:,n7;( !" // /alue a+ fetched // now ca+t 0t to a num1er

The ou$!u$ #s(


+tr0ng:T; .27)%6ST. float:27);

>5 LKSGTERR9TBRJ had #ns$ead been se$ $o amer0ca $he ou$!u$ *ould ha'e been correc$(
+tr0ng:T; .27).6ST. float:27).6ST;

The !roblem can also occur de!end#ng on $he $err#$or" com!onen$ o5 LKSGKALW, or $he 'alue o5 LKSGLUOER9CGC$ARACTERS. The la$$er 'ar#able can be used $o o'err#de $he number 5orma$ *h#le o$her $err#$or" se$$#ngs rema#n #n e55ec$. >$ can be se$ as an en'#ronmen$ 'ar#able(
R e<port LKSGLUOER9CGC$ARACTERS-a.%a R apachectl +tart

or *#$h a logon $r#gger, or b" us#ng an AKTER SESS9BL command #n PHP(


,+ - oc0Gpar+e:,c% .alter +e++0on +et nl+Gnumer0cGcharacter+ - #.%#.;( oc0Ge<ecute:,+;(

hang#ng #$ #n PHP #s l#+el" $o be $he slo*es$ o5 $he me$hods. The $#! Do Got Set the Date ormat >nnecessarily #n $he cha!$er (onnecting to Oracle >sing O()* sho*s ho* an AKTER SESS9BL command can be used #n a da$abase logon $r#gger.

Orac%e Date =or4at'


The bas#c da$e 5orma$ used b" Oracle de!ends on "our Alobal#1a$#on se$$#ngs, such as $he 'alue #n LKSGKALW. The $hree d#55eren$ da$e !resen$a$#on 5orma$s #n Oracle are s$andard, shor$, and long da$es. The 5ollo*#ng e/am!les #llus$ra$e $he d#55erences be$*een $he shor$ da$e and long da$e 5orma$s 5or bo$h $he Un#$ed 3$a$es and Aerman".
SUK" alter +e++0on +et nl+Gterr0tory - amer0ca nl+Glanguage - amer0can( SUK" +elect employeeG0d Emp9D% 24%

Pre#enting )ata /#ing (onvention# 'x"ected *y the /#er 7 ) 5 6 SH +u1+tr:f0r+tGname%2%2;XX#.#XXla+tGname .EmpLame.% toGchar:h0reGdate%#DS#; .$0redate.% toGchar:h0reGdate%#DK#; .Kong $0reDate. from employee+ where employeeG0d 2?6( EmpLame ---------------S.C0ng L.Cochhar K.De $aan A.$unold B.Ern+t $0redate ---------?S/2T/2>MT ?>/72/2>M> ?2/2)/2>>) ?2/?)/2>>? ?6/72/2>>2 Kong $0reDate ----------------------------&edne+day% Yune 2T% 2>MT Thur+day% Septem1er 72% 2>M> &edne+day% Yanuary 2)% 2>>) &edne+day% Yanuary )% 2>>? Tue+day% Oay 72% 2>>2

EO89D ---------2?? 2?2 2?7 2?) 2?5

SUK" alter +e++0on +et nl+Gterr0tory-germany nl+Glanguage-german( SUK" 7 ) 5 6 SH +elect employeeG0d Emp9D% +u1+tr:f0r+tGname%2%2;XX#.#XXla+tGname .EmpLame.% toGchar:h0reGdate%#DS#; .$0redate.% toGchar:h0reGdate%#DK#; .Kong $0reDate. from employee+ where employeeG0d 2?6( EmpLame ----------------S.C0ng L.Cochhar K.De $aan A.$unold B.Ern+t $0redate -------2T.?S.MT 72.?>.M> 2).?2.>) ?).?2.>? 72.?6.>2 Kong $0reDate -----------------------------O0ttwoch% 2T. Yun0 2>MT Donner+tag% 72. Septem1er 2>M> O0ttwoch% 2). Yanuar 2>>) O0ttwoch% ). Yanuar 2>>? D0en+tag% 72. Oa0 2>>2

EO89D ---------2?? 2?2 2?7 2?) 2?5

>n add#$#on $o $hese $hree 5orma$ s$"les "ou can cus$om#1e $he 5orma$ us#ng man" o$her da$e 5orma$ s!ec#5#ers. 3earch $he Oracle documen$a$#on 5or Mda$e$#me 5orma$ elemen$sO $o see a l#s$. >5 $he da$e 5orma$ der#'ed 5rom $he LKSGKALW se$$#ng #s no$ $he one "ou *an$ 5or "our PHP sess#on, "ou can o'err#de $he 5orma$ b" se$$#ng $he en'#ronmen$ 'ar#able LKSGDATEG*BROAT #n $he shell $ha$ s$ar$s "our *eb ser'er or PHP e/ecu$able(
R e<port LKSGDATEG*BROAT-#JJJJ-OO-DD $$75:O9:SS# R apachectl +tart

&l$erna$#'el" "ou can se$ #$ #n a logon $r#gger, or change #$ a5$er connec$#ng #n PHP(
,+ - oc0Gpar+e:,c% .alter +e++0on +et nl+GdateGformat-#JJJJ-OO-DD $$75:O9:SS#.;( oc0Ge<ecute:,+;(

3ubse=uen$ =uer#es *#ll re$urn $he ne* 5orma$(


,+ ,r ,row echo oc0Gpar+e:,c% .+elect +y+date from dual.;( oc0Ge<ecute:,+;( - oc0GfetchGarray:,+% BC9GASSBC;( .Date 0+ . . ,row3.SJSDATE.4 . .En.(

The ou$!u$ #s(

241

G&o*a&i?ation Date 0+ 7??T-?M-?2 22:5):)?

One ad'an$age o5 se$$#ng $he da$e 5orma$ globall" #ns$ead o5 us#ng toGchar:; #s #$ allo*s PHP and Oracle $o share a common 5orma$ 5or #nser$s and =uer#es( Script *?" date$ormat%php
!php ,c - oc0Gconnect:#hr#% #hrpwd#% #localho+t/IE#;( // Set default Bracle date format ,+ - oc0Gpar+e:,c% .alter +e++0on +et nl+GdateGformat-#JJJJ-OO-DD $$75:O9:SS#.;( oc0Ge<ecute:,+;( // Th0+ 8$8 Date format matche+ the new Bracle format ,d - date:#J-m-d $:0:+#;( echo .9n+ert0ng ,dEn.( ,+ - oc0Gpar+e:,c% .0n+ert 0nto employee+ :employeeG0d% la+tGname% ema0l% h0reGdate% Zo1G0d; /alue+ :2% #Yone+#% #cZPe<ample.com#% :dt% #STGCKERC#;.;( oc0G10ndG1yGname:,+% .:dt.% ,d;( oc0Ge<ecute:,+;( ,+ - oc0Gpar+e:,c% .+elect h0reGdate from employee+ where employeeG0d - 2.;( oc0Ge<ecute:,+;( oc0GfetchGall:,+% ,re+;( /arGdump:,re+;( !"

The ou$!u$ #s(


9n+ert0ng 7??M-2?-7) ?5:?2:2T array:2; @ 3.$9REGDATE.4-" array:2; @ 3?4-" +tr0ng:2>; .7??M-2?-7) ?5:?2:2T. A A

Orac%e Lin"ui'tic Sort'


3!a#n $rad#$#onall" $rea$s ch, ll as *ell as M as un#=ue le$$ers, ordered a5$er c, l and n, res!ec$#'el". The 5ollo*#ng e/am!les #llus$ra$e $he e55ec$ o5 us#ng a 3!an#sh sor$ aga#ns$ $he em!lo"ee names (hen and (hung.
SUK" alter +e++0on +et nl+G+ort - 10nary( SUK" +elect employeeG0d Emp9D% 7 la+tGname .Ka+t Lame. ) from employee+ 242

Pre#enting )ata /#ing (onvention# 'x"ected *y the /#er 5 6 where la+tGname l0Ne #C=# order 1y la+tGname( Ka+t Lame ------------------------Ca1r0o Cam1rault Cam1rault Chen Chung Colmenare+

EO89D ---------2MT 25M 265 22? 2MM 22>

SUK" alter +e++0on +et nl+G+ort - +pan0+hGm( SUK" 7 ) 5 6 +elect employeeG0d Emp9D% la+tGname .Ka+t Lame. from employee+ where la+tGname l0Ne #C=# order 1y la+tGname( Ka+t Lame ------------------------Ca1r0o Cam1rault Cam1rault Colmenare+ Chen Chung

EO89D ---------2MT 25M 265 22> 22? 2MM

Orac%e Error 8e''a"e'


The LKSGKALWUAWE !arame$er also con$rols $he language o5 $he da$abase error messages be#ng re$urned 5rom $he da$abase. 3e$$#ng $h#s !arame$er !r#or $o subm#$$#ng "our 3K. s$a$emen$ ensures $ha$ $he language?s!ec#5#c da$abase error messages *#ll be re$urned $o $he a!!l#ca$#on. ons#der $he 5ollo*#ng ser'er message(
BRA-??>57: ta1le or /0ew doe+ not e<0+t

0hen $he LKSGKALWUAWE !arame$er #s se$ $o *rench, $he ser'er message a!!ears as 5ollo*s(
BRA-??>57: ta1le ou /ue 0ne<0+tante

2or more d#scuss#on o5 global#1a$#on su!!or$ 5ea$ures #n Oracle Da$abase N@, see /or5ing in a Global Environment #n $he Oracle Database Express Edition 2 Day Developer Guide.

243

G&o*a&i?ation

244

CHAPTER 1-

TESTIN! PHP AND THE OCI0 E7TENSION


Th#s ha!$er d#scusses #ns$all#ng and runn#ng $he PHP source code $es$s 5or O >8 on .#nu/. The PHP source code #ncludes $es$s 5or all $he core 5unc$#onal#$" and e/$ens#ons. Hou should run $he $es$s a5$er bu#ld#ng PHP on .#nu/. Hou should also 'er#5" "our a!!l#ca$#ons *or+ correc$l" *#$h an" ne* PHP b#nar" be5ore !u$$#ng #$ #n$o !roduc$#on. Th#s g#'es load and real?l#5e $es$#ng no$ !oss#ble *#$h PHPLs command?l#ne $es$ su#$e. ons#der con$r#bu$#ng ne* $es$s $o $he PHP commun#$". &dd#ng $es$s $ha$ are rele'an$ $o "our a!!l#ca$#on reduces $he r#s+s o5 PHP de'elo!ers brea+#ng PHP 5ea$ures #m!or$an$ $o "ou. Please send ne* $es$s or re!or$ #ssues *#$h PHPLs $es$ su#$e $o php-FaPl0+t+.php.net. >$ #s also a good #dea $o !ro?ac$#'el" $es$ "our a!!l#ca$#ons *#$h PHP Mrelease cand#da$esO and sna!sho$s, http://+nap+.php.net. Please re!or$ !roblems so $he" can be 5#/ed be5ore each 5#nal PHP release. Th#s ensures PHP con$#nues do#ng *ha$ "ou need.

Runnin" OCI0 Te't'


The $es$s #n php+,%2%-<ext<oci*<tests 'er#5" $he beha'#or o5 $he O >8 e/$ens#on. 2or $he $es$s $o run success5ull" some con5#gura$#on #s needed. To run $he O >8 $es$s( 1. 3e$ $he Oracle connec$#on de$a#ls, e#$her b" ed#$#ng $he details%inc $es$ con5#gura$#on 5#le, or b" se$$#ng en'#ronmen$ 'ar#ables. To change $he con5#gura$#on 5#le( @d#$ php+5.2.7Iext<oci*<tests<details%inc and se$ $he Oracle system user !ass*ord 5or "our da$abase(
,u+er - .+y+tem.( ,pa++word - .+y+tempwd.(

>n older 'ers#ons o5 PHP $hese 'ar#ables are loca$ed #n connect%inc. The $es$s rel" on be#ng able $o crea$e $ables, $"!es, s$ored !rocedures, and so on. >5 "ou change ,u+er, "ou ma" ha'e $o gran$ $ha$ da$abase user e/$ra !r#'#leges. &$ $he end o5 details%inc, se$ $he connec$#on s$r#ng 5or $he da$abase(
,d1a+e - .localho+t/IE.(

>5 PHP #s runn#ng on $he same mach#ne as $he da$abase, $hen also se$(
,oracleGonGlocalho+t - TRUE(

Th#s s!ec#5#es $o $es$ 5unc$#onal#$" *here $he Oracle da$abase d#rec$l" accesses 5#les crea$ed b" PHP. >5 $he da$abase and PHP are no$ us#ng $he same 5#le s"s$em, $h#s #s no$ !oss#ble and $he 'ar#able should be le5$ *AKSE.

245

-e#ting PHP and the O(I 'xten#ion

2#nall", *#$h O >8 1.P, and "ou are us#ng Oracle Da$abase 11g onnec$#on Pool#ng, se$(
,te+tGdrcp - TRUE

To use DR P, $he !ool mus$ be enabled and $he connec$#on s$r#ng mus$ s!ec#5" $ha$ a !ooled da$abase ser'er should be used. &l$erna$#'el", *#$h PHP 5.2.E on*ards, en'#ronmen$ 'ar#ables can be se$ #ns$ead o5 ed#$#ng details%inc. &$ $he shell, se$ $hese en'#ronmen$ 'ar#ables.
, , , , , e<port e<port e<port e<port e<port 8$8GBC9MGTESTGUSER-+y+tem 8$8GBC9MGTESTG8ASS-+y+tempwd 8$8GBC9MGTESTGDB-localho+t/IE 8$8GBC9MGTESTGDBGBLGKBCAK$BST-TRUE 8$8GBC9MGTESTGDRC8-*AKSE

The 'ar#ables corres!ond $o $he se$$#ngs descr#bed abo'e #n sec$#on 1. 2. hec+ $ha$ /ar0a1le+Gorder has E #n "our php%ini, 5or e/am!le(
/ar0a1le+Gorder - .EW8CS.

0#$hou$ $h#s 5lag, $he Oracle en'#ronmen$ 'ar#ables are no$ !ro!aga$ed $hrough $he $es$ s"s$em and $es$s 5a#l $o connec$. P. 3e$ an" necessar" Oracle en'#ronmen$ 'ar#ables #n "our shell. 2or e/am!le, 5or PHP l#n+ed *#$h Oracle Da$abase N@ en$er(
, e<port B$-/u+r/l01/oracle/<e/app/oracle/product/2?.7.?/+er/er , . ,B$/10n/oracleGen/.+h

7o$e $he s!ace a5$er $he 5ull s$o!. 2or o$her da$abase 'ers#ons run oraen/ and en$er $he #den$#5#er o5 "our da$abase(
, . /u+r/local/10n/oraen/ BRACKEGS9D - 34 ! orcl

>5 Oracle #s on a d#55eren$ mach#ne, "ou ma" manuall" need $o se$ $he en'#ronmen$ 'ar#ables se$ b" $hese scr#!$s. E. Run PHPLs $es$ su#$e *#$h(
, cd php-6.7.T , maNe te+t

>5 "ou *an$ $o run 8us$ $he O >8 $es$s use(


, maNe te+t TESTS-e<t/oc0M

@ach $es$ scr#!$ #s e/ecu$ed and #$s s$a$us re!or$ed.


-------------------------------------------------------------246

!unning O(I -e#t# 8$8 : /home/myhome/php-6.7.T/+ap0/cl0/php 8$8GSA89 : cl0 8$8GVERS9BL : 6.7.T ]ELDGVERS9BL: 7.7.? 8$8GBS : K0nu< - K0nu< def 7.S.75-2>-gener0c 9L9 actual : /u+r/local/apache/conf/php.0n0 Oore .9L9+ : C&D : /home/myhome/php-6.7.T E<tra d0r+ : -------------------------------------------------------------Runn0ng +elected te+t+. 8ASS oc0G10ndGarrayG1yGname:; and 0n/al0d /alue+ 2 3arrayG10ndG??2.phpt4 8ASS oc0G10ndGarrayG1yGname:; and 0n/al0d /alue+ 7 3arrayG10ndG??7.phpt4 8ASS oc0G10ndGarrayG1yGname:; and 0n/al0d /alue+ ) 3arrayG10ndG??).phpt4 ...

3uccess5ul $es$s beg#n *#$h 8ASS. Tes$s $ha$ are $o be s+#!!ed #n $he curren$ con5#gura$#on are mar+ed SC98. 2a#l#ng $es$s are mar+ed *A9K. & summar" o5 $he 5a#l#ng $es$s #s g#'en a$ $he com!le$#on o5 $he $es$s.

Runnin" a Sin"%e Te't


To run onl" one or $*o $es$s, call $he run+tests%php scr#!$ d#rec$l" and !ass $he $es$ names as !arame$ers. 2or e/am!le, $o run $he demotest%phpt scr#!$, do $he 5ollo*#ng(
, e<port TESTG8$8GEIECUTABKE-/home/myhome/php-6.7.T/+ap0/cl0/php , /home/myhome/php-6.7.T/+ap0/cl0/php run-te+t+.php E " e<t/oc0M/te+t+/demote+t.phpt

The TESTG8$8GEIECUTABKE 'ar#able con$a#ns $he PHP b#nar" *#$h *h#ch $o $es$ demotest%phpt. >n $he e/am!le, $he same PHP b#nar" #s used $o run $he con$roll#ng run+tests%php scr#!$, bu$ $he" could be d#55eren$ e/ecu$ables. The $es$ ou$!u$ #s s#m#lar $o $he !re'#ous ou$!u$.

Te't' that =ai%


The ou$!u$ o5 5a#l#ng $es$s #s +e!$ 5or anal"s#s. 2or e/am!le, #5 ext<oci*<tests<demotest%phpt 5a#ls, $he 5ollo*#ng 5#les *#ll be #n php+,%2%-<ext<oci*<tests(

#able !?" #est $iles and their contents%


=i%e na4e demotest%phpt demotest%php demotest%out demotest%exp demotest%di$$ demotest%log =i%e Content' Tes$ 5rame*or+ scr#!$ PHP 5#le e/ecu$ed Tes$ ou$!u$ @/!ec$ed ou$!u$ as coded #n $he %phpt 5#le D#55erence be$*een ac$ual and e/!ec$ed ou$!u$ &c$ual and e/!ec$ed ou$!u$ #n a s#ngle 5#le

247

-e#ting PHP and the O(I 'xten#ion

Occas#onall" a 5e* $es$s are +no*n $o 5a#l. These m#gh$ be 5or un5#/ed bugs, or *here $he PHP $es$ #n5ras$ruc$ure doesnG$ !rac$#call" allo* $es$s $o acce!$ d#55erences #n 'ers#ons o5 Oracle. >5 "ou use $he la$es$ O >8 e/$ens#on *#$h an older 'ers#on o5 PHP, d#55erences #n PHPGs /arGdump:; ou$!u$ *#ll ma+e $es$s a!!ear $o 5a#l.

Creatin" OCI0 Te't'


To add a ne* O >8 $es$, crea$e a phpt 5#le #n php+,%2%-<ext<oci*<tests us#ng $he $es$ 5#le 5orma$. 0hen "ou run maNe te+t $he ne* 5#le #s au$oma$#call" run. 2or e/am!le, crea$e demotest%phpt( Script ?2" demotest%phpt
--TEST-Demo to te+t the Te+t +y+tem --SC989*-!php 0f :'e<ten+0onGloaded:#oc0M#;; d0e:.+N0p no oc0M e<ten+0on.;( !" --*9KE-!php reFu0re d0rname:GG*9KEGG;.#/connect.0nc#( ,+ - oc0Gpar+e:,c% .+elect u+er from dual.;( oc0Ge<ecute:,+;( oc0GfetchGall:,+% ,re+;( /arGdump:,re+;( echo .DoneEn.( !" ---DBLE--!php e<0t:?;( !" --EI8ECT-array:2; @ 3.USER.4-" array:2; @ 3?4-" +tr0ng:S; .SJSTEO. A A ---DBLE---

The $es$ beg#ns *#$h a commen$ $ha$ #s d#s!la"ed *hen $he $es$ runs. The SC989* sec$#on causes $he $es$ $o be s+#!!ed *hen $he O >8 e/$ens#on #s no$ enabled #n PHP. The *9KE sec$#on #s $he PHP code $o be e/ecu$ed. The EI8ECT sec$#on has $he e/!ec$ed ou$!u$. The 5#le connect%inc #s 5ound #n php+,%2%-<ext<oci*<tests<connect%inc. >$ #ncludes details%inc7 connec$s $o Oracle, and re$urns $he connec$#on resource #n ,c. >n PHP 5.P, "ou can use PHPGs #nbu#l$ cons$an$ GGD9RGG #ns$ead o5 d0rname:GG*9KEGG; $o loca$e $he correc$ #ncluded 5#les, bu$ $h#s *#ll l#m#$ $es$ com!a$#b#l#$". The l#ne ---DBLE--- #s ou$s#de $he e/ecu$ed scr#!$ and #s echoed 'erba$#m, 'er#5"#ng $ha$ $he scr#!$ com!le$ed. The e/$ra PHP bloc+ con$a#n#ng e<0t:?; ma+es runn#ng $he $es$ d#rec$l" #n PHP a l#$$le cleaner. 3ome o5 $he PHPT con$en$ #s sho*n, bu$ onl" $he ac$ual, and no$ $he e/!ec$ed ou$!u$ #s d#s!la"ed. Th#s can ma+e #$ eas#er $o =u#c+l" 'al#da$e $es$s(
24

(reating O(I -e#t# , php demotest.phpt --TEST-Demo to te+t the Te+t +y+tem --SC989*---*9KE-array:2; @ 3.USER.4-" array:2; @ 3?4-" +tr0ng:S; .SJSTEO. A A Done ---DBLE---

The !age /riting #ests a$ http://Fa.php.net/wr0te-te+t.php sho*s o$her sec$#ons a $es$ 5#le can ha'e, #nclud#ng *a"s $o se$ argumen$s and php%ini !arame$ers. Th#s !age also has gener#c e/am!les and hel!5ul #n5orma$#on on *r#$#ng $es$s. 0r#$#ng good un#$s $es$s #s an ar$. Ma+#ng a $es$ !or$able, accura$e, s#m!le and sel5?d#agnos#ng re=u#res 5#ne 8udgmen$ and $#d" !rogramm#ng. &!!l#ca$#on le'el $es$#ng br#ngs e'en more challenges. There are se'eral PHP $es$ 5rame*or+s *#$h so!h#s$#ca$ed 5ea$ures $ha$ m#gh$ be more su#$ed $o "our a!!l#ca$#on $es$ su#$es. The" #nclude PHP>nit and Simple#est.

OCI0 Te't He%per Script'


&long *#$h connect%inc and details%inc, $here are se'eral use5ul scr#!$s #n php+,%2%-<ext<oci*<tests 5or crea$#ng and dro!!#ng bas#c $ables and $"!es(

create8table%inc create8type%inc drop8table%inc drop8type%inc

@ach $es$ should crea$e an" ob8ec$s #$ needs and dro! $hem a$ $he end o5 $he $es$.

Con2i"urin" the Data)a'e =or Te'tin"


3ome$#mes #$ #s !oss#ble 5or ra!#dl" e/ecu$#ng O >8 $es$ scr#!$s $o 5lood $he da$abase *#$h connec$#ons. Th#s ma" be no$#ceable *#$h Oracle Da$abase N@, *h#ch has smaller de5aul$s. Random $es$s 5a#l *#$h errors l#+e $he 5ollo*#ng(
BRA-2762S TLS:l0+tener could not f0nd a/a0la1le handler w0th match0ng protocol +tacN BRA-2767?: TLS:l0+tener could not f0nd a/a0la1le handler for reFue+ted type of +er/er

The solu$#on #s $o con5#gure $he da$abase $o su#$ $he load. 2or $he errors abo'e, $he number o5 M!rocessesO $ha$ Oracle can handle needs $o be #ncreased.

24$

-e#ting PHP and the O(I 'xten#ion

To #ncrease $he number o5 !rocesses #n Oracle( 1. Hou ma" need $o +u as $he oracle user so "ou ha'e o!era$#ng s"s$em !r#'#leges $o s$ar$ 3K.TPlus(
, +u [ oracle 8a++word:

2.

3e$ $he Oracle en'#ronmen$ 'ar#ables needed b" 3K.TPlus, 5or e/am!le(
, e<port BRACKEG$BOE-/u+r/l01/oracle/<e/app/oracle/product/2?.7.?/+er/er , . ,BRACKEG$BOE/10n/oracleGen/.+h

P.

Use 3K.TPlus $o connec$ as a !r#'#leged da$abase user(


, +Flplu+ / a+ +y+d1a

E.

hec+ $he curren$ 'alue o5 !rocesses us#ng $he S$B& 8ARAOETER 8RBCESSES command(
SUK" show parameter processes LAOE TJ8E VAKUE ------------------------- ----------- ----------... proce++e+ 0nteger 5?

5.

>ncrease $he 'alue $o, sa", 2??(


SUK" a(ter system set processes:0++ scope:sp)i(e' Sy+tem altered.

F.

Res$ar$ $he da$abase us#ng $he S$UTDB&L 9OOED9ATE, 5ollo*ed b" $he STARTU8 command(
SUK" shutdown immediate Data1a+e clo+ed. Data1a+e d0+mounted. BRACKE 0n+tance +hut down. SUK" startup BRACKE 0n+tance +tarted. Total Sy+tem Wlo1al Area 7M>5?S>TS 1yte+ *0<ed S0Qe 276M5MM 1yte+ Var0a1le S0Qe >S5T7?T7 1yte+ Data1a+e Buffer+ 2MMT5)SM? 1yte+ Redo Buffer+7>)7T)S 1yte+ Data1a+e mounted. Data1a+e opened.

C.

Use $he S$B& 8ARAOETER 8RBCESSES command $o con5#rm $he ne* 'alue #s #n e55ec$(
SUK" show parameter processes LAOE TJ8E VAKUE ------------------------- ----------- ---------... proce++e+ 0nteger 2??

25%

(on.iguring the )ata*a#e :or -e#ting

8.

@/#$ 3K.TPlus us#ng $he EI9T command(


SUK" e<it

D.

7o* $he $es$s can be run aga#n(


, maNe te+t TESTS-e<t/oc0M

251

-e#ting PHP and the O(I 'xten#ion

252

APPENDI7 A

TRACIN! OCI0 INTERNALS


Th#s &!!end#/ d#scusses $rac#ng $he O >8 #n$ernals. To see e/ac$l" *ha$ calls $o $he Oracle da$abase $he O >8 e/$ens#on ma+es, "ou can $urn on debugg#ng ou$!u$. Th#s #s mos$l" use5ul 5or $he ma#n$a#ners o5 $he O >8 e/$ens#on.

Ena)%in" OCI0 De)u""in" output


Trac#ng can be $urned on #n "our scr#!$ *#$h oc0G0nternalGde1ug:;. 2or a scr#!$ $ha$ connec$s and does an #nser$( Script ?!" trace%php
!php oci_interna(_debu,$0;( // turn on trac0ng

,conn - oc0Gconnect:.hr.% .hrpwd.% .localho+t/IE.;( ,+ - oc0Gpar+e:,conn% .0n+ert 0nto te+tta1le /alue+ :#my data#;.;( oc0Ge<ecute:,+% BC9GDE*AUKT;( // do not auto-comm0t !"

Hou ge$ ou$!u$ l#+e(


BC9M BC9M BC9M BC9M BC9M BC9M BC9M BC9M BC9M BC9M BC9M BC9M BC9M BC9M BC9M BC9M BC9M BC9M BC9M BC9M BC9M BC9M BC9M DEBUW: BC9Ll+En/0ronmentVar0a1leWet at :/php/e<t/oc0M/oc0M.c:2M77; DEBUW K2: Wot LB cached connect0on at :/php/e<t/oc0M/oc0M.c:2MST; DEBUW: BC9En/Ll+Create at :/php/e<t/oc0M/oc0M.c:7TT7; DEBUW: BC9$andleAlloc at :/php/e<t/oc0M/oc0M.c:7S)7; DEBUW: BC9$andleAlloc at :/php/e<t/oc0M/oc0M.c:7S55; DEBUW: BC9Se++0on8oolCreate at :/php/e<t/oc0M/oc0M.c:7SS7; DEBUW: BC9AttrSet at :/php/e<t/oc0M/oc0M.c:7ST5; DEBUW K2: createG+pool: :?<M>6>1T?; at :/php/e<t/oc0M/oc0M.c:7S>?; DEBUW K2: u+0ng +hared pool: :?<M>6>1T?; at :/php/e<t/oc0M/oc0M.c:7>T7; DEBUW: BC9$andleAlloc at :/php/e<t/oc0M/oc0M.c:7>M); DEBUW: BC9$andleAlloc at :/php/e<t/oc0M/oc0M.c:7>>); DEBUW: BC9AttrSet at :/php/e<t/oc0M/oc0M.c:)??7; DEBUW: BC9AttrSet at :/php/e<t/oc0M/oc0M.c:)?25; DEBUW: BC9AttrWet at :/php/e<t/oc0M/oc0M.c:)?7S; DEBUW: BC9AttrWet at :/php/e<t/oc0M/oc0M.c:)?7T; DEBUW K2: :numopen-?;:num1u+y-?; at :/php/e<t/oc0M/oc0M.c:)?7>; DEBUW: BC9Se++0onWet at :/php/e<t/oc0M/oc0M.c:)?5?; DEBUW: BC9AttrWet at :/php/e<t/oc0M/oc0M.c:)?66; DEBUW: BC9AttrWet at :/php/e<t/oc0M/oc0M.c:)?6T; DEBUW: BC9Conte<tWetValue at :/php/e<t/oc0M/oc0M.c:)?6>; DEBUW: BC9Conte<tWetValue at :/php/e<t/oc0M/oc0M.c:)265; DEBUW: BC9OemoryAlloc at :/php/e<t/oc0M/oc0M.c:)2S2; DEBUW: BC9Conte<tSetValue at :/php/e<t/oc0M/oc0M.c:)2T6; 253

-racing O(I Interna&# BC9M DEBUW: BC9AttrSet at :/php/e<t/oc0M/oc0M.c:)?M6; BC9M DEBUW K2: Lew Lon-8er+0+tent Connect0on addre++: :?<MMe?adc; at :/php/e<t/oc0M/oc0M.c:7?>); BC9M DEBUW K2: numGper+0+tent-:?;% numGl0nN+-:2; at :/php/e<t/oc0M/oc0M.c:7?>6; BC9M DEBUW: BC9$andleAlloc at :/php/e<t/oc0M/oc0MG+tatement.c:6T; BC9M DEBUW: BC9Stmt8repare7 at :/php/e<t/oc0M/oc0MG+tatement.c:T7; BC9M DEBUW: BC9AttrSet at :/php/e<t/oc0M/oc0MG+tatement.c:277; BC9M DEBUW: BC9AttrWet at :/php/e<t/oc0M/oc0MG+tatement.c:)>S; BC9M DEBUW: BC9StmtE<ecute at :/php/e<t/oc0M/oc0MG+tatement.c:57?; BC9M DEBUW: BC9StmtRelea+e at :/php/e<t/oc0M/oc0MG+tatement.c:T7); BC9M DEBUW: BC9$andle*ree at :/php/e<t/oc0M/oc0MG+tatement.c:T)2; BC9M DEBUW: BC9Tran+Roll1acN at :/php/e<t/oc0M/oc0M.c:72ST; BC9M DEBUW: BC9Se++0onRelea+e at :/php/e<t/oc0M/oc0M.c:7))?; BC9M DEBUW: BC9$andle*ree at :/php/e<t/oc0M/oc0M.c:7725; BC9M DEBUW: BC9$andle*ree at :/php/e<t/oc0M/oc0M.c:772T; BC9M DEBUW: BC9Se++0on8oolDe+troy at :/php/e<t/oc0M/oc0M.c:)277; BC9M DEBUW: BC9$andle*ree at :/php/e<t/oc0M/oc0M.c:)27S; BC9M DEBUW: BC9$andle*ree at :/php/e<t/oc0M/oc0M.c:)2)?; BC9M DEBUW: BC9$andle*ree at :/php/e<t/oc0M/oc0M.c:)2)5; BC9M DEBUW: BC9$andle*ree at :/php/e<t/oc0M/oc0M.c:2225; BC9M DEBUW: BC9$andle*ree at :/php/e<t/oc0M/oc0M.c:222>;

Man" o5 $hese calls 8us$ alloca$e local resources :handles; and se$ local s$a$e :attributes;, bu$ some re=u#re a round trip $o $he da$abase. One o5 $hese #s $he BC9Tran+Roll1acN:; call near $he end o5 $he scr#!$. The BC9GDE*AUKT 5lag sa#d no$ $o au$o?comm#$ and $here *as no e/!l#c#$ oc0Gcomm0t:; call. &s !ar$ o5 PHPLs end o5 HTTP re=ues$ shu$do*n a$ $he conclus#on o5 $he scr#!$, $he rollbac+ *as #ssued. >5 "ou change $o au$o?comm#$ mode "ou *#ll no$ see a call $o BC9Tran+Comm0t:; because $he comm#$ message #s !#gg"?bac+ed *#$h OracleGs s$a$emen$ e/ecu$#on call, $hus sa'#ng a round?$r#!. >5 a scr#!$ onl" #nser$s one ro* #$ #s 5#ne $o au$o?comm#$. O$her*#se, do $he $ransac$#on managemen$ "oursel5.

254

APPENDI7 <

OCI0 PHP.INI PARA8ETERS


Th#s &!!end#/ l#s$s $he php%ini !arame$ers 5or $he O >8 e/$ens#on. D#scuss#on o5 $he#r use #s co'ered #n !re'#ous cha!$ers. The !arame$er 'alues can be changed #n $he PHP con5#gura$#on 5#le php%ini, 5or e/am!le(
oc0M.defaultGprefetch - T6

,ar#ables can also be se$ #n httpd%con$(


9fOodule modGphp6.c" phpGadm0nGflag oc0M.oldGoc0Gclo+eG+emant0c+ Bn phpGadm0nG/alue oc0M.connect0onGcla++ OJ8$8A88 /9fOodule"

The *eb ser'er mus$ be res$ar$ed 5or an" changes $o $a+e e55ec$. The loca$#on o5 php%ini and $he curren$ 'alues o5 $he O >8 !arame$ers can be 5ound b" runn#ng $he command l#ne PHP e/ecu$able *#$h $he -0 o!$#on, or b" load#ng $h#s scr#!$ #n a bro*ser( Script ?2" phpin$o%php
!php php0nfo:;( !"

>5 "ou are us#ng 0#ndo*s ,#s$a, remember $o ed#$ php%ini us#ng adm#n#s$ra$#'e !r#'#leges.

#able 22" O()* php%ini parameters


Na4e oc0M.connect0onGcla++ De2au%t null @a%id Ran"e De'cription & shor$ s$r#ng & user?chosen name 5or Oracle Da$abase 11g onnec$#on Pool#ng :DR P;. >n general, use $he same name 5or all *eb ser'ers runn#ng $he same a!!l#ca$#on. an also be se$ *#$h 0n0G+et:;. >n$roduced #n O >8 1.P.

255

O(I "h"@ini Para7eter#

Na4e oc0M.e/ent+

De2au%t Bff

@a%id Ran"e De'cription


Bff or Bn

&llo*s PHP $o rece#'e 2as$ &!!l#ca$#on 7o$#5#ca$#on :2&7; e'en$s 5rom Oracle $o g#'e #mmed#a$e no$#5#ca$#on o5 a da$abase node or ne$*or+ 5a#lure. The da$abase mus$ be con5#gured $o !os$ e'en$s. >n$roduced #n O >8 1.P. Ma/#mum number o5 !ers#s$en$ connec$#ons each PHP !rocess caches. Toggles *he$her oc0Gclo+e:; uses $he old beha'#or, *h#ch *as a Mno?o!O.

oc0M.ma<Gper+0+tent

-2

S[ ?1
-2 means no

l#m#$ oc0M.oldGoc0Gclo+eG+emant0c+ oc0M.per+0+tentGt0meout Bff -2

Bff or Bn

S ?1

Ho* man" seconds a !ers#s$en$ connec$#on #s allo*ed $o rema#n #dle -2 means no be5ore be#ng $erm#na$ed. $#meou$ S[ 0 Ho* man" seconds a !ers#s$en$ connec$#on can be unused be5ore an -2 means no e/$ra chec+ a$ connec$#on 'er#5#es $he e/$ra da$abase connec$#on #s s$#ll 'al#d. chec+#ng
Bff or Bn

oc0M.p0ngG0nter/al

S?

oc0M.pr0/0legedGconnect oc0M.+tatementGcacheG+0Qe

Bff 7?

Toggles *he$her 3H3D-& and 3H3OP@R connec$#ons are !erm#$$ed. >m!ro'es da$abase !er5ormance b" cach#ng $he g#'en number o5 3K. s$a$emen$s #n PHP.

S0

256

APPENDI7 C

OCI0 =,NCTION NA8ES IN PHP ( AND PHP #


>n PHP 5 se'eral e/$ens#ons #nclud#ng O >8 under*en$ name s$andard#1a$#on. PHP E 5unc$#ons l#+e BC9Kog0n:; became oc0Gconnect:;, BC98ar+e:; became oc0Gpar+e:;, and so on. The old O >8 names s$#ll e/#s$ as al#ases, so PHP E scr#!$s do no$ necessar#l" need $o be changed. P@ . O >8 releases 5rom 1.1 on*ards ha'e $he name s$andard#1a$#on change.

Note: The +C*4 -.A e>tension builds and runs with #)# 7 too. *f you are usin& #)# 7 and cannot up&rade to #)# 6, you should replace the +C*4 code with the new version to &et improved stability and performance optimi2ations. ;teps to do this are &iven in this boo!.

One s#de e55ec$ o5 renam#ng $he 5unc$#on names #s $ha$ user con$r#bu$ed commen$s #n $he PHP manual :http://www.php.net/oc0M; are 5ound #n $*o d#55eren$ !laces. The commen$s are e#$her on $he !age 5or $he old s"n$a/, or on $he !age 5or $he ne* s"n$a/. 0here a 5unc$#on has an al#as, chec+ bo$h manual !ages 5or user $#!s. 2unc$#on names #n PHP are case #nsens#$#'e and #$ #s common $o see $he PHP E names *r#$$en *#$h a ca!#$al#1ed !re5#/. Table 21 sho*s $he O >8 5unc$#on names #n PHP E and PHP 5 *here names or 5unc$#onal#$" d#55er.

#able 2!" Relationship bet&een O()*Is PHP 0 and PHP , $unction names%
Operation onnec$#on Action O!en connec$#on O!en ne* connec$#on Pers#s$en$ connec$#on lose connec$#on ursor O!en cursor lose cursor PHP ( Na4e oc0logon:; oc0nlogon:; oc0plogon:; oc0logoff:; oc0newcur+or:; oc0freecur+or:; oc0free+tatement:; PHP # Na4e oc0Gconnect:; oc0GnewGconnect:; oc0Gpconnect:; :and ne* php%ini !arame$ers; oc0Gclo+e:; oc0GnewGcur+or:; oc0GfreeG+tatement:;

257

O(I :unction 6a7e# in PHP 4 and PHP 5

Operation Pars#ng -#nd#ng

Action Parse s$a$emen$

PHP ( Na4e oc0par+e:;

PHP # Na4e oc0Gpar+e:; oc0G10ndG1yGname:; oc0G10ndGarrayG1yGname:; oc0Gdef0neG1yGname:;

-#nd 'ar#able oc010nd1yname:; -#nd arra" 7o$ a'a#lable oc0def0ne1yname:;

De5#n#ng

De5#ne ou$!u$ 'ar#ables @/ecu$e s$a$emen$ 2e$ch ro* 2e$ch ro*

@/ecu$#on 2e$ch#ng

oc0e<ecute:; oc0fetch:; oc0fetch0nto:;

oc0Ge<ecute:; oc0Gfetch:; oc0GfetchGarray:; oc0GfetchGrow:; oc0GfetchGa++oc:; oc0GfetchGo1Zect:;

2e$ch all ro*s oc0fetch+tatement:; 2e$ch column oc0re+ult:; >s $he column oc0column0+null:; 7U..Y ancel 2e$ch Transac$#on Managemen$ omm#$ Rollbac+ Descr#!$ors oc0cancel:; oc0comm0t:; oc0roll1acN:; oc0newde+cr0ptor:; oc0freede+c:; @rror Handl#ng oc0error:;

oc0GfetchGall:; oc0Gre+ult:; oc0Gf0eldG0+Gnull:; oc0Gcancel:; oc0Gcomm0t:; oc0Groll1acN:; oc0GnewGde+cr0ptor:; oc0GfreeGde+cr0ptor:; oc0Gerror:;

25

O(I :unction 6a7e# in PHP 4 and PHP 5

Operation .ong Ob8ec$s :.O-s; 7o$e( Me$hods on a collec$#on ob8ec$ can be used #n add#$#on $o $hese 5unc$#ons.

Action

PHP ( Na4e oc0+a/elo1:;

PHP # Na4e oc0Glo1G+a/e:;

oc0+a/elo1f0le:; oc0wr0telo1tof0le:;

oc0Glo1G0mport:; oc0Glo1Ge<port:;

oc0wr0tetemporarylo1:; BC9-Ko1-"wr0teTemporary:; oc0loadlo1:; oc0clo+elo1:; ollec$#ons 7o$e( Me$hods on a collec$#on ob8ec$ can be used #n add#$#on $o $hese 5unc$#ons. oc0freecollect0on:; oc0collappend:; oc0collgetelem:; oc0colla++0gn:; oc0colla++0gnelem:; oc0coll+0Qe:; oc0collma<:; oc0colltr0m:; Me$ada$a 3$a$emen$ $"!e oc0+tatementtype:; oc0GfreeGcollect0on:; oc0Gcollect0onGappend:; oc0Gcollect0onGelementGget:; BC9-Collect0on-"a++0gn:; oc0Gcollect0onGelementGa++0gn:; oc0Gcollect0onG+0Qe:; oc0Gcollect0onGma<:; oc0Gcollect0onGtr0m:; oc0G+tatementGtype:; oc0newcollect0on:; oc0Glo1Gload:; BC9-Ko1-"clo+e:; oc0GnewGcollect0on:;

25$

O(I :unction 6a7e# in PHP 4 and PHP 5

Operation

Action

PHP ( Na4e

PHP # Na4e oc0Gf0eldGname:; oc0Gf0eldG+0Qe:; oc0Gf0eldGtype:; oc0Gf0eldGtypeGraw:; oc0Gf0eldGprec0+0on:; oc0Gf0eldG+cale:; oc0GnumGrow+:; oc0GnumGf0eld+:;

oc0columnname:; 7ame o5 resul$ column 3#1e o5 resul$ column oc0column+0Qe:;

oc0columntype:; Da$a$"!e o5 resul$ column oc0columntyperaw:; Da$a$"!e o5 resul$ column oc0columnprec0+0on:; Prec#s#on o5 resul$ column 3cale o5 resul$ oc0column+cale:; column oc0rowcount:; 7umber o5 ro*s a55ec$ed 7umber o5 columns re$urned hang#ng Pass*ord Trac#ng 3er'er ,ers#on Tun#ng oc0numcol+:;

oc0pa++wordchange:; oc00nternalde1ug:; oc0+er/er/er+0on:; oc0+etprefetch:;

oc0Gpa++wordGchange:; oc0G0nternalGde1ug:; oc0G+er/erG/er+0on:; oc0G+etGprefetch:; :and ne* php%ini !arame$er;

26%

APPENDI7 D

THE O<SOLETE ORACLE E7TENSION


Th#s &!!end#/ d#scusses $he obsole$e Oracle PHP e/$ens#on. ,er" rarel" "ou m#gh$ come across PHP scr#!$s $ha$ use $he earl" Oracle e/$ens#on. Th#s e/$ens#on #s obsole$e and #s no longer #ncluded *#$h PHP. The 5unc$#onal#$" #$ o55ered *as l#m#$ed, and u!grad#ng $o $he ne* O >8 e/$ens#on m#gh$ be as s#m!le as enabl#ng $he ne*er O >8 e/$ens#on #n $he PHP b#nar", and chang#ng $he oraG 5unc$#on calls #n "our scr#!$s. Pa"#ng some a$$en$#on $o $ransac$#on managemen$ and connec$#on handl#ng #s *#se $o ma+e sure all "our da$a #s comm#$$ed *hen "ou e/!ec$ #$ $o be. Th#s ha!$er g#'es a com!ar#son o5 $he obsole$ed Oracle PHP e/$ens#on and $he curren$ O >8 e/$ens#on.

Orac%e and OCI0 Co4pari'on


Table 22 sho*s $he general rela$#onsh#! be$*een $he obsole$e and curren$ e/$ens#ons.

#able 22" Relationship bet&een the O()* and the obsolete Oracle extensions%
Operation onnec$#on Action ORA 2unction Ho)'o%eteI OCI0 2unction oc0Gconnect:; oc0GnewGconnect:; oc0Gpconnect:; :and ne* php%ini !arame$ers; oc0Gclo+e:; oc0GnewGcur+or:; oc0GfreeG+tatement:; oc0Gpar+e:; oc0G10ndG1yGname:; oc0G10ndGarrayG1yGname:; oc0Ge<ecute:; O!en connec$#on oraGlogon:; O!en ne* connec$#on Pers#s$en$ connec$#on 7o$ a'a#lable oraGplogon:;

lose connec$#on oraGlogoff:; ursor O!en cursor lose cursor Pars#ng -#nd#ng Parse s$a$emen$ -#nd 'ar#able -#nd arra" @/ecu$#on @/ecu$e s$a$emen$ oraGopen:; oraGclo+e:; oraGpar+e:; oraG10nd:; 7o$ a!!l#cable oraGe<ec:;

261

-he O*#o&ete Orac&e 'xten#ion

Operation

Action Pre!are, e/ecu$e and 5e$ch

ORA 2unction Ho)'o%eteI oraGdo:;

OCI0 2unction oc0Gpar+e:; oc0Ge<ecute:; 2ollo*ed b" one o5( oc0GfetchGall:; oc0GfetchGarray:; oc0GfetchGa++oc:; oc0GfetchGo1Zect:; oc0GfetchGrow:; oc0Gfetch:;

2e$ch#ng

2e$ch ro* 2e$ch ro*

oraGfetch:; oraGfetchG0nto

oc0Gfetch:; oc0GfetchGarray:; oc0GfetchGrow:; oc0GfetchGa++oc:; oc0GfetchGo1Zect:;

2e$ch all ro*s 2e$ch column >s $he column 7U..Y ancel 2e$ch Transac$#on Managemen$ omm#$ omm#$ mode Rollbac+ @rror Handl#ng

7o$ a!!l#cable oraGgetcolumn:; 7o$ a!!l#cable 7o$ a!!l#cable oraGcomm0t:; oraGcomm0ton:; oraGcomm0toff:; oraGroll1acN:; oraGerror:; oraGerrorcode:;

oc0GfetchGall:; oc0Gre+ult:; oc0Gf0eldG0+Gnull:; oc0Gcancel:; oc0Gcomm0t:; Pass BC9GDE*AUKT 5lag $o oc0Ge<ecute:; oc0Groll1acN:; oc0Gerror:;

262

Orac&e and O(I (o7"ari#on

Operation .ong Ob8ec$s :.O-3;

Action

ORA 2unction Ho)'o%eteI 7o$ a!!l#cable

OCI0 2unction BC9-Ko1-"append BC9-Ko1-"clo+e BC9-Ko1-"eof BC9-Ko1-"era+e BC9-Ko1-"e<port BC9-Ko1-"flu+h BC9-Ko1-"free BC9-Ko1-"getBuffer0ng BC9-Ko1-"0mport BC9-Ko1-"load BC9-Ko1-"read BC9-Ko1-"rew0nd BC9-Ko1-"+a/e BC9-Ko1-"+a/e*0le BC9-Ko1-"+eeN BC9-Ko1-"+etBuffer0ng BC9-Ko1-"+0Qe BC9-Ko1-"tell BC9-Ko1-"truncate BC9-Ko1-"wr0te BC9-Ko1-"wr0teTemporary BC9-Ko1-"wr0teTo*0le

ollec$#ons

7o$ a!!l#cable

BC9-Collect0on-"append BC9-Collect0on-"a++0gn BC9-Collect0on-"a++0gnElem BC9-Collect0on-"free BC9-Collect0on-"getElem BC9-Collect0on-"ma< BC9-Collect0on-"+0Qe BC9-Collect0on-"tr0m

Me$ada$a

3$a$emen$ $"!e 7ame o5 resul$ column

7o$ a!!l#cable oraGcolumnname:;

oc0G+tatementGtype:; oc0Gf0eldGname:;

263

-he O*#o&ete Orac&e 'xten#ion

Operation

Action 3#1e o5 resul$ column

ORA 2unction Ho)'o%eteI oraGcolumn+0Qe:;

OCI0 2unction oc0Gf0eldG+0Qe:; oc0Gf0eldGtype:; oc0Gf0eldGtypeGraw:; oc0Gf0eldGprec0+0on:; oc0Gf0eldG+cale:; oc0GnumGrow+:; oc0GnumGf0eld+:; oc0Gpa++wordGchange:; oc0G0nternalGde1ug:; oc0G+er/erG/er+0on:; oc0G+etGprefetch:; :and ne* php%ini !arame$er;

Da$a$"!e o5 resul$ oraGcolumntype:; column Prec#s#on o5 resul$ 7o$ a!!l#cable column 3cale o5 resul$ column 7umber o5 ro*s e55ec$ed 7o$ a!!l#cable oraGnumrow+:;

oraGnumcol+:; 7umber o5 columns re$urned hang#ng Pass*ord Trac#ng 3er'er ,ers#on Tun#ng 7o$ a!!l#cable 7o$ a!!l#cable 7o$ a!!l#cable 7o$ a!!l#cable

264

APPENDI7 E

RESO,RCES
Th#s &!!end#/ g#'es l#n+s $o documen$a$#on, resources and ar$#cles d#scussed #n $h#s boo+, and $o o$her *eb s#$es o5 #n$eres$. Th#s boo+ #$sel5 can be 5ound onl#ne a$( http://www.oracle.com/technology/tech/php/pdf/underground-php-oraclemanual.pdf

!enera% In2or4ation and =oru4'


! De(eloper "enter on Oracle Technolo#& Aet)ork FOTAG
http://www.oracle.com/technology/tech/php/0nde<.html

OTA ! Discussion Borum


http://www.oracle.com/technology/forum+/php.html

Blo#H "hristopher Iones on O ,+


http://1log+.oracle.com/opal/

Blo#H ,lison !ollo)a& on !


http://1log+.oracle.com/al0+on/

,skTom Aeneral Oracle language and a!!l#ca$#on des#gn hel! http://a+Ntom.oracle.com/ Oracle /etalink Oracle 3u!!or$ *eb s#$e http://metal0nN.oracle.com/ OracleJs Bree and Open Source So3t)are
http://o++.oracle.com/

Orac%e Docu4entation
Oracle Documentation ortal
http://tah0t0.oracle.com/

265

!e#ource#

Oracle Call Interface Programmer's Guide Oracle Da$abase 11g Release 1 :11.1; http://download.oracle.com/doc+/cd/B7M)6>G?2/appde/.222/17M)>6/toc.htm Oracle Database Express Edition 2 Day Plus PHP Developer Guide
http://download.oracle.com/doc+/cd/B76)7>G?2/doc/appde/.2?7/176)2T/toc.htm

Oracle Database 17# $'press $dition Documentation Oracle Da$abase 10g @/!ress @d#$#on :N@; Release 2 :10.2; http://www.oracle.com/pl+/<e2?7/homepage Oracle Database Express Edition 2 Day Plus !ocator Developer Guide Oracle Da$abase 10g Release 2 :10.2; http://download.oracle.com/doc+/cd/B76)7>G?2/doc/appde/.2?7/17M??5/toc.htm Oracle Database "et #ervices $dministrator's Guide Oracle Da$abase 11g Release 1 :11.1; http://download.oracle.com/doc+/cd/B7M)6>G?2/networN.222/17M)2S/toc.htm Oracle Database P!%#&! !anguage 'eference Oracle Da$abase 11g Release 1 :11.1; http://download.oracle.com/doc+/cd/B7M)6>G?2/appde/.222/17M)T?/toc.htm Oracle Database #&! !anguage 'eference Oracle Da$abase 11g Release 1 :11.1; http://download.oracle.com/doc+/cd/B7M)6>G?2/+er/er.222/17M7MS/toc.htm

Se%ected PHP and Orac%e <oo>'


Oracle Database $($) * PHP +eb $pplication Development .ee -arne" and M#chael Mc.aughl#n, Oracle Press, 2008 PHP Oracle +eb Development Hul# ,as#l#e', Pac+$ Publ#sh#ng, 200C ,eginning PHP and Oracle- .rom "ovice to Professional 0. )ason A#lmore and -ob -r"la, &!ress, 200C $pplication Development /it0 Oracle * PHP on !inux for ,eginners >'an -a"ross and 3haranam 3hah, 3hro55 Publ#shers W D#s$r#bu$ers, 2nd @d#$#on 200C

266

Se&ected PHP and Orac&e <oo3#

Oracle Database 12g Express Edition PHP +eb Programming M#chael Mc.aughl#n, Osbourne Oracle Press, 200F Easy Oracle PHP- Create Dynamic +eb Pages /it0 Oracle Data Mladen Aogala, Ram!an$ TechPress, 200F ,rticles and Other Re3erences
http://www.oracle.com/technology/tech/php/htdoc+/phpGtrou1le+hoot0ngGfaF.htm l

PHP #calability and Hig0 $vailability Oracle 0h#$e!a!er, &!r#l 2008 http://www.oracle.com/technology/tech/php/pdf/php-+cala10l0ty-ha-twp.pdf Oracle Database 11g P!%#&! Programming M#chael Mc.aughl#n, Oracle Press, 2008. on$a#ns a PHP !r#mer. Improving Performance 30roug0 Persistent Connections )ohn oggeshall http://www.oracle.com/technology/pu1/art0cle+/oracleGphpGcooN1ooN/cogge+hall Gper+0+t.html 4sing PHP 5 /it0 Oracle )6! D, Hul# ,as#l#e' http://www.oracle.com/technology/oramag/oracle/?6-Zul/o56php.html ,n O(er(ie) on 0lobaliCin# Oracle ! ,pplications
http://www.oracle.com/technology/tech/php/pdf/glo1al0Q0ngGoracleGphpGappl0ca t0on+.pdf

30e PHP 5 Data Ob7ect 8PDO9 $bstraction !ayer and Oracle 0e1 2urlong http://www.oracle.com/technology/pu1/art0cle+/phpGe<pert+/otnGpdoGoracle6.ht ml

So2t9are and Source Code


! Distribution Releases 3ource and 0#ndo*s b#nar#es http://www.php.net/download+.php

267

!e#ource#

! Snapshots 3na!sho$s o5 PHPGs source code and 0#ndo*s b#nar#es http://+nap+.php.net/ $"+ O"I* 3ource !ac+age http://pecl.php.net/pacNage/oc0M $"+ DO?O"I 3ource sna!sho$ http://pecl.php.net/pacNage/8DBGBC9 2end "ore 3or Oracle
http://www.oracle.com/technology/tech/php/Qendcore/

Oracle Instant "lient


http://www.oracle.com/technology/tech/oc0/0n+tantcl0ent/

O"I* Source "ode in ":S


http://c/+.php.net//0ewc/+.cg0/php-+rc/e<t/oc0M/

Oracle S<+ De(eloper Do*nloads and documen$a$#on http://www.oracle.com/technology/product+/data1a+e/+FlGde/eloper/0nde<.html ,DOdb Database ,bstraction +ibrar& 3or ! Fand &thonG
http://adod1.+ourceforge.net/

! $'tension 3or IDe(eloper


http://www.oracle.com/technology/product+/Zde//htdoc+/partner+/add0n+/e<chan ge/php/0nde<.html

! Unit ! Unit Tester


http://www.phpun0t.de/

SimpleTest ! Unit Tester


http://www.+0mplete+t.org/

6debu# D Debu##er and ro3iler Tool 3or !


http://www.<de1ug.org/

26

PHP ,in3#

PHP Lin>'
! !ome a#e
http://www.php.net/

0eneral ! Documentation
http://www.php.net/doc+.php

! Oracle O"I* Documentation


http://www.php.net/oc0M

DO Documentation

http://www.php.net/pdo

! <ualit& ,ssurance Site


http://Fa.php.net/

! Bu# S&stem
http://1ug+.php.net/

! Wiki
http://w0N0.php.net/

26$

!e#ource#

27%

!LOSSARY
Anon:4ou' <%oc>
& P.I3K. bloc+ $ha$ a!!ears #n "our a!!l#ca$#on and #s no$ named or s$ored #n $he da$abase. >n man" a!!l#ca$#ons, P.I3K. bloc+s can a!!ear *here'er 3K. s$a$emen$s can a!!ear. & P.I3K. bloc+ grou!s rela$ed declara$#ons and s$a$emen$s. -ecause $hese bloc+s are no$ s$ored #n $he da$abase, $he" are generall" 5or one?$#me use.

AWR
&u$oma$#c 0or+load Re!os#$or". Used $o s$ore and re!or$ s$a$#s$#cs on da$abase !er5ormance.

<indin"
& me$hod o5 #nclud#ng da$a #n 3K. s$a$emen$s $ha$ allo*s 3K. s$a$emen$s $o be e55#c#en$l" reused *#$h d#55eren$ da$a.

<=ILE
The -2>.@ da$a$"!e s$ores uns$ruc$ured b#nar" da$a #n o!era$#ng?s"s$em 5#les ou$s#de $he da$abase. & -2>.@ column or a$$r#bu$e s$ores a 5#le loca$or $ha$ !o#n$s $o an e/$ernal 5#le con$a#n#ng $he da$a.

<LO<
The -.O- da$a$"!e s$ores uns$ruc$ured b#nar" da$a #n $he da$abase.

CHAR
The H&R da$a$"!e s$ores 5#/ed?leng$h charac$er s$r#ngs #n $he da$abase.

CLO< and NCLO<


The .O- and 7 .O- da$a$"!es s$ore u! $o 8 $erab"$es o5 charac$er da$a #n $he da$abase. .O-s s$ore da$abase charac$er se$ da$a, and 7 .O-s s$ore Un#code na$#onal charac$er se$ da$a.

Co%%ection T:pe
& collec$#on #s an ordered grou! o5 elemen$s, all o5 $he same $"!e. @ach elemen$ has a un#=ue subscr#!$ $ha$ de$erm#nes #$s !os#$#on #n $he collec$#on. P.I3K. da$a$"!es T&-.@ and ,&RR&H enable collec$#on $"!es such as arra"s, bags, l#s$s, nes$ed $ables, se$s and $rees.

Connection Identi2ier
The s$r#ng used $o #den$#5" *h#ch da$abase $o connec$ $o, 5or e/am!le, localho+t/IE.

271

G&o##ary

Connection Strin"
The 5ull s$r#ng used $o #den$#5" *h#ch da$abase $o connec$ $o commonl" used 5or 3K.TPlus. >$ con$a#ns $he username, !ass*ord and connec$ #den$#5#er, 5or e/am!le, hr/hrpwdPlocalho+t/IE.

C@S
oncurren$ ,ers#ons 3"s$em, an o!en source 'ers#on con$rol s"s$em used 5or de'elo!men$ o5 PHP.

Data Dictionar:
& se$ o5 $ables and '#e*s $ha$ are used as a read?onl" re5erence abou$ $he da$abase.

Data)a'e
& da$abase s$ores and re$r#e'es da$a. @ach da$abase cons#s$s o5 one or more da$a 5#les. &l$hough "ou ma" ha'e more $han one da$abase !er mach#ne, $"!#call" a s#ngle Oracle da$abase con$a#ns mul$#!le schemas. & schema #s o5$en e=ua$ed *#$h a user. Mul$#!le a!!l#ca$#ons can use $he same da$abase *#$hou$ an" con5l#c$ b" us#ng d#55eren$ schemas.

Data)a'e Lin>
& !o#n$er $ha$ de5#nes a one?*a" commun#ca$#on !a$h 5rom an Oracle Da$abase ser'er $o ano$her da$abase ser'er. & da$abase l#n+ connec$#on allo*s local users $o access da$a on a remo$e da$abase.

Data)a'e Na4e
The name o5 $he da$abase. >n PHP, $h#s #s $he $e/$ used #n oc0Gconnect:; calls. &lso see Easy (onnect.

Datat:pe
@ach column 'alue and cons$an$ #n a 3K. s$a$emen$ has a da$a$"!e, *h#ch #s assoc#a$ed *#$h a s!ec#5#c s$orage 5orma$, cons$ra#n$s, and a 'al#d range o5 'alues. 0hen "ou crea$e a $able, "ou mus$ s!ec#5" a da$a$"!e 5or each o5 #$s columns. 2or e/am!le, 7UM-@R, or D&T@.

DATE
The D&T@ da$a$"!e s$ores !o#n$?#n?$#me 'alues :da$es and $#mes; #n a da$abase $able.

D<A
Da$abase &dm#n#s$ra$or. & !erson *ho adm#n#s$ers $he Oracle da$abase. Th#s !erson #s a s!ec#al#s$ #n Oracle da$abases, and *ould usuall" ha'e 3H3D-& access $o $he da$abase.

DDL
3K.Ls Da$a De5#n#$#on .anguage. 3K. s$a$emen$s $ha$ de5#ne $he da$abase s$ruc$ure or schema, l#+e CREATE, AKTER, and DRB8.

272

G&o##ary

D8L
3K.Ls Da$a Man#!ula$#on .anguage. 3K. s$a$emen$s $ha$ de5#ne or manage $he da$a #n $he da$abase, l#+e SEKECT, 9LSERT, U8DATE and DEKETE.

Ea': Connect
& s#m!le hos$name and da$abase name s$r#ng $ha$ #s used $o #den$#5" *h#ch da$abase $o connec$ $o.

HR
The sam!le user crea$ed b" de5aul$ *#$h an Oracle seed da$abase #ns$alla$#on. The hr user has access $o $he Human Resources demons$ra$#on $ables #n $he HR schema.

Inde&
>nde/es are o!$#onal s$ruc$ures assoc#a$ed *#$h da$abase $ables. >nde/es can be crea$ed $o #ncrease $he !er5ormance o5 da$a re$r#e'al.

In'tance
The Oracle >ns$ance #s $he runn#ng com!onen$ o5 an Oracle da$abase ser'er. 0hen an Oracle da$abase #s s$ar$ed, a s"s$em global area :3A&; #s alloca$ed and Oracle bac+ground !rocesses are s$ar$ed. The comb#na$#on o5 $he bac+ground !rocesses and memor" bu55ers #s called an Oracle #ns$ance.

In'tant C%ient
The Oracle >ns$an$ l#en$ #s a small se$ o5 l#brar#es, *h#ch allo* "ou $o connec$ $o an Oracle Da$abase. & subse$ o5 $he 5ull Oracle l#en$, #$ re=u#res m#n#mal #ns$alla$#on bu$ has 5ull 5unc$#onal#$". >ns$an$ l#en$ #s do*nloadable 5rom OT7 and #s usable and d#s$r#bu$able 5or 5ree.

LO<
& large ob8ec$. .O-3 ma" be !ers#s$en$ :s$ored #n $he da$abase; or $em!orar". 3ee (3OC, C3OC, and C )3E.

LO< Locator
& M!o#n$erO $o .O- da$a.

8ateria%i;ed @ie9
& ma$er#al#1ed '#e* !ro'#des access $o $able da$a b" s$or#ng $he resul$s o5 a =uer" #n a se!ara$e da$abase schema ob8ec$. Unl#+e an ord#nar" '#e*, *h#ch does no$ $a+e u! an" s$orage s!ace or con$a#n an" da$a, a ma$er#al#1ed '#e* con$a#ns $he ro*s resul$#ng 5rom a =uer" aga#ns$ one or more base $ables or '#e*s.

273

G&o##ary

NCHAR and N@ARCHAR


7 H&R and 7,&R H&R2 are Un#code da$a$"!es $ha$ s$ore Un#code charac$er da$a #n $he da$abase.

N,8<ER
The 7UM-@R da$a$"!e s$ores 5#/ed and 5loa$#ng?!o#n$ numbers #n $he da$abase.

Pac>a"e'
& !ac+age #s a grou! o5 rela$ed P.I3K. !rocedures and 5unc$#ons, along *#$h $he cursors and 'ar#ables $he" use, s$ored $oge$her #n $he da$abase 5or con$#nued use as a un#$.

Procedure' and =unction'


& P.I3K. !rocedure or 5unc$#on #s a schema ob8ec$ $ha$ cons#s$s o5 a se$ o5 3K. s$a$emen$s and o$her P.I3K. !rogramm#ng cons$ruc$s, grou!ed $oge$her, s$ored #n $he da$abase, and run as a un#$ $o sol'e a s!ec#5#c !roblem or !er5orm a se$ o5 rela$ed $as+s.

O)6ect Pri.i%e"e
& r#gh$ $o !er5orm a !ar$#cular ac$#on on a s!ec#5#c da$abase schema ob8ec$. D#55eren$ ob8ec$ !r#'#leges are a'a#lable 5or d#55eren$ $"!es o5 schema ob8ec$s. The !r#'#lege $o dele$e ro*s 5rom $he departments $able #s an e/am!le o5 an ob8ec$ !r#'#lege.

ORACLEAHO8E in'ta%%
&n Oracle l#en$ or Oracle Da$abase #ns$all. These #ns$alls con$a#n all so5$*are re=u#red b" PHP #n a d#rec$or" h#erarch". Th#s se$ o5 d#rec$or#es #ncludes b#nar#es, u$#l#$#es, con5#gura$#on scr#!$s, demons$ra$#on scr#!$s and error message 5#les 5or each com!onen$ o5 Oracle. &n" !rogram us#ng Oracle $"!#call" re=u#res $he BRACKEG$BOE en'#ronmen$ 'ar#able $o be se$ $o $he $o! le'el #ns$alla$#on d#rec$or".

Orac%e Net
The ne$*or+#ng com!onen$ o5 Oracle $ha$ connec$s cl#en$ $ools such as PHP $o local or remo$e da$abases. The Oracle 7e$ l#s$ener #s a !rocess $ha$ handles connec$#on re=ues$s 5rom cl#en$s and !asses $hem $o $he $arge$ da$abase.

OTN
The Oracle Technolog" 7e$*or+ #s OracleLs 5ree re!os#$or" o5 ar$#cles on Oracle $echnolog#es. >$ also hos$s so5$*are do*nloads and man" d#scuss#on 5orums, #nclud#ng one on PHP.

Pac>a"e
& grou! o5 P.I3K. !rocedures, 5unc$#ons, and 'ar#able de5#n#$#ons s$ored #n $he Oracle da$abase. Procedures, 5unc$#ons, and 'ar#ables #n !ac+ages can be called 5rom o$her !ac+ages, !rocedures, or 5unc$#ons.
274

G&o##ary

PEAR
The PHP @/$ens#on and &!!l#ca$#on Re!os#$or" :P@&R; #s a re!os#$or" 5or reusable !ac+ages *r#$$en #n PHP.

PECL
The PHP @/$ens#on ommun#$" .#brar" :P@ .; #s a re!os#$or" o5 PHP e/$ens#ons $ha$ can be l#n+ed #n$o $he PHP b#nar".

PHP
& !o!ular, #n$er!re$ed scr#!$#ng language commonl" used 5or *eb a!!l#ca$#ons. PHP #s a recurs#'e acron"m 5or MPHP( H"!er$e/$ Pre!rocessorO.

php.ini
The con5#gura$#on 5#le used b" PHP. Man" :bu$ no$ all; o!$#ons $ha$ are se$ #n php%ini can also be se$ a$ run$#me us#ng 0n0G+et:;.

PL5S3L
OracleLs !rocedural language e/$ens#on $o 3K.. >$ #s a ser'er?s#de, s$ored !rocedural language $ha$ enables "ou $o m#/ 3K. s$a$emen$s *#$h !rocedural cons$ruc$s. 0#$h P.I3K., "ou can crea$e and run P.I3K. !rogram un#$s such as !rocedures, 5unc$#ons, and !ac+ages. P.I3K. !rogram un#$s generall" are ca$egor#1ed as anon"mous bloc+s, s$ored 5unc$#ons, s$ored !rocedures, and !ac+ages.

Prepared State4ent
& 3K. s$a$emen$ $ha$ has been !arsed b" $he da$abase. >n Oracle, #$ #s generall" called a !arsed s$a$emen$.

Re"u%ar E&pre''ion
& !a$$ern used $o ma$ch da$a. Oracle has se'eral 5unc$#ons $ha$ acce!$ regular e/!ress#ons.

Round Trip
& call and re$urn se=uence 5rom PHP O >8 $o $he Da$abase !er5ormed b" $he underl"#ng dr#'er l#brar#es. @ach round $r#! $a+es ne$*or+ $#me and mach#ne PU resources. The 5e*er round $r#!s !er5ormed, $he more scalable a s"s$em #s l#+el" $o be. PHP O >8 5unc$#ons ma" #n#$#a$e 1ero or man" round $r#!s.

Sche4a
& schema #s a collec$#on o5 da$abase ob8ec$s. & schema #s o*ned b" a da$abase user and has $he same name as $ha$ user. 3chema ob8ec$s are $he log#cal s$ruc$ures $ha$ d#rec$l" re5er $o $he da$abaseGs da$a. 3chema ob8ec$s #nclude s$ruc$ures l#+e $ables, '#e*s, and #nde/es.

275

G&o##ary

SDC
3o5$*are De'elo!men$ 6#$. Oracle >ns$an$ l#en$ has an 3D6 5or bu#ld#ng !rograms $ha$ use $he >ns$an$ l#en$ l#brar#es.

SeEuence
& se=uence :a se=uen$#al ser#es o5 numbers; o5 Oracle #n$egers o5 u! $o P8 d#g#$s de5#ned #n $he da$abase.

Ser.ice Na4e
& ser'#ce name #s a s$r#ng $ha$ #s $he global da$abase name, com!r#sed o5 $he da$abase name and doma#n name. Hou can ob$a#n #$ 5rom $he SERV9CEGLAOES !arame$er #n $he da$abase #n#$#al#1a$#on !arame$er 5#le or b" us#ng S$B& 8ARAOETERS #n 3K.TPlus. >$ #s used dur#ng connec$#on $o #den$#5" *h#ch da$abase $o connec$ $o.

SID HS:'te4 Identi2ierI


The s"s$em #den$#5#er #s commonl" used $o mean $he da$abase name al#as #n $he connec$#on s$r#ng.

SID HSe''ion Identi2ierI


& sess#on #den$#5#er #s a un#=ue number ass#gned $o each da$abase user sess#on *hen $he" connec$ $o $he da$abase.

S3LDP%u'
The $rad#$#onal command l#ne $ool 5or e/ecu$#ng 3K. s$a$emen$s a'a#lable *#$h all Oracle da$abases. &l$hough recen$l" su!erseded b" AU> $ools l#+e OracleLs 5ree 3K. De'elo!er, 3K.TPlus rema#ns hugel" !o!ular. >$ #s also con'en#en$ $o sho* e/am!les us#ng 3K.TPlus.

Stored Procedure' and =unction'


& P.I3K. bloc+ $ha$ Oracle s$ores #n $he da$abase and can be called b" name 5rom an a!!l#ca$#on. 2unc$#ons are d#55eren$ $han !rocedures #n $ha$ 5unc$#ons re$urn a 'alue *hen e/ecu$ed. 0hen "ou crea$e a s$ored !rocedure or 5unc$#on, Oracle !arses $he !rocedure or 5unc$#on, and s$ores #$s !arsed re!resen$a$#on #n $he da$abase.

S:non:4
& s"non"m #s an al#as 5or an" da$abase $able, '#e*, ma$er#al#1ed '#e*, se=uence, !rocedure, 5unc$#on, !ac+age, $"!e, )a'a class schema ob8ec$, user?de5#ned ob8ec$ $"!e, or ano$her s"non"m.

SYS
&n Oracle da$abase adm#n#s$ra$#'e user accoun$ name. sys has access $o all base $ables and '#e*s 5or $he da$abase da$a d#c$#onar".

276

G&o##ary

SYSD<A
&n Oracle da$abase s"s$em !r#'#lege $ha$, b" de5aul$, #s ass#gned onl" $o $he sys user. >$ enables sys $o !er5orm h#gh?le'el adm#n#s$ra$#'e $as+s such as s$ar$#ng u! and shu$$#ng do*n $he da$abase.

SYSOPER
3#m#lar $o sysdba, bu$ *#$h a l#m#$ed se$ o5 !r#'#leges $ha$ allo*s bas#c adm#n#s$ra$#'e $as+s *#$hou$ ha'#ng access $o user da$a.

SYSTE8
&n Oracle da$abase adm#n#s$ra$#'e user accoun$ name $ha$ #s used $o !er5orm all adm#n#s$ra$#'e 5unc$#ons o$her $han s$ar$#ng u! and shu$$#ng do*n $he da$abase.

S:'te4 pri.i%e"e
The r#gh$ $o !er5orm a !ar$#cular ac$#on, or $o !er5orm an ac$#on on an" da$abase schema ob8ec$s o5 a !ar$#cular $"!e. 2or e/am!le, $he !r#'#leges $o crea$e $ables and $o dele$e $he ro*s o5 an" $able #n a da$abase.

Ta)%e
Tables are $he bas#c un#$ o5 da$a s$orage. Da$abase $ables hold all user?access#ble da$a. @ach $able has columns and ro*s.

Ta)%e'pace
Tables!aces are $he log#cal un#$s o5 Oracle da$a s$orage made u! o5 one or more da$a5#les. Tables!aces are o5$en crea$ed 5or #nd#'#dual a!!l#ca$#ons because $ables!aces can be con'en#en$l" managed. Users are ass#gned a de5aul$ $ables!ace $ha$ holds all $he da$a $he users crea$es. & da$abase #s made u! o5 de5aul$ and D-&?crea$ed $ables!aces.

Te4porar: LO<
3ee 3OC.

Te4porar: Ta)%e
& Alobal Tem!orar" Table #s a s!ec#al $able $ha$ holds sess#on?!r#'a$e da$a $ha$ e/#s$s onl" 5or $he dura$#on o5 a $ransac$#on or sess#on. The $able #s crea$ed be5ore $he a!!l#ca$#on runs.

Tn'na4e'.ora
The Oracle 7e$ con5#gura$#on 5#le used 5or connec$#ng $o a da$abase. The 5#le ma!s an al#as $o a local or remo$e da$abase and allo*s 'ar#ous con5#gura$#on o!$#ons 5or connec$#ons. The al#as #s used #n $he PHP connec$#on s$r#ng. T73 s$ands 5or Trans!aren$ 7e$*or+ 3ubs$ra$e.

277

G&o##ary

Tran'action
& se=uence o5 3K. s$a$emen$s *hose changes are e#$her all comm#$$ed, or all rolled bac+.

Tri""er
& s$ored !rocedure assoc#a$ed *#$h a da$abase $able, '#e*, or e'en$. The $r#gger can be called a5$er $he e'en$, $o record #$, or $a+e some 5ollo*?u! ac$#on. The $r#gger can be called be5ore $he e'en$, $o !re'en$ erroneous o!era$#ons or 5#/ ne* da$a so $ha$ #$ con5orms $o bus#ness

,'er
& da$abase user #s o5$en e=ua$ed $o a schema. @ach user connec$s $o $he da$abase *#$h a username and secre$ !ass*ord, and has access $o $ables, and so on, #n $he da$abase.

@ARCHAR and @ARCHAR


These da$a$"!es s$ore 'ar#able?leng$h charac$er s$r#ngs #n $he da$abase. The names are curren$l" s"non"ms bu$ ,&R H&R2 #s recommended $o ensure ma/#mum com!a$#b#l#$" o5 a!!l#ca$#ons #n 5u$ure.

@ie9
,#e*s are cus$om#1ed !resen$a$#ons o5 da$a #n one or more $ables or o$her '#e*s. & '#e* can also be cons#dered a s$ored =uer". ,#e*s do no$ ac$uall" con$a#n da$a. Ra$her, $he" der#'e $he#r da$a 5rom $he $ables on *h#ch $he" are based, re5erred $o as $he base $ables o5 $he '#e*s.

78LT:pe
NM.T"!e #s a da$abase da$a$"!e $ha$ can be used $o s$ore NM. da$a #n $able columns.

27

The Underground PHP and Oracle Manual


About this Book
This book is for PHP programmers developing applications for Oracle Database. It bridges the gap between the many PHP and Oracle books available and shows how to use the PHP scripting language with Oracle Database. 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 a lead maintainer of PHPs open source OCI8 extension and works closely with the PHP community. He also helps ensure that future versions of Oracle Database are compatible with PHP . He is the author of various technical articles on PHP and Oracle technology, and has presented at conferences including PHP|Tek, the International PHP Conference, the OReilly 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.

You might also like