2

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/

2
  • You may find solution in this thread: stackoverflow.com/questions/6980927/… Commented Oct 11, 2012 at 13:01
  • Like I said I'd been all over stackoverflow, reading relevant tags. Unfortunately unlike in that thread, I'm not seeing any error messages or useful information. Commented Oct 11, 2012 at 13:36

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I was sure I had overlooked something important, like odbcinst.ini. I'm using adodb_lite, thought I installed the iSeries ODBC driver, but don't see odbcinst.ini (IBM/DB2 newb).
The odbcinst.ini file will be installed when you install odbc. sudo apt-get install unixodbc php5-odbc On Ubuntu it will put odbc.ini and odbcinst.ini in /etc. When I installed the iSeries driver I had to also do: sudo cp /opt/ibm/iSeriesAccess/lib/* /usr/lib to get the .so files in the place where ODBC expected them. Have you tried going to a command line and using the isql command to see if you can connect to the AS/400?
Tried downloading the DB2 ODBC driver from IBM from www-304.ibm.com/support/docview.wss?rs=4020&uid=swg27016878 (per uzyn.com/how-to-connect-to-db2-on-php-via-odbc), but once done and extracted there's no odbcinst.ini. Am I downloading the wrong package?
I downloaded the driver from www-03.ibm.com/systems/power/software/i/access/… (You will need to have an account on their site to download the driver). It's an RPM so I used "alien" to convert it to a DEB package for Ubuntu: sudo alien iSeriesAccess-6.1.0-1.2.i386.rpm However to answer your question - the odbc.ini and ocbcinst.ini files will be installed when you install unixodbc (and you'll also need the php5-odbc package on Ubuntu so PHP can use ODBC).
Thanks for your help Benny. The site and connecting DB2 are on Windows servers with PHP 5.3.8. Is there something comparable to unixodbc I can use?
|
2

try using this

http://www.sitepoint.com/php-database-db2/

1 Comment

Good info, but didn't quite help my scenario. Thanks though Dilip!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.