Professional Documents
Culture Documents
Oracle PHP Underground
Oracle PHP Underground
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
"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...................................................................................................................*
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
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
/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# !%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.-
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+.
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.
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.
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
-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"
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 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#(
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/
,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+.
,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.
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.
1%
CHAPTER $
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.
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 !"
13
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 !"
14
<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
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
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.
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+.
Orac%e C%ient Li)rarie' ,'a)%e 9ith PDOAOCI 8i, Di, 10g, 11g
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
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
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.
O >8 1.P
CHAPTER (
&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(
1$
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(
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.
P.
2%
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
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.
>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
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.
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
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
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, '.
2.
24
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.
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(
+ource /u+r/l01/oracle/<e/app/oracle/product/2?.7.?/+er/er/10n/oracleGen/.c+h
25
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
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
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
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
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$.
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!.
.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$
3%
CHAPTER #
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
32
P.
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.
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.
34
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
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.
36
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
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.
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.
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$
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.
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.
4%
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
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.
&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
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 .
43
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.
44
.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$.
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&$
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
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
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
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
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
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
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
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
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"
>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$
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.?
-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.
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
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
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.
53
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
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.
55
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 < $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
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?" 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*
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.
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$
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%
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
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 -
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.
>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
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.
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.
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&.
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.
65
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+.
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.
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
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$
>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.
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
>5 "ou do*nloaded $he %tar%g. 5#le, e/$rac$ #$ *#$h tar -Q<f. P.
7%
>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.
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.
2.
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.
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
5.
>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
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.
72
F.
C.
8.
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&&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.
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
>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.
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.
: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
& !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.
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.
C.
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.
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.
>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
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.
&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
CHAPTER 0
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
&!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.
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
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.
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.
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.
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.
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.
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.
2.
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.
$%
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
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
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
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
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
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
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".
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.
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.
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.
$$
1%%
CHAPTER +
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.
@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
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#$.
&n @as" onnec$ s$r#ng & onnec$ Descr#!$or s$r#ng & onnec$ 7ame
1%2
>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
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!!%!:.
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#$".
>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
>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.
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.
.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.
1%6
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
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.
&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%
>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
"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%$
>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.
(&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.
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
>$ #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.
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.
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
>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.
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#(
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
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.
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
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
>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.
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
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
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.
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$.
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$
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.
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.
12%
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.
121
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
123
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.
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*
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
,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:,+;( !"
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
,+ - 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(
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
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;(
: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;(
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$
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
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 . . .
:etch :unction# Addre++ 0+ 7?2T Sh0nZuNu-Nu Addre++ 0+ >56? Cam0ya-cho Addre++ 0+ 7?25 Ya11erwocNy Rd . . .
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.
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.(
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.
>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
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.(
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.(
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.(
&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
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
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#
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
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
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.
13
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()*%
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$
,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".
&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%
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
141
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
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.(
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
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
& 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
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
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
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
&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 !"
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.
147
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:,+;(
-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:,+;( !"
$o
OURTELSTRASSE >72
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$
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.
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%
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.
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
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.;(
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
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:,+;(
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.
>$ #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
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?
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
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( /
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
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*.
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
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.
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.
<&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
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:.
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.
(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(
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.
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
The call command #s ac$uall" a 3K. command and does no$ ha'e a $ra#l#ng sem#?colon.
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:;.
164
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!.
- pGcount(
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 !"
& 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
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#
>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
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 . . .
$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
>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 .
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
>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(
/#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:,+;( !"
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.
175
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.
/#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>
177
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+
/#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 $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.
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 '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
// 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 !"
>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*.
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.
,+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+;( !"
1 5
The CUSTBOERG9D, *9RSTGLAOE and KASTGLAOE columns are scalar LUOBER and VARC$AR7 columns re$urned d#rec$l" #n$o a PHP arra".
-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
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&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$%
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
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".
/#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:;( !"
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 !"
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+ !"
>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(#;(
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
The 5ull l#s$ o5 .O- me$hods and 5unc$#ons #s sho*n #n Table 10. hec+ $he PHP manual 5or usage.
1$7
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#$"
1$
>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".( !"
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
2%2
CHAPTER 1$
2%3
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.
: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"
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
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.
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
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%
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
2%$
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(
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
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(
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.
213
igure ?'" /ithout DR(P7 idle persistent connections $rom PHP still consume database resources%
214
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
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 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.
216
#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
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.
& 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.
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
Shared Ser.er' 5000 T E00 67o$e( 2or 3hared 3er'ers, sess#on memor" #s alloca$ed 5rom $he 3A&.
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.
21$
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#!$.
on5#gur#ng and enabl#ng $he !ool. on5#gur#ng PHP. De!lo"#ng $he a!!l#ca$#on.
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.
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:;(
221
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
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.
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.
223
<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.
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
"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
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.
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(
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
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.
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
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
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.
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.
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
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
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.
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
>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
&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.(
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.
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
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.
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.
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<A#'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.
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.
23
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.???%??
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
>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
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.
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
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
>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:,+;(
241
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+;( !"
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+
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
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-
>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
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
!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.
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$.
247
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.
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.
@ach $es$ should crea$e an" ob8ec$s #$ needs and dro! $hem a$ $he end o5 $he $es$.
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$
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.
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.
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%
8.
D.
251
252
APPENDI7 A
,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 !"
-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 <
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.
255
Na4e oc0M.e/ent+
De2au%t Bff
&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
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
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
De5#n#ng
@/ecu$#on 2e$ch#ng
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:;
25
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
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$
Operation
Action
PHP ( Na4e
PHP # Na4e oc0Gf0eldGname:; oc0Gf0eldG+0Qe:; oc0Gf0eldGtype:; oc0Gf0eldGtypeGraw:; oc0Gf0eldGprec0+0on:; oc0Gf0eldG+cale:; oc0GnumGrow+:; oc0GnumGf0eld+:;
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+:;
26%
APPENDI7 D
#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
Operation
OCI0 2unction oc0Gpar+e:; oc0Ge<ecute:; 2ollo*ed b" one o5( oc0GfetchGall:; oc0GfetchGarray:; oc0GfetchGa++oc:; oc0GfetchGo1Zect:; oc0GfetchGrow:; oc0Gfetch:;
2e$ch#ng
oraGfetch:; oraGfetchG0nto
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
Action
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
Me$ada$a
oc0G+tatementGtype:; oc0Gf0eldGname:;
263
Operation
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
,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
266
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
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 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/
26
PHP ,in3#
PHP Lin>'
! !ome a#e
http://www.php.net/
0eneral ! Documentation
http://www.php.net/doc+.php
DO Documentation
http://www.php.net/pdo
! 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.
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
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#$.
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.
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.
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.
@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