You are currently on IBM Systems Media’s archival website. Click here to view our new website.


Sometimes You Need to be Flexible

PHP-built utility scripts provide more flexibility


In “PHP Revisited,” we explained how PHP makes it really easy to do things like consume Web services but we haven’t touched on how the flexibility of the language makes it possible to do things that are all but impossible in RPG.

For example, think about the challenges involved in writing an RPG program that lets the user specify a qualified file name (any file name) and then produce a listing of that file’s content. While dynamic SQL can do some aspects of this, you still have a challenge in how to store, format and output the data.

The first problem is that RPG wants to know the format of the file at compile time. That’s not possible since we’ve already said that we want to be able to process any file. So perhaps we could use a program-described file and simply define the record length as that of the largest record we have.

So far so good, but we now face the problem that all we have is one huge character field and that won’t display very well, particularly if it contains packed-decimal or binary fields. In theory, even this situation is manageable in that we can use system APIs to obtain a list of the fields in the record, their formats and offsets within the record—but how to apply that knowledge? One approach might be to use a set of standard fields, each of which is defined as BASED, and then calculate the value for the individual basing pointers from the address of the start of the record plus each field’s offset. That would let us obtain the values in the individual fields, but we’re still faced with editing them, formatting the resulting output and more.

As you can see, it’s not a task for the faint of heart but as Jon often says, “But it might be fun!” If, like Susan, you don’t see any way in which that amount of work could be construed as “fun” then you’ll like that PHP’s flexible nature makes it a natural choice for utility functions such as this. Would you believe that a basic PHP script to accomplish our original task could consist of as little as 50 lines of code? We’ll prove it to you.

Looking Into the PHP Script

Our PHP script starts out after the user has entered the desired table (file) and schema (library) names on a simple HTML form. If you want to see the following code in action before examining it, check out a short demonstration video.

This first section of the script is pretty standard stuff. It begins by obtaining the table ($dbTable) and library ($dbLib) names from the browser input (i.e., the $_POST array).


// Get requested table and library names
$dbTable = $_POST['table'];
$dbLib = $_POST['library'];

// Use defaults values if nothing was supplied
if ($dbLib == '') $dbLib = "JONSDEMOS";
if ($dbTable == '') $dbTable = "CUSTOMERS";

$conn = i5_connect("localhost", "username", "password");
if (!$conn) die("No connection");

If no value is retrieved, default values are used. A connection is then made using the i5_connect() function, supplying the relevant user name and password. If we’re unable to make the connection, we simply terminate (die) and issue an error message.


Jon Paris is a technical editor with IBM Systems Magazine and co-owner of Partner400.

Susan Gantner is a technical editor with IBM Systems Magazine and co-owner of Partner400.



2019 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

Are You Multilingual?

Rational enables development in multiplatform environments

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters