PHP与DB2的连接


PHP Connection to DB2

我正在尝试从php脚本连接数据库。

<?php
$database = '****';
$user = '*****';
$password = '******';
$hostname = '********';
$port = 50000;
$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.";
}
?>

我得到以下错误。

在第10行上调用/home/s1.php中未定义的函数db2_connect()

有人能帮我解决以下问题吗。

我认为,您需要安装DB2扩展。

这是链接http://php.net/manual/en/ibm-db2.installation.php

除了通过PECL安装db2扩展之外,在运行pecl install之前,还需要在系统上下载开发头文件和库。

这是一个创建3个文件的shell脚本:

  • Dockerfile
  • docker-compose.yml
  • html/index.php

显然,请在复制/粘贴之前阅读脚本,以确保您感到舒适简单地说,它使用catheredoc语法来输出文本到文件:

#! /bin/sh
cat << 'EOF' > docker-compose.yml
version: '3'
services:
  odb-web:
    build: .
    ports:
      - 3030:80
    volumes:
      - ./html:/var/www/html
  mydb2:
    image: ibmcom/db2
    ports:
      - 50000:50000
    volumes:
      - ./data:/database
    environment:
      LICENSE: accept
      DB2INST1_PASSWORD: ChangeMe!
      DBNAME: testdb
    privileged: true
EOF
cat << 'EOF' > Dockerfile
FROM php:7.3-apache
# To build the ibm_db2 extension, the DB2 application development header files and libraries must be installed on your system. 
# DB2 does not install these by default, so you may have to return to your DB2 installer and add this option. 
# The header files are included with the DB2 Application Development Client freely available for download from 
# the IBM DB2 Universal Database » support site: https://www.ibm.com/developerworks/downloads/im/db2express/index.html
# https://github.com/php/pecl-database-ibm_db2
# Download linuxx64_odbc_cli.tar.gz from https://public.dhe.ibm.com/ibmdl/export/pub/software/data/db2/drivers/odbc_cli/
## add header files and libraries
RUN mkdir -p /opt/ibm/ && curl https://public.dhe.ibm.com/ibmdl/export/pub/software/data/db2/drivers/odbc_cli/linuxx64_odbc_cli.tar.gz | tar -xz -C /opt/ibm/
# if you prefer to keep the file locally, download it and use:
# ADD odbc_cli/linuxx64_odbc_cli.tar.gz /opt/ibm/
## set env vars needed for PECL install
ENV IBM_DB_HOME=/opt/ibm/clidriver
ENV LD_LIBRARY_PATH=/opt/ibm/clidriver/lib
## install ibm_db2 drivers
RUN pecl install ibm_db2
RUN echo "extension=ibm_db2.so" > /usr/local/etc/php/conf.d/ibm_db2.ini
EOF
mkdir html && cat << 'EOF' > html/index.php
<?php
$database = 'testdb';
$user = 'db2inst1';
$password = 'ChangeMe!';
$hostname = 'host.docker.internal';
$port = 50000;
$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.'n";
    db2_close($conn);
}
else {
    echo "Connection failed.'n";
}
?>
EOF

在运行该脚本后(或者如果在windows上,手动复制粘贴文件内容),您将有3个文件,并且可以运行docker-compose up -d来使用apache构建php7.3映像,然后它会显示两个容器——php-apache服务器和ibmdb2数据库。

浏览到http://localhost:3030你应该看到";连接成功";。

您可以根据需要修改html/index.php文件,并刷新页面以测试结果。

只需从PECL 下载合适的版本