Stored Procedures with PHP on IBM i Mike Pavlak Solution Consultant mike.p@zend.com © All rights reserved. Zend Technologies, Inc. PHP Sessions Tue – 10:30 • What’s New with Zend Server Tue – 11:40 • Business value of PHP Tue – 13:30 • Stored procedures with PHP and IBM i Tue – 14:40 • PHP Toolkit Examples Tue – 16:00 • Web Services with PHP and IBM i Wed – All day | 2 Copyright © 2009 Zend Technologies, Inc, All rights reserved • Workshop with PHP & IBM i © All rights reserved. Zend Technologies, Inc. 02/03 Agenda • DB2 SQL Stored procedures Examples • RPG Hello World from RPG (sp1) Pass an array from RPG to PHP (sp2) Pass a record set from RPG to PHP (sp3) • Q&A 3 © All rights reserved. Zend Technologies, Inc. Stored Procedures with PHP for IBM i www.zend.com Stored procedures Introduction © All rights reserved. Zend Technologies, Inc. Stored Procedures…What are they? • Database artifacts that perform a specific task. • Zend Server PHP on IBM i supports access to multiple DB’s DB2 MySQL MS SQL Server Oracle Mongo DB (Zend Server 5.6) • Therefore, SP’s on each are available to PHP on IBM i 5 © All rights reserved. Zend Technologies, Inc. Stored Procedures on IBM i…What are they? • Operating System Objects but Database attributes • Typically written in SQL or RPG, but… • Can be written in nearly any HLL like Java, REXX, C, CL, C++, COBOL, FORTRAN, PL1 6 © All rights reserved. Zend Technologies, Inc. What do they look like? Simple SP data retrieval: CREATE PROCEDURE sales_price( IN Customer# CHAR(8), IN Product# CHAR(8), OUT price DECIMAL(12,2)) LANGUAGE SQL BEGIN SELECT CustPrice FROM priceTable WHERE CUSTNO=Customer# AND PRODNO=Product# AND active = ‘Y’ END 7 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. New Toolkit… 8 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Command line procedure creation … From PASE: QSH CMD(‘/usr/bin/db2 -t -f /QSYS.LIB/MPAVLAK.lib/QSQLSRC.FILE/MPSQL1.MBR') Or… From Native Command Line RUNSQLSTM SRCFILE(MPAVLAK/QSQLSRC) SRCMBR(MPSQL1) 9 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Examples in New Toolkit • New toolkit (XML Service) is made up of RPG CL DB2 Stored procedures (these are the pipes XML service uses!) 10 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. IBM Systems Navigator… 11 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. DB2 House Keeping… • Supports 2 types External (RPG, COBOL, etc.) SQL • Implemented via C programs (transparently) • Enhancements with each release of IBM i Be Careful if developing for multiple OS versions • SQL details: Supports multiple statements Supports iterative processing (Loop, Repeat Until, While) Get Diagnostics and feedback (SQLSTATE, SQLCODE) Can return discrete results or result sets 12 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Example… 13 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Before & After: PHP code original… 14 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Output… 15 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Before & After: PHP Code revised w/SP… 16 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. New, looks like the old… 17 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Observations about DB2 SP’s… • Advantages? Static objects Indexing, permanent objects Manage queries, DBA role is emerging Ever heard of little Bobby Tables? 18 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Security in no laughing matter • Security is better and this helps! See Kevin Schroeder’s piece in October 2011, SystemiNews http://www.iprodeveloper.com/article/other-languages/best- practices-for-developing-secure-php-web-apps-66102 19 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Stored Procedures with PHP for IBM i www.zend.com RPG – Hello World © All rights reserved. Zend Technologies, Inc. Start slow! • Don’t try to boil the ocean with your first SP! • How did you learn RPG? Reports Batch Interactive Subfiles, etc… • Start with simple SP’s and grow.. 21 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. An approach • Process: Create RPG Identify parameter interface Create procedure Test in Database Navigator Try it in PHP Increase complexity a little Repeat! 22 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. RPG 23 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Create the Procedure & test CREATE PROCEDURE MPAVLAK/RPGSP1 (INOUT Response CHAR ( 32)) LANGUAGE RPGLE NOT DETERMINISTIC CONTAINS SQL EXTERNAL NAME MPAVLAK/RPGSP1 PARAMETER STYLE GENERAL Then fire up System i Navigator 24 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Admire the result in DB Navigator 25 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Now try it in PHP 26 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Results from Script… 27 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Stored Procedures with PHP for IBM i www.zend.com Array from RPG to PHP © All rights reserved. Zend Technologies, Inc. Now an array Data elements are nice, but… Sometimes you need a little more… How about an array of information? 29 © All rights reserved. Zend Technologies, Inc. Have RPG build an array and use SQL… 30 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. SQL to create the SP & test in iNav CREATE PROCEDURE mpavlak/rpgsp2() LANGUAGE RPGLE EXTERNAL NAME mpavlak/rpgsp2 GENERAL 31 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Test it in iNav… 32 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Now open results in PHP… 33 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Results in browser… 34 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Stored Procedures with PHP for IBM i www.zend.com Record Set from RPG to PHP © All rights reserved. Zend Technologies, Inc. Have RPG build the result set and use SQL… 36 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. SQL to create the SP & test in iNav CREATE PROCEDURE mpavlak/rpgsp3() LANGUAGE RPGLE EXTERNAL NAME mpavlak/rpgsp3 GENERAL 37 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Now open results in PHP… 38 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Results in browser… 39 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Stored Procedures with PHP for IBM i www.zend.com Wrap it up... © All rights reserved. Zend Technologies, Inc. You’ve sold me but I need to ramp up fast! • Zend Services • Smart Start Combines many features in 2 week engagement • Setup • Foundations 1 training • Training Project • Consultant coach to develop first application Customer selects the initial application At the end of 2 weeks, customer often has working application Great way to build skills quickly Reduce time wasted “figuring stuff out on your own” 41 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc. Join us at ZendCon The premier PHP conference! October 22-25, 2012 Santa Clara, CA Conference Themes Conference Highlights PHP in 2012 - The latest PHP technologies and tools Learn how to leverage the latest mobile, HTML 5, testing and PHP best practices • Sessions focused on how to best develop and deploy PHP Zend Framework 2 - Hit the ground running Learn how to build faster, more modular and more expandable applications • Intensive tutorials for accelerated learning Development & The Cloud – A love story Learn how the latest developments in cloud-based services, infrastructure and best practices can benefit you • Sessions designed for all knowledge levels • PHP Certification crash courses and testing • Exhibit hall showcasing the latest products • Special networking opportunities during meals and events • Focus track for IBM i w w w. ze n d co n . co m © All rights reserved. Zend Technologies, Inc. Q&A www.zend.com mike.p@zend.com Please fill out your Session Evaluation! 43 Insert->Header & Footer © All rights reserved. Zend Technologies, Inc.