MSSQL Server's Native ODBC Driver for Linux and PHP 5.4
linux, odbc, php, sql-server
Solution
MSSQL Server's Native ODBC Driver for Linux has a bug on it
To connect MS SQL Server correctly, use FreeTDS See more details in : PHP 5.4 on Linux: How to connect with MS SQL Server 2008?
Problem
I have Apache 2.2.16 and PHP 5.4.3 on a Linux Debian 6 x64. To install the MSSQL Server's Native ODBC Driver for Linux, I use the following instructions: http://www.codesynthesis.com/~boris/blog/2011/12/02/microsoft-sql-server-odbc-driver-linux/ I configured my odbc.ini file this way: ``` [mydsn] Driver = SQL Server Native Client 11.0 Database = datbase Server = xxx.xxx.xxx.xxx,port ``` and my odbcinst.ini this way: ``` [SQL Server Native Client 11.0] Description=Microsoft SQL Server ODBC Driver V1.0 for Linux Driver=/opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0 Threading=1 UsageCount=1 ``` To test, I run the following command: ``` $ isql -v mydsn dbusername dbpassword ``` And I got success: ``` +---------------------------------------+ | Connected! | | | | sql-statement | | help [tablename] | | quit | | | +---------------------------------------+ SQL> ``` Then, a use phpize to install unixODBC on PHP 5.4, using this: (The first command, ln -s ..., is used because ./configure can't find the headers of php on the default location) ``` $ sudo ln -s /usr/include/php5 /usr/include/php $ phpize $ ./configure --with-pdo-odbc=unixODBC && make && make test $ sudo make install ``` On my phpinfo() I get: ``` PDO support - enabled PDO drivers - odbc PDO Driver for ODBC (unixODBC) - enabled ODBC Connection Pooling - Enabled, strict matching ``` Now it's time to test everything on a PHP 5.4 script: ``` <?php ini_set('display_errors', 1); error_reporting(E_ALL); $conn = new PDO('odbc:DSN=mydsn;UID='.$usr.';PWD='.$psw); $query = 'select * from my_table'; $stmt = $conn->prepare($query); $stmt->execute(); while ($row = $stmt->fetch()) { echo "<pre>"; print_r($row); echo "</pre>"; } ?> ``` But it doesn't work... I got this error message: ``` Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[01000] SQLDriverConnect: 0 [unixODBC][Driver Manager]Can't open lib '/opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0' : file not found' in /var/www/testemssql.php:17 Stack trace: #0 /var/www/testemssql.php(17): PDO->__construct('odbc:DSN=mydsn...') #1 {main} thrown in /var/www/testemssql.php on line 17 ``` So my question is: what is happen? What configuration I'm missing? How to set up correctly the MSSQL Server's Native ODBC Driver on Linux and PHP 5.4? Ps.: When I try to use the odbc_connect() PHP says the function doesn't exist.