沃梦达 / 编程问答 / php问题 / 正文

php pdo 连接到 DB2 不同的 CODEPAGE

php pdo connected to DB2 different CODEPAGE(php pdo 连接到 DB2 不同的 CODEPAGE)

本文介绍了php pdo 连接到 DB2 不同的 CODEPAGE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在连接到 DB2 DB

I'm connecting to DB2 DB

$sql = 'CALL procedures.name(1,1,'text',1,1,'2017-08-30','2017-08-31',?,?)';
    try {
        $con = new PDO("idb:all_the_connections_stuu",'user','pass',
           [
              PDO::ATTR_PERSISTENT => FALSE,
              PDO::ATTR_ERRMODE => PDO:ERRMODE_EXCEPTION,
              PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL,
              PDO::ATTR_AUTOCOMMIT => 0
           ]
        );
        $stmt = $con->prepare($sql);
        $stmt->bindParam(1, $errorNumber, PDO::PARAM_INT);    //also trying without PDO::params  
        $stmt->bindParam(2, $errorCode, PDO::PARAM_STR, 800); //and with |PDO::PARAM_INPUT_OUTPUT
        $stmt->execute();        //return *TRUE*
        var_dump($errorNumber);  //return NULL
        var_dump($errorCode);    //return NULL
        var_dump($stmt->fetchAll(PDO::FETCH_ASSOC)); //returns Error
        $stmt->closeCursor();
        $stmt = null;
    } catch (PDOException $e) {
        echo ($e->getMessage());
    }

得到了这个错误:

Fatal error:  Uncaught PDOException: SQLSTATE[57017]: <<Unknown error>>: -332 
[IBM][CLI Driver][DB2] SQL0332N  
Character conversion from the source code page "" to the target code page "" is not supported.  
SQLSTATE=57017
     (SQLFetchScroll[-332] at /home/user/shared/PDO_IBM-1.3.4/ibm_statement.c:1306) in /var/www/html/server/testsIBM/index.php:80
    Stack trace:
    #0 /var/www/html/server/testsIBM/index.php(80): PDOStatement->fetchAll(7, 0)
    #1 {main}
      thrown in /var/www/html/server/testsIBM/index.php on line 80

如果我用 db2_connect 连接

If I connect with db2_connect

$con = db2_connect("DATABASE=DB2D;HOSTNAME=10.243.13.65;PORT=5000;PROTOCOL=TCPIP;USERNAME=asdf23;PASSWORD=asdfasf1","", "")

并输出 db2_client_info($con)db2_server_info($con) 我看到来自客户端的 CONN_CODEPAGE = 819 和 DB_CODEPAGE 来自服务器 = 1208.

and output db2_client_info($con) and db2_server_info($con) I see that CONN_CODEPAGE from client = 819 and DB_CODEPAGE from server = 1208.

如何在我的客户端中设置 CONN_CODEPAGE?我已经在 CentOs 语言环境中设置了 LANG,但仍然出现字符转换错误.

How can I set CONN_CODEPAGE in my client? I already set LANG in CentOs locale, but still got error of character conversion.

______更新:

当前 $LANG = en_US.utf8(也在 locale -a 我有 en_US、en_US.iso88591、en_US.iso885915、en_US.utf8)

Current $LANG = en_US.utf8 (also in locale -a I have en_US, en_US.iso88591, en_US.iso885915, en_US.utf8)

无论 $LANG 设置什么,db2_client_info($con) 的输出还是一样的:

No matter what $LANG is set output from db2_client_info($con) still the same:

 ["APPL_CODEPAGE"]=>
  int(819)
  ["CONN_CODEPAGE"]=>
  int(819)

关于从源代码页面进行字符转换

我正在使用 PHP+Apache.Apache httpd.conf defaultCharset 设置不同的语言环境并没有改变任何东西.

I'm using PHP+Apache. Apache httpd.conf defaultCharset setting different locales didnt't change anything.

在 php 代码中 setlocale 也不要更改任何内容.

In php code setlocale don't changing anything either.

我认为这个设置存储在其他地方,ibm_db2pdo_ibm 驱动程序使用它.=|

I think this setting store somewhere else, and ibm_db2 or pdo_ibm drivers use it. =|

____upd2

我发现的唯一方法是从已安装的 db2 客户端更改 DB2CODEPAGE.但是我没有安装客户端.我正在使用内部库.

The only way I found out - change DB2CODEPAGE from installed db2 client. But I don't have installed client. I'm using inly libraries.

现在我安装了 v11.1.2fp2_linuxx64_client.tar.gz (1.03 GB).但我不能 laucn db2 客户端.认为我需要另一个新问题 =

Now i installed v11.1.2fp2_linuxx64_client.tar.gz (1.03 GB). But i can't laucn db2 client. Think I need another new question =

____upd3

在使用 CentO、apache 语言环境进行所有操作之后 - 我仍然得到 -322 异常.无论我更改了什么 - db2_client_info($con) 仍然显示 819 CodePage.一切都很好,直到 fetch()/fetchAll()

After all manipulations with CentOs, apache locales - I still get -322 exception. No matter what I changed - db2_client_info($con) still shows me 819 CodePage. And all work nice, until fetch()/fetchAll()

CentOs 7.3、带有 PDO_IBM 1.3.4 补丁的 PHP 7.1.8 和 ibm_db2.但是我从 PECL 来源制作这个模块(因为服务器没有互联网连接).

CentOs 7.3, PHP 7.1.8 with PDO_IBM 1.3.4-patched, and ibm_db2. BUT I make this modules from PECL sources (cause server doesn't have internet connection).

而且我的应用服务器上也没有安装任何 DB2 产品(这就是为什么我决定 CodePage 从 CentOs 语言环境获取).从您的测试看来,DB2 数据服务器客户端 是必需的?

And I also don't have any DB2 products installed on mine application server (thats why I decide that CodePage getting from CentOs locale). It seems from your test, that DB2 data server client is required?

推荐答案

如果您使用的是专门为您的 Centos 客户端使用的IBM DB2 Data server driver for ODBC and CLI",您可以尝试确保该帐户的环境运行 PDO 并连接到 DB2 的 DB2CODEPAGE=1208.您可以通过以下方式导出此变量(仅针对此特定客户端类型):

If you are using the "IBM DB2 Data server driver for ODBC and CLI" specifically for your Centos client, you can try ensuring that the environment for the account that runs PDO and connects to DB2 has DB2CODEPAGE=1208. You can export this variable (only for this specific client type) via:

export DB2CODEPAGE=1208

并重新启动解决方案中涉及的任何进程.

and restart whatever process(es) are involved in your solution.

对于您用于连接 DB2 的帐户,他们的客户端 LANG 设置应该是 UTF-8 语言环境(并且需要在您的 Centos 上安装该语言环境).使用命令 locale -a 来显示安装了哪些语言环境,然后选择一个具有 utf-8 的您所在地区的语言环境.例如,如果您的 $LANG 是 en_us,那么如果安装了该语言环境,则将其更改为 en_us.utf-8.在相关帐户的 shell 启动中导出该 $LANG 并重新启动您的应用程序.

Also for the account(s) you are using to connect to DB2, their client LANG setting should be an UTF-8 locale (and that locale needs to be installed on your Centos). Use the command locale -a to show which locales are installed, and choose one which has an utf-8 for your territory. For example if your $LANG was en_us then change it to en_us.utf-8, if that locale is installed. Export that $LANG in the shell startup of the relevant accounts and restart your apps.

如果您使用的是完整的 DB2 客户端(或 unix 上的 DB2 服务器上的本地 DB2 客户端),您还需要将 LANG 变量正确设置为与 DB2 数据库编码兼容的值.否则,您将在运行时获得代码页转换,这可能会产生意想不到的结果,包括不存在合适转换时的异常.

If you are using a full-DB2 client (or the local DB2-client on a DB2-server on unix) you also need to correctly set the LANG variable to a value compatible with the encoding of your DB2 database. Otherwise you will either get codepage conversion at run time, which may have unexpected results, including exceptions if no suitable conversion exists.

有关信息:

在 ubuntu 16.04 LTS 上测试了带有 PDO_IBM 1.3.4 补丁的 PHP 7.0.20 和带有 DB2 V11.1.2.2 的 ibm_db2 - appl_codepage = conn_codepage = db_codepage.

Tested PHP 7.0.20 with PDO_IBM 1.3.4-patched, and ibm_db2 , with DB2 V11.1.2.2 , on ubuntu 16.04 LTS - appl_codepage = conn_codepage = db_codepage.

还在 RHEL 6.9 上测试了带有 PDO_IBM 1.3.4 补丁的 PHP 5.3.3 和带有 DB2 V10.5.0.7 的 ibm_db2:appl_codepage = conn_codepage = db_codepage.

Also tested PHP 5.3.3 with PDO_IBM 1.3.4-patched, and ibm_db2, with DB2 V10.5.0.7, on RHEL 6.9: appl_codepage = conn_codepage = db_codepage.

还在 CENTOS 7.3 上测试了带有 PDO_IBM 1.3.4-patched 和 ibm_db2 的 PHP 5.4.16,带有 DB2 V11.1.2.2 数据服务器客户端:appl_codepage = conn_codepage = db_codepage.

Also tested PHP 5.4.16 with PDO_IBM 1.3.4-patched and ibm_db2, with DB2 V11.1.2.2 data server client , on CENTOS 7.3: appl_codepage = conn_codepage = db_codepage.

注意:在从 github 构建 pdo_ibm 或使用 pecl 安装 ibm_db2 之前,Ubuntu、RHEL、Centos 已为 utf-8 设置了默认语言环境.所有本地和远程数据库都有 utf-8 编码.

Note: Ubuntu , RHEL, Centos had default locale set for utf-8, before building pdo_ibm from github, or installing ibm_db2 with pecl. All local and remote databases had utf-8 encoding.

这篇关于php pdo 连接到 DB2 不同的 CODEPAGE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:php pdo 连接到 DB2 不同的 CODEPAGE

基础教程推荐