PHP与DB2的连接

时间:2016-05-06 07:42:12

标签: php 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()

有人可以帮我解决以下问题。

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

只需从PECL下载合适的版本

答案 2 :(得分:0)

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

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

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

很显然,请在复制/粘贴之前阅读脚本,以确保您感到舒适 它的功能,但简而言之,它使用cat heredoc语法输出 文本到文件中

#! /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来构建php 7.3映像,然后它会弹出两个容器-php apache服务器和ibm db2数据库。

浏览到http:// localhost:3030,您应该看到“连接成功”。

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