My focus
Contact information
Connecting from off the box Ensuring your EBCDIC data gets converted when
sent to PHP
Prerequsites
Details of differences:
http://mikepavlak.blogspot.com/2010/08/i-want-to-do-php-onibm-i-so-what-do-i.html
Alan Seiden Consulting 8 DB2 and PHP Best Practices on IBM i
10
11
DB2 drivers
odbc
Less functionality than the others
IBM_PDO
PDO = PHP Data Objects Generic DB2. No / separator or library lists (yet)
ibm_db2
Provides IBM i-specific features such as library list support
Alan Seiden Consulting 13 DB2 and PHP Best Practices on IBM i
ibm_db2 documentation
Manual page
http://php.net/ibm_db2
14
db2_connect()
resource db2_connect (string $database, string $username, string $password [, array $options ])
db2_pconnect() is similar
pconnect creates persistent connections (more on that later)
18
$username
Leave blank ('') for default Apache user (QTMHHTTP) Use any valid user profile to associate queries with that user
Uppercase the user profile to be safe
$password
Leave blank ('') if $database and $username were blank Otherwise, provide password corresponding to $username
Alan Seiden Consulting 19 DB2 and PHP Best Practices on IBM i
20
$options, parameter #4
An optional array to fine-tune the connection Below are choices that are most relevant for IBM i Details on next few slides
i5_lib
Set a single default library
i5_libl
Set a library list (be sure to set i5_naming on)
i5_naming
Choose system or SQL naming
i5_commit
Commitment control options
autocommit
DB2_AUTOCOMMIT_ON (default) or _OFF
21
i5_lib
Specify one library as default
'i5_lib'=>'MYLIB'
22
23
i5_naming
DB2_I5_NAMING_ON
A constant equal to 1 Turns on system naming mode Table files qualified using the slash (/) delimiter Unqualified files are resolved using the library list for the job
DB2_I5_NAMING_OFF
A constant equal to 0 (default) Enables SQL naming mode Table files qualified using the period (.) delimiter Unqualified files are resolved using either the default library (i5_lib) or the user profile name specified on db2_connect() (could be QTMHHTTP) Message to watch for: MYTABLE in YOURNAME type *FILE not found. SQLCODE=-204
24
25
Commitment control
If one query fails, roll back previous related queries Example: If a detail record update fails, roll back the
header update
27
i5_commit
i5_commit
Options for commitment control Gives you fine-grained control (and ability to turn off altogether) Only relevant when commitment control is enabled system-wide
ibm_db2.i5_allow_commit = 1 in INI file
Choices:
DB2_I5_TXN_NO_COMMIT turns off commitment control for this connection DB2_I5_TXN_READ_UNCOMMITTED DB2_I5_TXN_READ_COMMITTED DB2_I5_TXN_REPEATABLE_READ DB2_I5_TXN_SERIALIZABLE
29
autocommit
DB2_AUTOCOMMIT_ON
A constant equal to 1 (default) Turns autocommit on
End of script causes commit
Only relevant when commitment control is used Convenient: insert/update/delete will work without db2_commit()
DB2_AUTOCOMMIT_OFF
A constant equal to 0 Turns autocommit off Only relevant when commitment control is used Provides flexibility to ensure data integrity in multi-step transactions by using db2_commit()/db2_rollback() around groups of insert/update/ delete queries
30
In db2_connect() options:
Modify default settings with i5_commit option Choose autocommit on or off
Persistent = fast
Persistent connections
Create a pool of database jobs
Known as a connection pool You will connect quickly because a job is waiting for you DB2 will...
Choose a job each time you run a query Create new jobs to handle high workload
No guarantee that a PHP script connects to same job each time Cannot rely on maintaining state (QTEMP, library lists)
33 DB2 and PHP Best Practices on IBM i
34
35
36
Global settings
ibm_db2.ini
Located in /usr/local/zendsvr/etc/conf.d/ A small file containing only ibm_db2 settings No need for a [ibm_db2] section Initial contents:
extension=ibm_db2.so
38
Global configuration
Setting ibm_db2.i5_all_pconnect ibm_db2.i5_ignore_userid ibm_db2.i5_allow_commit Default "0" "0" "0" What it does Force persistent connections Ignore user/pwds specified in db2_connect() turn commitment control on/off for schemas (and journal-able libraries)
ibm_db2.i5_all_pconnect
When ibm_db2.i5_all_pconnect = 1:
40
ibm_db2.i5_ignore_userid
When ibm_db2.i5_ignore_userid = 1:
41
ibm_db2.i5_allow_commit
When ibm_db2.i5_allow_commit = 1:
42
Prepare queries
They are also known as prepared statements Heres an example of the mischief they prevent
44
Do you see any potential problems? What if the name is OShea ? Error!
$sql = "select custno from custfile where name = 'O'Shea' and status = 'ACTIVE' ";
Single quotes confuse query parser when they serve two purposes
Used as apostrophe in data Delimiter of string literals in the SQL syntax
45
46
instead of literal values Its fine to retain hard-coded values in the query
48
51
Check result
54
55
57
No persistence or sharing with other PHP jobs Connect to only one RDB (database) per PHP job
58
Disadvantages
59
Freeing resources
62
65
66
Logical files
68
SQE is better: utilizes improvements of recent years Solution: use a WHERE clause, when possible,
instead of select/omit (or upgrade to 7.1)
69
To sum up
Limit the number of user profiles that you connect with Be consistent about the options you use
72
73
DeveloperWorks wiki
http://ibm.com/developerworks/ibmi
Keep up to date
Send me feedback so I can continue to hone this
information
75
Questions
Contact
Alan Seiden
Alan Seiden Consulting
Ho-Ho-Kus, NJ
alanseiden.com
alan@alanseiden.com
Alan Seiden Consulting
201-447-2437
77
twitter: @alanseiden
DB2 and PHP Best Practices on IBM i