7/30/2019 Getting Started with PHP and Oracle
1/51
PHP for PL/SQL Developers
Lewis Cunningham
JP Morgan Chase
1
7/30/2019 Getting Started with PHP and Oracle
2/51
What is PHP? PHP is a HTML pre-processor
PHP allows you to generate HTML dynamically
PHP is a scripting language usable on the web, theserver and the desktop
PHP is embeddable in HTML
PHP is resolved on the server, not in the browser
PHP has a very easy, and probably familiar, syntax
PHP is extremely easy for PL/SQL Developers to add totheir toolbox
2
7/30/2019 Getting Started with PHP and Oracle
3/51
Where did PHP come from? Created by a developer who wanted to make his job
easier
Was originally just a set of Perl scripts Called Personal Home Page tools
Version 2.0 was re-written in C
3.0 extended PHP with many new APIs
4.0 Added bad OOP syntax
5.0 Fixed 4.0
6.0 is on the way
3
7/30/2019 Getting Started with PHP and Oracle
4/51
How does PHP work? PHP files reside where HTML files live
The web server is configured to serve certain
extensions as static HTML (ex. HTM) and otherextensions as PHP (ex. PHP)
HTML is served unchanged, PHP is sent to the PHPprocessor
The PHP processor can talk to databases, performcomplex logic and knows how to build HTML
PHP returns control to the web server which thenreturns an HTML page to the requesting browser
4
7/30/2019 Getting Started with PHP and Oracle
5/51
Why PHP for PL/SQL Devs? Familiar data types, all the usual (sort of)
Procedural OR Object syntax
Block based Exception handling
NULLs
Arrays and Objects
Large built-in function list
5
7/30/2019 Getting Started with PHP and Oracle
6/51
Using PHP To use PHP, you'll need to install some softwware
Fortunately, PHP runs on pretty much any OS
You'll need a web server Download Apache and PHP
Download Zend Server CE (I'm using this today)
If you are using a database, install the database first -
I've had much better luck that way
6
7/30/2019 Getting Started with PHP and Oracle
7/51
Hello World
Note: While PHP supports using
7/30/2019 Getting Started with PHP and Oracle
8/51
Todays quick demo Convert a static HTML form to PHP
Have it use an Oracle database
Display existing dataAdd new data
Show Oracles OCI syntax and PHPs PDO syntax
In general, if you are working with Oracle, you arebetter off with OCI
8
7/30/2019 Getting Started with PHP and Oracle
9/51
User Comments HTML Form
9
A not very useful, static HTML form
7/30/2019 Getting Started with PHP and Oracle
10/51
User Comments HTML FormWhat does it do?
Not much.
Can enter first name, last name and some comments. Press Ok and it calls itself, returning as an empty form.
What would we like it to do?
Enter the same information but save it to a database.
Also, display existing comments below the form.
10
7/30/2019 Getting Started with PHP and Oracle
11/51
User Comments HTML Form
11name_page.php
7/30/2019 Getting Started with PHP and Oracle
12/51
Same Form, as PHP
12name_page.php
7/30/2019 Getting Started with PHP and Oracle
13/51
Huh? I just changed the extension from HTML to PHP
My web server is configured to recognize the PHP
extension and send that request to the PHP processor PHP pre-processes HTML
If it finds PHP instructions, it executes them
This page has no PHP so the processor passes it
through unchanged
13
7/30/2019 Getting Started with PHP and Oracle
14/51
The First Change PHP Freebie
14name_page1.php
7/30/2019 Getting Started with PHP and Oracle
15/51
Remember, ends it
The echo procedure is sort of like HTP.PRN, it sendsthe text to the web page
$ says this is a variable
_SESSION is a global super variable
Items in [] are array elements, $_SESSION is anassociative array
PHP instructions end with a ;
15
7/30/2019 Getting Started with PHP and Oracle
16/51
Starting some real code
16name_page2.php
7/30/2019 Getting Started with PHP and Oracle
17/51
Line 1 : Start PHP code
Lines 3-5 : Initialize variables
Line 7 : See if the _POST supervariable has my formsitems
Lines 8-10 : Assign the values of the _POST array to myvariables
Line 12 : Display variables to web page Line 14 : End PHP code
17
7/30/2019 Getting Started with PHP and Oracle
18/51
Changing the HTML
18name_page2.php
7/30/2019 Getting Started with PHP and Oracle
19/51
Redisplay entered data Remember the echo function sends output to the web
page
7/30/2019 Getting Started with PHP and Oracle
20/51
Now when we run, before OK
20
7/30/2019 Getting Started with PHP and Oracle
21/51
After OK
21
7/30/2019 Getting Started with PHP and Oracle
22/51
Reusing Code in PHP Include Insert code into another script, will give a
warning if the file can't be found
Require Insert code into a script, fail with an error ifthe file can't be loaded
Include_once, require_once Same as the above butchecks to see if the file has already been loaded and
will not load it a second time Files for Include and Require can be anywhere on the
file system
PHP_INI has an INCLUDE_PATH
22
7/30/2019 Getting Started with PHP and Oracle
23/51
Best Practice Alert Included and required files
Do not put your include files in the document root ofyour web server
You will often put important information, like databasepasswords, in these files
If the files are viewable, someone may find them, even ifaccidentally
You can put these files anywhere, on any file system,that is accessible from a command line
23
7/30/2019 Getting Started with PHP and Oracle
24/51
Connecting to the Database
24name_page3.php
7/30/2019 Getting Started with PHP and Oracle
25/51
The connection
25
Yellow is the actual OCI connection string (username, password and database)
The code circled in red just displays an error message if the connection failsHtmlentities makes sure the text is safe to display on the web page
connect.inc
7/30/2019 Getting Started with PHP and Oracle
26/51
After the connection In this case, unless the connection fails, we don't see
any differences
View the generated HTML code and there is nodifference there either
The connection will end when the current page isfinished being generated
You can use persistent connections and connectionpooling (both discussed further, later)
26
7/30/2019 Getting Started with PHP and Oracle
27/51
Now to see some dataAt this point we want to see any existing records so we
will include code to select and fetch
That means creating a table in the database and
adding a record
27user_comments.sql
7/30/2019 Getting Started with PHP and Oracle
28/51
Selecting some data
28
We are adding a select and fetch step at the bottom ofour web page
name_page4.php
7/30/2019 Getting Started with PHP and Oracle
29/51
Selecting and Fetching
29oci_fetch.inc
7/30/2019 Getting Started with PHP and Oracle
30/51
Selecting and Fetching Line 3 : This is the select statement. This line is like the
parse step in DBMS_SQL
Line 5 : Executes the select and checks for success
Line 17 : This is the exception code that executesshould the oci_execute fail
Lines 6 15 : This is where the code is generated
An HTML table is created The while loop gets the row data
The foreach loops through the columnar data
30
7/30/2019 Getting Started with PHP and Oracle
31/51
Looping This while loop in this code is a very traditional while.
While something is true, continue.
The foreach is much like the PL/SQL cursor for loop orlooping through an Oracle associative array (which inthis case, $row IS an associative array)
PHP also offers
DO..WHILE traditional do loop which always executesat least once before checking the condition
FOR Traditional for loop, for (init, check value,increment) for ($var = 1, $var
7/30/2019 Getting Started with PHP and Oracle
32/51
Selecting and Fetching Oci_fetch_array fetches an easy to use associative array
Oci_fetch_array can return an associative array, a numericarray or both
Alternatives to oci_fetch_array are
Oci_fetch_row like fetch array but with only a numeric index
Oci_fetch_all Return ALL rows into a user defined array
32
7/30/2019 Getting Started with PHP and Oracle
33/51
What it looks like now
33
7/30/2019 Getting Started with PHP and Oracle
34/51
A little cleanup Best Practice? Time to clean up the HTML a little
Moving all executable code to INCLUDE files
Personal standard CSS External so that designers own it
DB Code External include files so that DB guys own it
PHP Code Mostly external so that pure code changes
are separate from HTML changes
34
7/30/2019 Getting Started with PHP and Oracle
35/51
Init.inc
35init.inc
7/30/2019 Getting Started with PHP and Oracle
36/51
Inserting a record, HTML change
36name_page5.php
7/30/2019 Getting Started with PHP and Oracle
37/51
Why would insert be at the top? Unless you use AJAX, HTML only does something
when the user requests it
In this case, the user requests a new page
The new page is this page, but with any changesPOSTed
This is, by default, the only way for HTML to see a
change Request PageMake changes Submit Page
Server Processes Returns a new page
37
7/30/2019 Getting Started with PHP and Oracle
38/51
Inserting a record, include file
38oci_insert.inc
7/30/2019 Getting Started with PHP and Oracle
39/51
Inserting a record Lines 2-6 : Code that was in the HTML file, now
moved to the include file
Lines 8-10 : oci_parse, the SQL INSERT statement
Lines 12-14 : bind the page variables to SQL variables
Line 16 : The insert is executed
Lines 17-18 : Exception handling if insert fails
Line 20 : Success message
39
7/30/2019 Getting Started with PHP and Oracle
40/51
No what is it doing?
40
7/30/2019 Getting Started with PHP and Oracle
41/51
Can we improve this?A general best practice for any web application is to
not embed your data logic in the application
The optimal approach is to make an API available andkeep specific knowledge of the database, in thedatabase
In this case, both our insert and our select can be
moved to a stored procedureWill tackle the insert first
41
7/30/2019 Getting Started with PHP and Oracle
42/51
A User Comments Package, Spec
42user_comments_pkg.sql
7/30/2019 Getting Started with PHP and Oracle
43/51
A User Comments Package, Body
43user_comments_pkg.sql
7/30/2019 Getting Started with PHP and Oracle
44/51
A new oci_insert.inc
44oci_insert_proc.inc
7/30/2019 Getting Started with PHP and Oracle
45/51
What changed? The only thing that changed is that the insert was
converted to a stored proc call
The variables are still bound
The execution is the same
Exception handling is the same
The display hasn't changed
45
7/30/2019 Getting Started with PHP and Oracle
46/51
Bigger changes for the Select
46oci_select_proc.inc
7/30/2019 Getting Started with PHP and Oracle
47/51
Step by Step
We now have 2 cursors
A ref cursor is a cursor inside a cursorWe get a cursor on the stored proc call and another on
the OUT parameter for the return record set
47
We bind the ref parameter (:ref) to the new cursor($ref_cursor)
7/30/2019 Getting Started with PHP and Oracle
48/51
Step by Step
48
We execute $stmt which is the stored proc call
If that is successful, we execute the ref cursor
7/30/2019 Getting Started with PHP and Oracle
49/51
Step by Step The remainder of the code is executed as normal
The row fetch is followed by the item fetch, using the$ref_cursor variable instead of the $stmt variable
We handle execptions for both executes
49
7/30/2019 Getting Started with PHP and Oracle
50/51
Using PDO instead of OCI OCI is an oracle specific driver
You can't run OCI against MySQL, or other databases
PHP offers a native wrapper called PDOWith PDO, your code can be reused between
databases
PDO does not support REF Cursors or some other
Oracle specific constructs Oracle recommends using the OCI package
50
7/30/2019 Getting Started with PHP and Oracle
51/51
References and additional info PHP.net The best resource for any PHP needs
PHP.net/Manual Online and downloadable PHPmanual
Zend.com The Zend engine and plenty of usefuldocumentation
eclipse.org/pdt Eclipse PDT project, PHP
Development Tools PHPBuiler.com Tutorials, articles, forum
Top Related