Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to PHP 5 and IBM DB2 Building web applications with PHP 5.2 and IBM DB2.

Similar presentations


Presentation on theme: "Introduction to PHP 5 and IBM DB2 Building web applications with PHP 5.2 and IBM DB2."— Presentation transcript:

1 Introduction to PHP 5 and IBM DB2 Building web applications with PHP 5.2 and IBM DB2

2 2 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 Overview ❙ From Personal Homepage Tools to PHP 5.2 ❙ PHP 5.2 and databases ❙ What‘s new in PHP 5.2 ? ❙ PDO: PHP Data Objects ❙ Why IBM DB2 ? ❙ PHP and IBM DB2 ❙ Introduction of ibm_db2 extension ❙ Functions and special features

3 3 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 About the presenter ❙ Thorsten Rinne ❙ Born in 1977 ❙ Graduated in computer sciences ❙ Software developer at Mayflower GmbH / ThinkPHP, Munich, Germany ❙ Rating and reporting applications ❙ Business critical bank applications ❙ 1999: started PHP programming ❙ 2001: started the development with the open source FAQ management software phpMyFAQ (www.phpmyfaq.de) ❙ 2002: first contact with IBM DB2 ❙ 2005: porting phpMyFAQ to IBM DB2

4 4 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 From Personal Homepage Tools to PHP 5.2 ❙ Released as a collection of some Perl scripts in 1995 by Rasmus Lerdorf, called „Personal Homepage Tools“. ❙ 1997: PHP/FI was rewritten in C ❙ PHP 3 with simple features of object orientation and a new core by Zeev Suraski and Andi Gutmans was released 1998 ❙ In 2000 PHP 4 was released with better object orientation and Zend Engine 1 ❙ 2004: PHP 5.0 brought many missing features for object orientated programming ❙ PHP 5.2 was released in November 2006 ❙ In development: PHP 6 with unicode support

5 5 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 What‘s new in PHP 5.2 ? ❙ New filter and JSON extension ❙ Object orientated zip extension ❙ Improved memory manager ❙ Improved PDO and SPL methods ❙ Added new functions ❙ Fixed many bugs

6 6 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 PHP and databases: Basics ❙ PHP supports all important database interfaces: ❙ MySQL ❙ PostgreSQL ❙ MS SQL server ❙ Oracle ❙ SQLite ❙ Firebird / Interbase ❙ Sybase ❙ Informix ❙ ODBC ❙ …and IBM DB2 ❙ But: every database needs its own extention!

7 7 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 PHP and databases: PHP 5.2 ❙ PDO features a common and efficient interface ❙ Special extensions like MYSQLi ❙ Ready for PEAR ❙ Support for ❙ File based databases like SQLite ❙ Connection with internal and external database servers ❙ ODBC

8 8 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 Introduction of PDO ❙ Abstraction of the data access (standard API) ❙ Plugins for various database systems ❙ Object-orientated ❙ Support for PHP iterators ❙ Very fast because of the C implemention ❙ PDO… ❙ … drivers are available in PECL since PHP 5.0 ❙ … is part of the PHP core since version 5.1 ❙ … delivers pre-compiled Windows DLLs

9 9 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 Features of PDO ❙ Prepared statements ❙ SQL error codes ❙ Database portability features ❙ Support for transactions ❙ Scrollable cursors ❙ Support for PHP error handling and PHP exceptions ❙ Planned for a future release: support for LOBs

10 10 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 Using PDO prepare('SELECT * FROM faquser'); $stmt->setFetchMode(PDO::FETCH_NAMED); $stmt->execute(); while ($row = $stmt->fetch()) { print $row. " "; } $db = null; } catch (PDOException $e) { print "Error: ". $e->getMessage(). " "; die(); }

11 11 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 Why using IBM DB2 ? ❙ IBM DB2 is the database for enterprise applications ❙ Available for z/OS, iSeries, Linux, UNIX, and Windows ❙ Highly scalable ❙ Starts with IBM DB2 Express-C for 2 CPUs ❙ Available for systems with more than 64 processors ❙ Clustered stystems up to 1024 nodes ❙ Performance leader on Linux (TPC-H) ❙ Current version: IBM DB2 V.9.1 ❙ for relational and XML data ❙ SQL queries und XQuery ❙ SAP certified

12 12 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 IBM DB2 in the net Internet/Intranet Webbrowser DB2 client PHP / Webserver DB2 (Linux, iSeries, pSeries) DB2 (z/OS) DB2 Connect

13 13 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 PHP and IBM DB2 ❙ PHP offers four possibilities: ❙ ODBC (ext/odbc) ❙ The ibm_db2 extension for IBM DB2, IBM Cloudscape, and Apache Derby ❙ PDO_ODBC ❙ PDO_IBM (in development, no documentation) ❙ Open source software ❙ Download: http://www.php.net

14 14 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 PHP and IBM DB2: ODBC (ext/odbc) ❙ PHP core extension ❙ Typically compiled against the generic ODBC driver ❙ Can be compiled against the IBM DB2 client library (./configure --with-ibm-db2) ❙ Disadvantages: ❙ Scrollable cursors are very slow ❙ No support for IN/INOUT stored procedures

15 15 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 PHP and IBM DB2: ibm_db2 (ext/ibm_db2) ❙ Download source code and binaries from PECL repository ❙ Developed and supported by IBM ❙ Full support for stored procedures and LOBs ❙ Very fast because of its C implementation! ❙ Disadvantage: ❙ Complete new extension ❙ Not 100% free of bugs

16 16 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 PHP and IBM DB2: PDO_ODBC ❙ Fast, easy and C based standard interface for PHP ❙ PDO_ODBC will be compiled against the generic IBM DB2 libraries ❙ Standard API for better software development ❙ Fast and object orientated interface ❙ Disadvantages: ❙ Only for PHP 5.x and later ❙ Still in development

17 17 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 First contact with ext/ibm_db2 ?"; $conn = db2_connect($database, $user, $password); $stmt = db2_prepare($conn, $sql); $res = db2_execute($stmt, array(0)); while ($row = db2_fetch_assoc($stmt)) { print ' '. $row['REALNAME']. ' '; print $row['EMAIL']. ' '; }

18 18 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 What‘s happening ? ❙ The first connection takes a long time … ❙ With the first connection DB2 binds some system resources ❙ Solution: ACTIVATE DATABASE faq ❙ Persistent connections? Be careful, but really great in read mode

19 19 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 Closing the first connection ❙ Automatic disconnection after PHP script execution ❙ Explicit disconnection with: db2_close($conn); ?>

20 20 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 Error handling with ibm_db2 ❙ SQLSTATE return values ❙ db2_conn_error() ❙ db2_stmt_error() ❙ SQL error message ❙ db2_conn_errormsg() ❙ db2_stmt_errormsg()

21 21 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 Prepared statements with ibm_db2 ❙ The ibm_db2 extension supports prepared statements ❙ The values are binded at their position with the placeholder „?“: $sql = "SELECT id FROM faquser WHERE realname = '?'"; $stmt = db2_prepare($conn, $sql); $res = db2_execute($stmt, array('Thorsten Rinne'));

22 22 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 Fetching data by row ❙ The column number is the index: $row = db2_fetch_array($stmt); // $row[0] ❙ The column name is the index: $row = db2_fetch_assoc($stmt); // $row['COLUMN_NAME'] ❙ The column number and the name are the index: $row = db2_fetch_both($stmt); ❙ PHP object, column names are object properties: $row = db2_fetch_object($stmt); // $row->COLUMN_NAME

23 23 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 Differences to MySQL (I) ❙ IBM DB2 does not return the number of affected rows for SELECT statements ❙ Solutions: ❙ Slow: Scrollable Cursors $stmt = db2_exec($conn, "SELECT realname FROM faquser", array('cursor' => DB2_SCROLLABLE)); $num = db2_num_rows($stmt); ❙ Better: $stmt = db2_exec($conn, "SELECT COUNT(*) FROM faquser"); list($num) = db2_fetch_array($stmt); ❙ The best: $stmt = db2_exec($conn, "SELECT realname, realname FROM faquser"); while ($row = db2_fetch_assoc($stmt)) { … }

24 24 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 Differences to MySQL (II) ❙ IBM DB2 supports no LIMIT ❙ Solutions ❙ ROW_NUMBER OVER() syntax (ANSI SQL) ❙ Scrollable cursors ❙ Sequences and auto-generated columns for easy WHERE clauses ❙ Column names are always UPPERCASE: print $row['REALNAME']; print $row['realname']; // Notice!

25 25 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 Using XQuery in V.9.1 ❙ IBM DB2 V.9.1 can save XML data ❙ Example: $xquery = '$document/costumer/phone'; $res = db2_exec($conn, "SELECT XMLQUERY('$xquery' PASSING INFO AS \“document\") FROM COSTUMER");

26 26 „PHP 5 and IBM DB2“ © Thorsten Rinne 2006 Any questions? ???

27 Thanks for your attention! Thorsten Rinne, Dipl.-Inf. (FH) Mayflower GmbH Sendlinger Str. 42a D-80311 Munich Germany +49 (89) 24 20 54 – 31 rinne@mayflower.de


Download ppt "Introduction to PHP 5 and IBM DB2 Building web applications with PHP 5.2 and IBM DB2."

Similar presentations


Ads by Google