The IBM i and PHP logos surround a laptop
August 29, 2024

How to Use Db2 Services for PHP Apps on IBM i

IBM i
PHP Development

IBM i Db2 services provide developers with access to a great deal of vital system information, and leveraging these services wisely can improve general productivity. Add in a little PHP, HTML, and CSS, and Db2 services can give users access to information formerly in the green screen or buried in ACS menus. 

In this blog, I will demonstrate a simple PHP application that is functional, attractive, and extensible using Db2 services, provide insights for why I took the steps that I did, and discuss options you might choose along the way to building your own PHP application.

Back to top

Is Db2 Still Used?

Yes, Db2 services for PHP apps on IBM i are still used. Db2, or Database 2, services for PHP apps on IBM i cover many system-provided views, procedures, and functions. They are grouped into various categories, such as application services, performance services, plan cache services, and utility services.

Back to top

Getting Started With Db2 Services for PHP on IBM i

I had two principal goals for this discussion. First, to provide a useful entry level PHP project any IBM i shop can leverage and enhance, even with little or no PHP/web talent. Second, to illustrate the power of Db2 services via an example that can be used on a daily basis. To achieve these goals, I chose to follow a few simple rules as I created this Db2 PHP walkthrough.

First, no JavaScript. While JavaScript can be useful, it can also get a little unruly. It is easy to get lost in the mire of a good JS library rather than focus on the theme of the exhibit, which is the simplicity of PHP and Db2 services. Instead, there is heavy focus on PHP, HTML, and CSS for some lovely formatting.

Next, I went totally procedural on the style of code. Reflecting on this example, those in the community with an appreciation for object-oriented programming (OOP) will see exactly where to create the class. That was intentional. But I wanted brand new developers who maybe hail from the RPG world to be able to accept the code. If you have zero experience with PHP and even web development, you are in the right place. If you do have experience, maybe this code will stimulate a few ideas.

Lastly, I created an application structure emblematic of an RPG Service Program, which we will talk about in a minute.

Back to top

Walkthrough: Using Db2 for IBM i PHP Applications

All code used in this walkthrough can be accessed on Github.

The application begins with a bootstrap of index.php, which is implicitly called from the URL. The default page is meant to be a dashboard, an ideal starting location that provides immediate value to developers or managers via Db2 services functions and views. In the old days, we needed the toolkit to retrieve a lot of these values. Today, they are built right into the services through the use of a simple SQL statement.

Step One: Building and Understanding the Db2 PHP Dashboard

I selected three pieces of information for the dashboard. The table at the top, pictured below, contains popular system values that most software vendors might need to generate license keys and such. The table can be altered quite simply by adding more columns to the SQL statement in the function provided.

An example of a PHP application dashboard built with Db2 services

Current disk utilization is something that should be monitored periodically, so adding an HTML 5 "meter" seemed appropriate. HTML5 added a lot of dynamic looking features, and the meter is one of them. In this example, you can see the meter is yellow because I set the low value to 30, optimum value to 25, and high value to 90. This yields a green, yellow, red, kind of indicator that is easy to react to. Plus, you can change the values to suit your preferences. 

I borrowed some of the logic for the Inquiry message from an example Simon Hutchinson created, which he borrowed from the IBM manual. The only twist I added was an exception join of the table to itself to capture the pending escape messages. The performance of these is pretty good, when you are not calling a web service. The example above illustrates the look when there is an escape message pending and highlighting a red error message heading. 

If there are no messages, the dashboard calms down with a green message saying all’s clear, as illustrated below.

Dashboard displaying No Inquiry Messages

Many more components can be added to this page and with some styling, a bunch of widgets showing various pieces of important information could be included. 

Step Two: Styling With Db2 Services

Let’s talk about the elePHPant in the room: the selector at the top looks a lot like JavaScript. That’s because I wanted the application to look nice, and when I created the first version, it really screamed RPG Developer at Windows 3.1. 

As I am a serious back-end developer, I reached out to a member of the Zend Professional Services team, Guido Faecke, to assist with color and style through CSS and adding the "keyframe" selector. This aspect is probably the most intricate piece of code, and you can find references to this on the web, and it met my goal of styling with basic tools. 

Check out the before and after images and you’ll probably agree.

Before the CSS

Before the CSS example

 

After the CSS

After the CSS example

 

Step Three: Overall Application Structure

Taking a look at the structure of the overall application, you see several artifacts. Many of these could be arranged in a myriad of ways.

As I indicated, index.php kicks it all off. This file executes a "require" of the dashboard.php file to fill out the page. There is a standard header and footer and a menu option that creates the selector at the top of the page stored in a separate directory called "template." Organizing your code is a great first step when implementing web pages. But if you don’t get it right the first time, or don’t agree with my style, feel free to reorganize as you refactor.

List of relevant files

 

The menu.php file provides the selector information for the "keyframes" at the top of the page. As you add new pages to the application, simply add a line to this page. 

I created the pages separately with very little content because I plan to add more to each page as time goes on. Examples of things to add to the group PTF page include version and release of the OS, a list of licensed programs installed and their status, etc. If you don’t want to separate pages for group PTFs, you can consolidate and make room for another page containing additional information that matters to your organization.

The Group PTF status page is probably the most basic and similar in structure to the other two for group PTF Currency and Active Jobs by temporary storage. These effectively run the Db2 service and receive an associative array. 

List of PTF Group Status

For this walkthrough, I created a very basic function that takes any associative array and creates an HTML table with the keys of the array as headers for the table and the values as rows. When you run the PTF Group Currency page on your system, you may notice it might be slower than the other pages. This is not an issue with PHP, but with the service in that it actually goes out to the IBM site and compares your PTF groups to what is optimal at IBM and gives you the results. You can see at a glance which groups are behind and by how much. 

A table showing PTF Group Currency
View Table in New Tab


Once upon a time, all of this had to be completed by hand, which took up a great deal of time. Now it can all be completed with the click of a mouse.

Step Four: ServiceFunctions.php

The file "ServiceFunctions.php" contains most of the guts of the application, much like a service program might do. In here, you will find a function for each of the major pages and tasks for the common activities, like connecting to the database and formatting an HTML table. This file would be a great candidate for a class for those out there who are ambitions and want to start playing in the OOP realm of PHP. 

List of Active Jobs by Temp Storage

 

Back to top

Final Thoughts

Now that I have created this PHP application using Db2 for IBM i, I can continue optimizing in the days ahead. For example, I have many ideas for improving performance. I will probably cache the output of the GROUP PTF Currency page using SQLite or maybe a REDIS database. I don’t think I would store this in Db2 as it is not data that would comprise a system of record. Another idea is to add connectivity to multiple partitions. This is quite easy via SQL and services. 

I hope this Db2 PHP walkthrough for IBM i is a nice catalyst to get your creative juices flowing. The application created here will take advantage of the code and ZendPHP stack available on our site, and you could have this up and running in minutes. As a reminder, all code used in this guide can be found here on Github.

In general, using Db2 services for PHP applications on IBM i is an efficient and useful way to create and improve modern web application productivity - and Zend is here to help. From providing secure and supported PHP runtimes through ongoing Professional Services and more, our team is at your disposal.

No In-House PHP Experience? No Problem!

Zend Admin as a Service and other Professional Services are built to support your mission-critical PHP applications, including those on IBM i systems. Partner with us today to make our PHP experts your experts.

Discover Admin as a Service  Explore Professional Services

Back to top

Additional Resources

Back to top