PHP/Linux 到 AS/400-db2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5241267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:10:42  来源:igfitidea点击:

PHP/Linux to AS/400-db2

linuxodbcdb2ibm-midrange

提问by bob

I am trying to get php on Linux Centos server access as/400 (iSeries) db2 database.

我试图在 Linux Centos 服务器上获取 php 访问 as/400 (iSeries) db2 数据库。

I am using this IBM guide as much as possible, (though we could not get GUI configuration utility working.)

我尽可能多地使用这个 IBM 指南,(虽然我们无法让 GUI 配置实用程序工作。)

http://www-03.ibm.com/systems/i/soft...ide/index.html

http://www-03.ibm.com/systems/i/soft...ide/index.html

I downloaded and successfully installed iSeriesAccess drivers and pre-requisites.

我下载并成功安装了 iSeriesAccess 驱动程序和先决条件。

rpm -i iSeriesAccess-5.4.0-1.6.i386.rpm

I have configured these files to define drivers/DNS:

我已经配置了这些文件来定义驱动程序/DNS:

/etc/odbc.ini and /etc/odbcinst.ini

/etc/odbc.ini 和 /etc/odbcinst.ini

[iSeries Access ODBC Driver]
Description          = iSeries Access for Linux ODBC Driver
Driver               = /opt/ibm/iSeriesAccess/lib/libcwbodbc.so
Setup                = /opt/ibm/iSeriesAccess/lib/libcwbodbcs.so
Driver64             = /opt/ibm/iSeriesAccess/lib64/libcwbodbc.so
Setup64              = /opt/ibm/iSeriesAccess/lib64/libcwbodbcs.so
Threading            = 2
DontDLClose          = 1
UsageCount           = 1 

file /etc/odbc.ini was empty so I added this configuration:

文件 /etc/odbc.ini 是空的,所以我添加了这个配置:

[AS400]
Description     = iSeries Access ODBC Driver
Driver          = iSeries Access ODBC Driver
System          = 172.999.999.999             (from netstat option 1)
UserID          = my_user
Password        = my_pass
Naming          = 0
DefaultLibraries  = QGPL
Database          =
ConnectionType    = 0
CommitMode        = 2
ExtendedDynamic   = 1
DefaultPkgLibrary = QGPL
DefaultPackage    = A/DEFAULT(IBM),2,0,1,0,512
AllowDataCompression  = 1
LibraryView           = 0
AllowUnsupportedChar  = 0
ForceTranslation      = 0
Trace           = 1
DSN             = AS400 

I assume these are working because I can run

我认为这些是有效的,因为我可以运行

isql -v AS400

and I connect successfully to db2 database can perform queries from Linux box.

并且我成功连接到 db2 数据库可以从 Linux box 执行查询。

However I have been unable to get make and ODBC connection in PHP on the Linux box. Is there another way to test DSN from php? or get more detailed error information?

但是我一直无法在 Linux 机器上的 PHP 中获得 make 和 ODBC 连接。有没有另一种方法可以从 php 测试 DSN?或者获取更详细的错误信息?

$server="172.999.999.999";    
    // tried with both system name and "AS400", the dsn name
$user="my_user"; 
$pass="my_pass";

$conn=odbc_connect($server,$user,$pass);
if ($conn == false) {
  echo "Not able to connect to database...
"; }

result:

结果:

**Not able to connect to database...** 

phpinfo() shows that php was compiled with unixODBC and unixODBC is enabled.

phpinfo() 显示 php 是用 unixODBC 编译的,并且启用了 unixODBC。

any help is appreciated!

任何帮助表示赞赏!

回答by Jordan Owens

Try double checking your odbcinst.ini and odbc.ini config files. Do you have the correct database name/default library set in odbc.ini? I had success following these instructions:

尝试仔细检查您的 odbcinst.ini 和 odbc.ini 配置文件。您是否在 odbc.ini 中设置了正确的数据库名称/默认库?我按照以下说明取得了成功:

http://werk.feub.net/2010/11/ingredients-php-db2-and-unixodbc/

http://werk.feub.net/2010/11/ingredients-php-db2-and-unixodbc/

One difference is that I found a version of openmotif that included libXm.so.3.

一个区别是我找到了一个包含 libXm.so.3 的 openmotif 版本。

http://rpm.pbone.net/index.php3/stat/3/limit/2/srodzaj/1/dl/40/search/libXm.so.3/field[]/1/field[]/2

http://rpm.pbone.net/index.php3/stat/3/limit/2/srodzaj/1/dl/40/search/libXm.so.3/field[]/1/field[]/2

Restart apache after installing php-odbc.

安装php-odbc后重启apache。

/etc/odbc.ini

/etc/odbc.ini

[ISERIES]
Description = iSeries Access ODBC Driver DSN for iSeries
Driver = iSeries Access ODBC Driver
System = 192.168.1.1
UserID = MYUSER
Password = MYPASSWORD
Naming = 0
DefaultLibraries = QGPL
Database =
ConnectionType = 0
CommitMode = 2
ExtendedDynamic = 0
DefaultPkgLibrary = QGPL
DefaultPackage = A/DEFAULT(IBM),2,0,1,0,512
AllowDataCompression = 1
LibraryView = 0
AllowUnsupportedChar = 0
ForceTranslation = 0
Trace = 0

Sample PHP:

示例 PHP:

<?php
if (!$db = odbc_connect ( "ISERIES", "MYUSER", "MYPASSWORD") )
    echo 'error!';

$result = odbc_exec($db, "SELECT * FROM MYUSER.TABLENAME");
while (odbc_fetch_row($result)) {
    echo odbc_result($result, "ID")."\n";
}

odbc_close($db)
?>

回答by bob

indeed job log had this entry:

确实作业日志有这个条目:

Mar 9 14:04:52 mtl setroubleshoot: SELinux is preventing the http daemon from connecting to network port 8471

3 月 9 日 14:04:52 mtl setroubleshoot:SELinux 正在阻止 http 守护程序连接到网络端口 8471

I turned off SELinux and problem solved.

我关闭了 SELinux 并解决了问题。

Thanks for the lead!

谢谢带头!

回答by Mr.Dito

You need to use ODBC Source Name in odbc_connect()function, not the server IP. In your example AS400 is the name between brackets at the top of odbc.inifile.

您需要在odbc_connect()函数中使用 ODBC 源名称,而不是服务器 IP。在您的示例中,AS400 是odbc.ini文件顶部括号之间的名称。

回答by Michael

I had a similar issue. I finally found this post: https://adminramblings.wordpress.com/2015/02/27/odbc-from-linux-to-iseries-as400-for-php/that helped me install the correct drivers, configure them and connect to the database. I'll include the information here just in case:

我有一个类似的问题。我终于找到了这篇文章:https: //adminramblings.wordpress.com/2015/02/27/odbc-from-linux-to-iseries-as400-for-php/帮助我安装正确的驱动程序,配置它们并连接到数据库。我将在此处包含信息以防万一:



ODBC from Linux to iseries (AS400) for php

ODBC 从 Linux 到 iseries (AS400) for php

Posted: February 27, 2015 | Author: Stephen Dart | Filed under: Uncategorized |Leave a comment

发表时间:2015 年 2 月 27 日 | 作者:斯蒂芬·达特 | 提交如下:未分类 |发表评论

Using a linux (Ubuntu) box to connect to a IBM iSeries (AS400) to use php queries and reporting

使用 linux (Ubuntu) box 连接到 IBM iSeries (AS400) 以使用 php 查询和报告

IBM ODBC driver (login required) http://www-03.ibm.com/systems/power/software/i/access/linux/guide.html

IBM ODBC 驱动程序(需要登录)http://www-03.ibm.com/systems/power/software/i/access/linux/guide.html

Install apache with php and odbc modules.

使用 php 和 odbc 模块安装 apache。

sudo apt-get install libapache2-mod-php5 apache2 php5-odbc

Install unixodbc

安装 unixodbc

sudo apt-get install unixodbc

Copy the downloaded iseries odbc driver to the server and install

将下载的iseries odbc驱动复制到服务器并安装

sudo dpkg -i ibm-iaccess-1.1.0.2-1.0.amd64.deb

symbolic link the libraries to the /usr/lib folder for use for the system

将库符号链接到 /usr/lib 文件夹以供系统使用

sudo ln -s /opt/ibm/iSeriesAccess/lib64/libcwb* /usr/lib

We can now create the odbc setup using the driver. Find where the SYSTEM odbcinst.ini and odbc.ini files are:

我们现在可以使用驱动程序创建 odbc 设置。找到 SYSTEM odbcinst.ini 和 odbc.ini 文件的位置:

odbcinst -j

unixODBC 2.2.14
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources

Edit the files, the odbcinst.ini file should have the IBM iseries driver setup already within it as part of the driver install, but the default file is missing the header [ODBC Drivers] which I found caused problems, so can be added to the top.

编辑文件,odbcinst.ini 文件应该已经在其中安装了 IBM iseries 驱动程序作为驱动程序安装的一部分,但默认文件缺少我发现导致问题的标题 [ODBC 驱动程序],因此可以添加到最佳。

/etc/odbcinst.ini

[ODBC Drivers]
IBM i Access ODBC Driver
Description = IBM i Access for Linux ODBC Driver
Driver = /opt/ibm/iSeriesAccess/lib/libcwbodbc.so
Setup = /opt/ibm/iSeriesAccess/lib/libcwbodbcs.so
Driver64 = /opt/ibm/iSeriesAccess/lib64/libcwbodbc.so
Setup64 = /opt/ibm/iSeriesAccess/lib64/libcwbodbcs.so
Threading = 0
DontDLClose = 1
UsageCount = 2

[IBM i Access ODBC Driver 64-bit]
Description = IBM i Access for Linux 64-bit ODBC Driver
Driver = /opt/ibm/iSeriesAccess/lib64/libcwbodbc.so
Setup = /opt/ibm/iSeriesAccess/lib64/libcwbodbcs.so
Threading = 0
DontDLClose = 1
UsageCount = 2

Now create the odbc.ini for your specific system, its important to create a [ODBC Data Sources] and the [DSN]

现在为您的特定系统创建 odbc.ini,创建 [ODBC 数据源] 和 [DSN] 很重要

Example /etc/odbc.ini

示例 /etc/odbc.ini

[ODBC Data Sources]
DEV = DEV

[DEV]
Description = iSeries Access ODBC Driver
Driver = IBM i Access ODBC Driver
System = FQDN or IP
UserID = USER
Password = PASSWORD
Naming = 1
DefaultLibraries = QGPL
Database =
ConnectionType = 2
CommitMode = 2
ExtendedDynamic = 1
DefaultPkgLibrary =
DefaultPackage = A/DEFAULT(IBM),2,0,1,0,512
AllowDataCompression = 1
LibraryView = 0
AllowUnsupportedChar = 1
ForceTranslation = 1
Trace = 0

The bracketed name in the odbc is the DSN, in this case [DEV] Driver in odbc.in needs to match the name specified in odbcinst.ini

odbc 中括号内的名称是 DSN,在这种情况下,odbc.in 中的 [DEV] Driver 需要匹配 odbcinst.ini 中指定的名称

System is the network name in fqdn or IP.

System 是 fqdn 或 IP 中的网络名称。

UserID and Password are as required to login to the iseries.

UserID 和 Password 是登录到 iseries 所必需的。

DefaultLibraries, Database, and DefaultPkgLibrary can be specified as required or left blank and specified higher in the php, I only use the DefaultLibraries.

DefaultLibraries、Database 和 DefaultPkgLibrary 可以根据需要指定或留空并在 php 中指定更高,我只使用 DefaultLibraries。

Test the connection using the commandline isql and the DSN.

使用命令行 isql 和 DSN 测试连接。

isql -v DEV
[unixODBC][Driver Manager]Can't open lib '/opt/ibm/iSeriesAccess/lib64/libcwbodbc.so': file not found

Oooh ERROR, thats not good… but I have found a fix !

哦,错误,那不好……但我找到了解决方法!

There is a discrepancy between the iseries driver and the unixodbc libraries for the libodbcinst which can result in the above meaningless error when using the driver.

iseries 驱动程序和 libodbcinst 的 unixodbc 库之间存在差异,这可能导致在使用驱动程序时出现上述无意义的错误。

The file is within the location but there is a problem with the actual library and the error message is not very clear.

文件在该位置,但实际库有问题,错误信息不是很清楚。

To see the true error we need to use ldd to see the linked dependancies of libcwdodbc.so

要查看真正的错误,我们需要使用 ldd 来查看 libcwdodbc.so 的链接依赖项

ldd /opt/ibm/iSeriesAccess/lib64/libcwbodbc.so
linux-vdso.so.1 => (0x00007fff86dfe000)
libodbcinst.so.2 => not found
libcwbcore.so => /usr/lib/x86_64-linux-gnu/libcwbcore.so (0x00007f7f68545000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f7f68240000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7f67f3a000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f7f67d24000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7f6795d000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f7f6773f000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7f6753b000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f7f67332000)
/lib64/ld-linux-x86-64.so.2 (0x00007f7f68b98000)

Until there is a fix packaged version of unixodbc within the ubuntu packages (current version 2.2.14p2-5ubuntu5) then we can symbolic link so.1 to so.2

在 ubuntu 包(当前版本 2.2.14p2-5ubuntu5)中有 unixodbc 的修复打包版本之前,我们可以符号链接 so.1 到 so.2

sudo ln -s /usr/lib/x86_64-linux-gnu/libodbcinst.so.1 /usr/lib/x86_64-linux-gnu/libodbcinst.so.2

IBM article regarding this: http://www.ibm.com/developerworks/ibmi/library/i-ibmi-access-client-solutions-linux/

IBM 关于此的文章:http: //www.ibm.com/developerworks/ibmi/library/i-ibmi-access-client-solutions-linux/

Running ldd now sees the required libraries.

运行 ldd 现在可以看到所需的库。

ldd /opt/ibm/iSeriesAccess/lib64/libcwbodbc.so
linux-vdso.so.1 => (0x00007fff315fe000)
libodbcinst.so.2 => /usr/lib/x86_64-linux-gnu/libodbcinst.so.2 (0x00007fcef32ed000)
libcwbcore.so => /usr/lib/x86_64-linux-gnu/libcwbcore.so (0x00007fcef2f7a000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fcef2c75000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fcef296f000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fcef2759000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fcef2392000)
libltdl.so.7 => /usr/lib/x86_64-linux-gnu/libltdl.so.7 (0x00007fcef2188000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fcef1f6a000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fcef1d65000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fcef1b5d000)
/lib64/ld-linux-x86-64.so.2 (0x00007fcef37df000)

Now we can rerun isql and test.

现在我们可以重新运行 isql 并进行测试。

isql -v DEV
+---------------------------------------+
| Connected!                           |
|                                      |
| sql-statement                        |
| help [tablename]                     |
| quit                                 |
|                                      |
+---------------------------------------+

SQL

SQL

Woot, connected!

哇,连接!

Ok now you have a working ODBC connection to your iseries, so you can use it via the DSN and your applications.

好的,现在您有一个到您的 iseries 的工作 ODBC 连接,因此您可以通过 DSN 和您的应用程序使用它。

The next stage was to use php to link to this odbc.

下一阶段是使用 php 链接到这个 odbc。

Php page connecting to DSN [DEV] using a select statement and putting the results into a table:

Php 页面使用 select 语句连接到 DSN [DEV] 并将结果放入表中:

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>

<?php
try{
$as400conn = new PDO(‘odbc:DEV'); 
// Note: The name is the same as what's in our square brackets in ODBC.ini
$as400conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$library = “as400 library”;
$file = “as400 file”;
$i = 0;
$fields[$i++] = “file field 1″;
$fields[$i++] = “file field 2″;
$fields[$i++] = “file field 3″;
$AryLength = count($fields);
// Create SQL Have to include first field for comma separate outside of the while loop. field,field
$sql = “SELECT ” . $fields[0] ;
for($x = 1; $x < $AryLength; $x++) {
$sql = $sql . “,” . $fields[$x] ;
}
$sql = $sql.” FROM ” . $library . “/” . $file ;
echo $sql;
echo “<br>”;
$result = $as400conn->query($sql);

// Print Table Header //
echo “<table><tr>”;
for($x = 0; $x < $AryLength; $x++) {
echo “<th> $fields[$x] </th>”;
}
echo “</tr>”;

// Output Data of each row
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo “<tr> “;
for($x = 0; $x < $AryLength; $x++) {
echo “<td>” . $row[$fields[$x]] . “</td>”;
}
echo “</tr>”;
}
echo “</table>”;
$as400conn = null;

//end of try
}
catch (PDOException $e) {
echo $e->getMessage();
}
?>
</body>
</html>

I've installed on both 32 bit and 64 bit versions of Ubuntu using this tutorial. You just have to tweak a few of the folder names if you want to install the 32 bit version. Hope this helps someone else.

我已经使用本教程在 32 位和 64 位版本的 Ubuntu 上进行了安装。如果您想安装 32 位版本,您只需要调整一些文件夹名称。希望这对其他人有帮助。