Question

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');
Was it helpful?

Solution 2

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.

OTHER TIPS

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top