Вы находитесь на странице: 1из 24

An Introduction to PHP on the

IBM/i
By Trevor Seeney

History of IBM and ZEND


1 year free support from ZEND paid for by
IBM, after which you pay.
IMHO this policy penalized early adopters.
Which consequently retarded the
adoption of PHP on the IBM/i

A Case Study
An Item Inquiry Application
Implemented at Thermwell
With JQuery Mobile deployed

Case Study Item Inquiry

Application Parameters
Each inquiry request usually needs a Part
number and a Warehouse Id which are
passed in through the URL.
Information about Items is restricted by
user so we identify the user through a
login script.

Standard Required Processes


Directives for JQuery Mobile
User Login
Connection to the DB2 database
Load the PHP Toolkit for the IBM/i.

PHP is an
Interpretive Language
A backward step (IMHO) from objectorientated languages.
Forces a dependence on Copy modules.
Creates a lot of include statements.

Jquery Mobile Directives


<?php include '@JQueryMobile.php' ?>
<link rel="stylesheet"
href="http://code.jquery.com/mobile/1.3.1/jquery.mobile1.3.1.min.css" />
<script src="http://code.jquery.com/jquery1.9.1.min.js"></script>
<script
src="http://code.jquery.com/mobile/1.3.1/jquery.mobile1.3.1.min.js"></script>

PHP server-side
vs.
Javascript client-side
Can get complicated!
PHP variables:$part = $_GET["part"];

$whse = $_GET["whse"];
Javascript variables:<?php echo '<script>partparm = "' . $_GET["part"] . '";</script>'; ?>
<?php echo '<Script>whseparm = "' . $_GET["whse"] .

<!- Connect to Database -> <!- Connection succeeded. ->

Database Connection
<?php include '@ConnectDB.php' ?>

$conn = db2_connect("*LOCAL", '', '');


if ($conn) {
echo "<!- Connection succeeded.-> <br>\n";
}
else {
echo "<!- Connection failed. -><br>\n";
}

User Authentication
Uses cookies:
<?php
$cookie_name = 'logon';
if(!isset($_COOKIE[$cookie_name])) {
header("Location: i2iLogon.php");
}
else {
$user = $_COOKIE[$cookie_name];
}
?>

The PHP Toolkit for the IBM/i


What is it?
It is a means through which access to the
native IBM/i application to call subprocedures and stored-procedures to
retrieve data from the DB2 database and
make it available to the a PHP script

The Toolkit An Example


Given that we have a logged-on user Id, a
part Id and a warehouse code we want to
retrieve a value of for a particular piece of
information (e.g. stock-on-hand) and the
literal that should be placed on the screen
adjacent to it, plus an error code.
Stock On Hand:

9,340

Connect to the Toolkit


<?php

require_once('ToolkitService.php');

include '@ConnectTK.php' ;

?>

Connect to the Toolkit


include '@ConnectTK.php' ;
// connect to toolkit using DB2 credentials (can also leave blank for default authority)
try {
$conn = ToolkitService::getInstance('*LOCAL', '', '');
} catch (Exception $e) {
// Determine reason for failure.
// Probably database authentication error or invalid or unreachable database.
$code = $e->getCode();
$msg = $e->getMessage();
switch ($code) {
case 8001:
// "Authorization failure on distributed database connection attempt"
// Usually means a wrong DB2 user or password
echo 'Could not connect due to wrong user or password.';
break;

Connect to the Toolkit (cont.)


include '@ConnectTK.php' ;
case 42705:
echo 'Database not found. Try WRKRDBDIRE to check.';
break;
default:
echo 'Could not connect. Error: ' . $code . ' ' . $msg;
break;
} //(switch)
die; // couldn't connect...handle this however you wish
} //(try/catch)
echo '<!- ToolKit loaded -><br>';

* Source AlanSeiden.com

The Toolkit - Calling SubProcedures


The initial call is verbose
/* Procedure FeildAndValue Parms:User
10 Const
Part
15 Const
Warehouse
2 Const
FieldCode
15 Const
Label
30
$parms is an associate
Value
30
ErrorCode
7
*/
// define several input/output parms
$parms[] = $conn->AddParameterChar('in', 10, 'User', 'USER', 'SEENEY_T');
$parms[] = $conn->AddParameterChar('in', 15,'Part', 'PART', 'ACB160H');
$parms[] = $conn->AddParameterChar('in', 2,'Warehouse', 'WAREHOUSE', '**');
$parms[] = $conn->AddParameterChar('in', 15,'FieldCode', 'FIELDCODE', SOH');
$parms[] = $conn->AddParameterChar('out',30,'Label', 'LABEL', 'label');
$parms[] = $conn->AddParameterChar('out',30,'Value', 'VALUE', 'value');
$parms[] = $conn->AddParameterChar('out', 7,'Error', 'ERRORCODE', 'USR0001');

Legend: comment variable value

array

The Toolkit - Calling SubProcedures (continued)


$result = $conn->PgmCall('HTTP0010R', 'FRNMOD', $parms, null,
array('func'=>'FIELDANDVALUE'));
if (!$result) {
echo 'Error calling program. Code: ' . $conn->getErrorCode() . ' Msg: ' .
$conn->getErrorMsg();
}
echo '<br>Called program successfully.<BR><BR>';
echo 'LABEL: ' . $result['io_param']['LABEL'] . '<BR>';
echo 'VALUE: ' . $result['io_param']['VALUE'] . '<BR>';
echo 'ERRORCODE: ' . $result['io_param']['ERRORCODE'] . '<BR>';
Legend: Service Program Library Sub-Procedure

The Toolkit - Calling SubProcedures


Use Update $parms after first call
<?PHP
ProgramParameter::UpdateParameterValues( $parms,
array("FIELDCODE"=>"PUPC"));
$result = $conn->PgmCall('HTTP0010R', 'FRNMOD', $parms, null,
array('func'=>'FIELDANDVALUE'));
if (!$result) {echo '**Error** Code: ' . $conn->getErrorCode() . ' Msg: ' .
$conn->getErrorMsg(); }
else {echo $result['io_param']['LABEL']; }
?>

Update one parameter, then same as before.

The Toolkit - Calling StoredProcedures


Much easier to consume than subprocedures
Read result-set into arrays
Use index to parse
Create standard test-handler

Example Stored Procedure Call


// Prepare Stored Procedure call //
$proc = 'CALL FRNMOD.OOSCOMMENT(?,?,?)';
$stmt = db2_prepare($conn, $proc) or die("db2_prepare failed " .
db2_stmt_error(). " and " .db2_stmt_errormsg());
db2_bind_param($stmt, 1, 'user', DB2_PARAM_IN,DB2_CHAR);
db2_bind_param($stmt, 2, 'part', DB2_PARAM_IN,DB2_CHAR);
db2_bind_param($stmt, 3, 'whse', DB2_PARAM_IN,DB2_CHAR);
db2_execute($stmt);
while (db2_fetch_row($stmt)) {
$Fld1 = db2_result($stmt, 0);
* Set your procedure
$Fld2 = db2_result($stmt, 1);
* Prepare the statement
$Fld4 = db2_result($stmt, 4);
* Specify input parameters
$Fld5 = db2_result($stmt, 5);
* Consume result set and output as a table
echo '<tr>';
echo '<td>' . $Fld1 . '</td>';
echo '<td>' . $Fld2 . '</td>';
echo '<td>' . $Fld4 . '</td>';
echo '<td>' . $Fld5 . '</td>';
echo '</tr>';
}

Sample Stored Procedure


Output

Debbuging
Is difficult.
Start a service program
Embed JavaScript alerts or PHP echos into
your script
Use other technical resources (Ron, Steve or
Julie)

Enough for now

Вам также может понравиться