简体   繁体   中英

Unable to connect DB2 with PHP

I tried to connect to a remote DB2 database located on IBM AS400 server. I'm running debian 10 with PHP7.3 and nginx. I installed correctly DSDriver and ibm_db2 php extension with pecl install ibm_db2 command.

Here's my php code to test connection :

<?php

$driver = '{IBM DB2 ODBC DRIVER}';
$database = 'MY_DB';
$user = 'USERNAME';
$password = 'PASSWORD';
$hostname = 'MY_IP';
$port = 50000;


$conn_string = 'DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;';
$conn_string .= 'HOSTNAME=$hostname;PORT=$port;PROTOCOL=TCPIP;';
$conn_string .= 'UID=$user;PWD=$password;';

try {
$conn = db2_connect($conn_string, '', '');

if (! $conn) 
{
    echo db2_conn_errormsg();
}

else { 
    echo "Conn OK ";
}

db2_close($conn);
}

catch (Exception $e) {
        echo $e;
}


?>



The result is :

"Connection failed."

What am I missing, what could be wrong ?

Your question mentions "DSDriver" and Db2 for i-series, and your code example shows 'DRIVER={IBM DB2 ODBC DRIVER};

If you mean the IBM Db2 Data Server driver then this cannot connect via ODBC to Db2 for i-series unless either you connect via a pre-existing Db2-connect gateway, or you add a Db2-connect personal licence to your workstation.

If you installed on Debian the 'IBM i access' for Linux product (64 bit) this product will let you use unixODBC to connect to Db2-for-i, as long as your unixODBC version is a certain minimum level ( 2.2.13 or higher) and your PHP code uses the correct driver name which is DRIVER={iSeries Access ODBC Driver}; . IBM mentions the unixODBC version dependency in itsdocumentation , which may change from time to time,

After configuring unixODBC with the IBM i access product, it is helpful to get the isql tool to connect to the Db2-for-i before trying PHP, as it will verify the connectivity,port,credentials along with the ODBC interface. It may be easier to troubleshoot that before programming with PHP. If you get the isql working correctly with Db2-for-i, then PHP should just work as long as your code specifies the correct driver name.

Pay attention to the documented restrictions with the unixODBC interface to Db2 and the differences between the Windows driver and Linux driver that IBM describes in the redbook for this topic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM