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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效