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

无法在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');
  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制