Reputation: 124
Been digging through stackoverflow for a few days now. Need to connect to a DB2 database with PHP. The following code doesn't return the error message or any helpful information, and instead just breaks Javascript later in the page.
include(TEMPLATEPATH.'/inc/library/adodb_lite/adodb.inc.php');
$DBName = 'DBNAME';
$Host = 'IPADDRESS';
$User = 'USER';
$Password = 'PASS';
$db = ADONewConnection('db2');
$dsn = "driver={IBM db2 odbc DRIVER};Database=$DBName;hostname=$Host;protocol=TCPIP;uid=$User;pwd=$Password";
if ($db->Connect($dsn)) {
echo "<div style='color:green;font-size:21px;'>Connection Successful.</div>";
} else {
echo "<div style='color:#cc0000;font-size:21px;'>Connection Failed db->Connect(dsn)</div>";
echo 'SQLSTATE: '.$db->ErrorNo()."<br>";
echo 'Message: '.$db->ErrorMsg()."<br>";
}
As followed by http://adodb.sourceforge.net/
Upvotes: 2
Views: 5855
Reputation: 6240
My thought is that your driver name doesn't match what you have in your odbcinst.ini file. Here's what I'm using:
odbcinst.ini
[iSeries Access ODBC Driver]
Description = iSeries Access for Linux ODBC Driver
Driver = /usr/lib/libcwbodbc.so
Setup = /usr/lib/libcwbodbcs.so
NOTE1 = If using unixODBC 2.2.11 or later and you want the 32 and 64-bit ODBC drivers to share DSN's,
NOTE2 = the following Driver64/Setup64 keywords will provide that support.
Driver64 = /usr/lib/lib64/libcwbodbc.so
Setup64 = /usr/lib/lib64/libcwbodbcs.so
Threading = 2
DontDLClose = 1
UsageCount = 1
Note that the "name" of this driver is defined to be "iSeries Access ODBC Driver" - whatever you put between the brackets [].
Now my PHP code looks like this:
$this->db_connection = new PDO("odbc:DRIVER={iSeries Access ODBC Driver};SYSTEM=$this->he_database_server;PROTOCOL=TCPIP", $temp_username, $temp_password);
So check to make sure your driver name (IBM db2 odbc DRIVER) is the same in your odbcinst.ini file.
Now if that doesn't solve the problem I would recommend you turn on ODBC logging by adding the following to your odbcinst.ini file:
[ODBC]
TraceFile = /tmp/odbc.log
Trace = Yes
That will write out a lot of information to a log file and give you a good idea of what's wrong. If that still doesn't get it working my only other suggestions are the most basic of things: make sure the username/password is correct, the AS/400 is reachable from the server running PHP, etc.
Upvotes: 2