dongyijing2353
2014-02-05 01:17
浏览 167
已采纳

无法在Ubuntu上使用PHP和ODBC连接到SQL Anywhere 12

I'm trying to get a PHP 5.3.10 installation on Ubuntu 12.04 to connect to a a remote SQL Anywhere 12 (Sybase?) server using ODBC (unixODBC). However, PHP's execution halts at odbc_connect().

PHP code:

$odbc = odbc_connect('DSN=TP189902;', 'username', 'password');

if ($odbc)
{
    echo 'Connected';
}
else
{
    echo 'Failed: '.odbc_error($odbc);
}

So regardless of whether or not it connects, it should be outputting one of the echos, but it doesn't. If I try using PHP's PDO library instead, I get the same result.

My unixODBC setup looks like the following. And this might be where my mistake is, because I've never setup ODBC on linux before and am not very familiar with it.

odbcinst.ini

[SQL Anywhere 12]
Description = SQL Anywhere 12
Driver = /opt/sqlanywhere12/lib64/libdbodbc12.so
Setup  = /opt/sqlanywhere12/lib64/libdbodbc12.so

odbc.ini

[TP189902]
Description = TP189902
Uid = username
Pwd = password
Driver = SQL Anywhere 12
ServerName = 189902
CommLinks = tcpip(Host=1.2.3.4)
DatabaseName = DB189902

I've also tried a ton of alternatives, such as using the driver's path for the Driver value, using Host=1.2.3.4 instead of CommLinks, etc.

Also the command isql -v TP189902 username password doesn't output anything unless I give it a fake DSN so that it outputs and error.

I've also verified that libdbodbc12.so is the same architecture as isql and that it has all of it's dependencies.

On top of this, I have very similar setup on a Windows 7 machine running WAMP, that connects just fine (with both the ODBC and PDO library). I used the same DSN details on it.

Edit: I've also tried this to skip the DSN, but it gives the same result. It also works on the Windows box.

$odbc = odbc_connect('Driver={SQL Anywhere 12};Server=189902;CommLinks=tcpip(Host=1.2.3.4);', 'username', 'password');

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在Ubuntu 12.04上安装PHP 5.3.10以连接到远程SQL Anywhere 12( Sybase?)服务器使用ODBC(unixODBC)。 但是,PHP的执行在odbc_connect()停止。

PHP代码:

  $ odbc = odbc_connect(  'DSN = TP189902;','用户名','密码'); 
 
if($ odbc)
 {
 echo'Connected'; 
} 
else 
 {
 echo'Failed:'。  odbc_error($ odbc); 
} 
   
 
 

因此无论它是否连接,都应该输出其中一个回声,但事实并非如此。 如果我尝试使用PHP的PDO库,我会得到相同的结果。

我的unixODBC设置如下所示。 这可能是我的错误所在,因为我以前从未在Linux上设置ODBC并且对它不熟悉。

odbcinst.ini

  [SQL Anywhere 12] 
Description = SQL Anywhere 12 
Driver = /opt/sqlanywhere12/lib64/libdbodbc12.so
Setup = /opt/sqlanywhere12/lib64/libdbodbc12.so \  n   
 
 

odbc.ini

  [TP189902] 
Description = TP189902 
Uid =  username 
Pwd = password 
Driver = SQL Anywhere 12 
ServerName = 189902 
CommLinks = tcpip(Host = 1.2.3.4)
DatabaseName = DB189902 
   
 
 

我是 还尝试了大量的替代方案,例如使用Driver的路径获取Driver值,使用Host = 1.2.3.4而不是CommLinks等。

此外,命令 isql -v TP189902用户名密码不会输出任何内容,除非我给它一个假DSN以便它输出和错误。

我还验证了libdbodbc12.so是相同的架构 作为isql,它具有所有的依赖 es。

除此之外,我在运行WAMP的Windows 7机器上有非常类似的设置,连接得很好(同时使用ODBC和PDO库)。 我在其上使用了相同的DSN详细信息。

编辑:我也试过这个跳过DSN,但它给出了相同的结果。 它也适用于Windows框。

  $ odbc = odbc_connect('Driver = {SQL Anywhere 12}; Server = 189902; CommLinks = tcpip(Host = 1.2.3.4)  ;','用户名','密码'); 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanmei1536 2014-02-06 13:37
    已采纳

    Ultimately the issue was getting the LD_LIBRARY_PATH set to /opt/sqlanywhere12/lib64 for Apache.

    Setting it in /etc/environment got isql -v TP189902 and php connect.php working when called from any shell user, but not Apache.

    To get Apache to see it, I had to edit /etc/init.d/apache2 and change
    ENV="env -i LANG=C PATH=/usr/local/bin:/usr/bin:/bin"
    to
    ENV="env -i LANG=C PATH=/usr/local/bin:/usr/bin:/bin LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/sqlanywhere12/lib64".
    And then restart the Apache service. Various other methods I found online to do this did not work.

    One caveat is that with the path set, unixODBC still won't read my system DSN file for some reason. So to get Apache to access the DSN, I had to make a user DSN file (.odbc.ini) in /var/www, as that's the Apache user's (www-data) home folder.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doulao2916 2014-02-05 09:33

    I don't use PHP these days but here are some things I've spotted:

    1. I would totally ignore php until you get isql working.
    2. I'm assuming it is a typo that you say your system ini file is "odbcinstr.ini" - it should be "odbcinst.ini".
    3. How do you know you are looking at the right odbc ini files - run odbcinst -j to check the locations unixODBC is using.
    4. I know where that "[ODBC Data Sources]" section comes from (iodbc) but it not at all necessary for unixODBC - just delete the first 2 lines of your odbc.ini file.
    5. your isql line is probably missing a username and password - it should be "isql -v TP189902 username password". I cannot for the life of me see why it would output nothing at all.
    评论
    解决 无用
    打赏 举报