Every now and then you find a gem stuffed way back in the back of the forums. I came across this one and since it solves a problem that I've seen asked more than once I thought I'd post it here for all to see. This was original posted by Mike Smith of IBM. Many thanks to Mike for this informative post.
For those of you who read the entire thread, I do have confirmation from IBM that the problem pointed out near the bottom of the thread is fixed in the latest beta.
From this point on it's Mike talking, not me.
=C=
I've had quite a few question as to how to access DB2 data inside of i5/OS. Here are a couple of examples that will attempt to show how this data can be retrieved.
The library calls that are used are:
- db2_connect - Returns a connection to a database
- db2_conn_errormsg - Returns the last connection error message and SQLCODE value
- db2_exec - Executes an SQL statement directly
- db2_fetch_both - Returns an array representing a row in a result set
- db2_close - Closes a database connection
The first example will retrieve the list of names from a database table named NAMES in library RMSMITH.
To run this example, create and populate the table NAMES. Copy this script into the directory /usr/local/Zend/apache2/htdocs.
To execute the script, point your browswer to (assuming that you called the script connect.php)
http://:89/Core/connect.php
<?php
try {
$conn = db2_connect("" , '', '');
if (! $conn) {
echo db2_conn_errormsg();
} else {
echo "Names from Table RMSMITH/NAMES";
$result = db2_exec($conn, "select * from rmsmith.names ");
if ($result) {
while ($row = db2_fetch_both($result)) {
echo "Hello, ".$row['NAME']."";
}
}
db2_close($conn);
}
} catch (Exception $ex) {
echo $ex;
}
?>
This second example will use the system catalog to display the list of objects that are in the library specified in the PHP variable $library.
<?php
$library = "RMSMITH";
try {
$conn = db2_connect("", '', '');
if (! $conn) {
echo db2_conn_errormsg();
} else {
echo "Tables in library $library";
$result = db2_exec($conn, "select * from qsys2.systables
where table_schema = '".$library."'");
if ($result) {
while ($row = db2_fetch_both($result)) {
echo $row['TABLE_NAME']."";
}
}
db2_close($conn);
}
} catch (Exception $ex) {
echo $ex;
}
?>

Comments