0

I have some problem. I have been looking it for almost a week. I have done include ibm_db2.dll, change php version but not working. When I run the db2 query, it shows Fatal error: Uncaught Error: Call to undefined function db2_connect() in C:\laragon\www\hpc_dev\db2_conn.php:15 Stack trace: #0 C:\laragon\www\hpc_dev\index.php(3): include() #1 {main} thrown in C:\laragon\www\hpc_dev\db2_conn.php on line 15

Currently I am using php version 7.4.12 64bit (ts) using laragon in Windows 10. I also have tested using Xampp and the same problem occur. Below are my codes to connect to ibm db2:

db2_conn.php:

<?php

ini_set("display_errors", 1);

$database = 'DB2';
$user = 'xxx';
$password = 'xxx';
$hostname = 'xx.xx.xx';
$port = 60000;

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

if ($conn) {
    echo "Connection succeeded.";
    db2_close($conn);
}
else {
    echo "Connection failed.";
}

?>

My code to show data from db2:

<?php

include("db2_conn.php");
   
$sql = "SELECT * FROM EMPLOYER WHERE EMPR_LOGIN_NAME = 'xxxxx'";

if ($conn) 
{
 $stmt = db2_exec($conn, $sql);
 $row = db2_fetch_assoc($stmt);

 echo $row['SECTOR_DESC'];
 db2_close($conn);
}
?>

I don't know either the problem comes from db2 extension or others. Please help me. For information, I have download the ibm db2 extension here: https://github.com/ibmdb/php_ibm_db2

Thanks

  • 2
    In addition to downloading the extension, did you enable it in you `php.ini` and restart the webserver? – Barmar Nov 13 '20 at 01:03
  • @Barmar yes, I have enable and restart the webserver – user3239674 Nov 13 '20 at 06:14
  • Did you install the prerequisite CLIDRIVER ? – mao Nov 13 '20 at 10:13
  • @mao yes, install – user3239674 Nov 13 '20 at 12:25
  • If you open a command prompt (cmd.exe) and enter the command `db2level`, what is the text output of that command? – mao Nov 13 '20 at 12:54
  • @mao it show `DB21085I This instance or install (instance name, where applicable: "DB2") uses "64" bits and DB2 code release "SQL11054" with level identifier "0605010F". Informational tokens are "DB2 v11.5.4000.1449", "s2006161200", "DYN2006161200WIN64", and Fix Pack "0". Product is installed at "C:\PROGRA~1\IBM\SQLLIB" with DB2 Copy Name "DB2COPY1".` – user3239674 Nov 14 '20 at 02:05
  • I used laragon , with the pre-delivered php version 7.2.19, and then added the matching TS build for php_ibm_db2, configured php.ini, bounced the services, and the `db2_connect()` works correctly in a script in the terminal window. It helps to verify in the terminal window that you can connect at the command line if you have a db2 client that has that functionality, else can use `db2cli validate -connect ...` – mao Nov 18 '20 at 18:08

1 Answers1

0

Your question omits details of the command-lines and actions you performed.

Most likely you missed a step in the configuration.

Remember to open the laragon terminal and verify that the Db2 driver is loaded:

php -m | grep ibm

should return ibm_db2 if you have correctly configured the correct version of the extension.

If you do not see that the ibm_db2 extension is loaded then any db2_connect will fail until you resolve the missing module.

For information the current laragon_full (November 2020) delivers PHP 7.2 TS, and that works correctly with the php_ibm_db2.dll TS for v7.2.x "out of the box" (which you must first download and configure).

Additionally, if I upgrade the PHP version for laragon to PHP 7.4.12 TS, and subsequently download and configure the matching php_ibm_db2.dll TS for php v7.4.x, then it all "just works" without any issues on Win 10 x64 2020 to connect to Db2-LUW and run queries etc.

It helps to verify that your Db2 client is correctly configured. You can do that independently of PHP. If you are using the clidriver Db2-client (the smallest footprint driver) you can configure the db2dsdriver.cfg XML file and then run db2cli validate -connect -dsn -user ... -passwd ... to verify that the connection works. If you are using either the Db2-runtime-client or the Db2 full client, or a local Db2-server, you can run (in the laragon terminal):

(after configuring your Db2 node directory and Db2 db directory via appropriate configuration commands )::

set DB2CLP=**$$**

db2 connect to $yourdsn user ... using ...
mao
  • 11,321
  • 2
  • 13
  • 29