用于查询unixODBC的PHP脚本在浏览器中失败,但可在Linux命令提示符下使用 [英] PHP script to query unixODBC fails in browser but works from Linux command prompt

查看:192
本文介绍了用于查询unixODBC的PHP脚本在浏览器中失败,但可在Linux命令提示符下使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在CentOS7服务器上正确设置了unixODBC以与我的Transoft ODBC驱动程序一起使用,并且我可以成功地连接并查询要在根目录中使用以下命令使用的数据库:

I have unixODBC set up correctly on my CentOS7 server to work with my Transoft ODBC driver, and I can successfully connect and query the databases I am trying to hit using it in root with the command:

isql -v 'DSN' 'user' 'pwd'

下一步,创建了一个PHP脚本来运行查询.

I took the next step and created a PHP script to run a query.

$connect = odbc_connect("integra.udd","user","pwd");
$query = "SELECT Company1,Product,Vendor,Description1 FROM ICMAST WHERE Company1=1 LIMIT 5";
$result = odbc_exec($connect, $query);
while(odbc_fetch_row($result)){
  $product = odbc_result($result, 2);
  $desc = odbc_result($result, 4);
  print "\n$product $desc<br />";
}

如果我使用PHP odbctest.php在root用户中执行此操作,则它可以工作,并且我应该得到结果.此外,我可以使用su -s /bin/bash apache降到apache用户并运行它,它可以工作. (我通过在PHP脚本中运行命令echo exec ('whoami');来验证Apache服务器使用apache作为用户)

If I execute this in root using PHP odbctest.php, it works and I get the results I should. Furthermore, I can drop to the apache user with su -s /bin/bash apache and run it, and it works. (I verified that the Apache server uses apache as the user by running the command echo exec ('whoami'); in a PHP script)

但是,如果我使用浏览器并运行它,则会出现以下错误:

However, if I go to a browser and run it, I get the following error:

Warning: odbc_connect(): SQL error: [unixODBC][Driver Manager]Can't open lib '/usr/usql/sqlaccess/libtsodbc.so' : file not found, SQL state 01000 in SQLConnect in /var/www/html/2m/odbctest.php on line 10

与Apache一起运行时找不到共享的目标文件,但是从任何用户那里运行时,都可以找到共享的目标文件.

The shared object file is not found when running with Apache, but when running from any user, it is.

首先,我假设一个权限问题,所以我将libtsodbc.so文件的所有权更改为apache:apache.没变化.

First, I assumed a permissions issue, so I changed ownership of the libtsodbc.so file to apache:apache. No change.

然后我检查了文件权限,将其设置为555,因此应该可由用户执行.

Then I checked permissions on the file, it's set to 555, so should be executable by users.

然后,我尝试了在浏览器中运行的PHP脚本,该脚本检查文件是否在该路径中.这是我运行的脚本:

I then tried a PHP script run in a browser that checks if the file exists in that path. Here is the script I ran:

<?php
$filename = '/usr/usql/sqlaccess/libtsodbc.so';
if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}
?>

返回该文件存在,因此即使Apache正在执行脚本,PHP也可以看到它.

It returned that the file exists, so PHP can see it even when Apache is executing the script.

我放弃了它是SELinux或防火墙的想法,因为它甚至还没有达到尝试连接的地步,当它试图找到共享对象库文件时失败了.因此,问题必须在服务器上是内部的.

I discarded the idea that it is a SELinux or Firewalled thing as it doesn't even get to the point where it is trying to connect, it fails when it tries to find the shared object library file. So the issue has to be internal on the server.

最后,我认为这可能与共享库路径有关,即使它正在查看错误中文件的实际路径并且没有找到它.因此,我尝试将以下行添加到我的PHP脚本中以设置LD_LIBRARY_PATH:

Finally, I thought maybe this had to do with the shared library path, even though it is looking at the actual path to the file in the error and not finding it. So I tried adding the following line to my PHP script to set the LD_LIBRARY_PATH:

putenv("LD_LIBRARY_PATH=/usr/usql/sqlaccess/");

没有更改,仍然会显示找不到文件的错误.

No change, still gives the file not found error.

我确定这很简单,因为我是一名Linux初学者,但是我对下一步的尝试感到困惑.有任何想法吗?运行PHP脚本的用户和运行PHP脚本的Apache之间还有什么不同?

I'm sure it is something simple as I am a total Linux beginner, but I am at a loss as to what to try next. Any ideas? What else is different between a user running a PHP script and Apache running a PHP script?

编辑以添加 libtsodbc.sophphttpd文件命令输出:

Edit to add libtsodbc.so, php, and httpd file command output:

[root@intranetserver /]# file /usr/usql/sqlaccess/libtsodbc.so
/usr/usql/sqlaccess/libtsodbc.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, not stripped
[root@intranetserver /]# file /usr/bin/php
/usr/bin/php: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=70ddc88b357d818240da4d4b3db50790c7913822, stripped
[root@intranetserver /]# file /usr/sbin/httpd
/usr/sbin/httpd: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0890c878aa1d1a620d5c65d25a13d11cc2fdf96a, stripped

推荐答案

对我来说似乎有点不匹配.

Looks likely to a bitness mismatch to me.

检查这些命令的输出.它们都应该匹配-

Check output of these commands. They should all match --

file /usr/usql/sqlaccess/libtsodbc.so

file `which php`      # the runtime PHP

file `which httpd`    # the typical name of the apache executable

find / -name mod_php -exec file {} \;    
     # mod_php is the PHP module for Apache
     # if you know where mod_php is on your box, you can just do
     #     file /path/to/mod_php

这篇关于用于查询unixODBC的PHP脚本在浏览器中失败,但可在Linux命令提示符下使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆