title: MSSQL Server's Native ODBC Driver for Linux and PHP 5.4
body: <p>I have Apache 2.2.16 and PHP 5.4.3 on a Linux Debian 6 x64.</p>
  
  <p>To install the MSSQL Server's Native ODBC Driver for Linux, I use the following instructions:
  <a href="http://www.codesynthesis.com/~boris/blog/2011/12/02/microsoft-sql-server-odbc-driver-linux/" rel="noreferrer">http://www.codesynthesis.com/~boris/blog/2011/12/02/microsoft-sql-server-odbc-driver-linux/</a></p>
  
  <p>I configured my odbc.ini file this way:</p>
  
  <pre><code>[mydsn]
  Driver      = SQL Server Native Client 11.0
  Database    = datbase
  Server      = xxx.xxx.xxx.xxx,port
  </code></pre>
  
  <p>and my odbcinst.ini this way:</p>
  
  <pre><code>[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
  </code></pre>
  
  <p>To test, I run the following command:</p>
  
  <pre><code>$ isql -v mydsn dbusername dbpassword
  </code></pre>
  
  <p>And I got success:</p>
  
  <pre><code>+---------------------------------------+
  | Connected!                            |
  |                                       |
  | sql-statement                         |
  | help [tablename]                      |
  | quit                                  |
  |                                       |
  +---------------------------------------+
  SQL&gt;
  </code></pre>
  
  <p>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)</p>
  
  <pre><code>$ sudo ln -s /usr/include/php5 /usr/include/php
  $ phpize
  $ ./configure --with-pdo-odbc=unixODBC &amp;&amp; make &amp;&amp; make test
  $ sudo make install
  </code></pre>
  
  <p>On my phpinfo() I get:</p>
  
  <pre><code>PDO support - enabled
  PDO drivers - odbc
  
  PDO Driver for ODBC (unixODBC) - enabled
  ODBC Connection Pooling        - Enabled, strict matching
  </code></pre>
  
  <p>Now it's time to test everything on a PHP 5.4 script:</p>
  
  <pre><code>&lt;?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-&gt;prepare($query);
      $stmt-&gt;execute();
      while ($row = $stmt-&gt;fetch()) {
          echo "&lt;pre&gt;";
          print_r($row);
          echo "&lt;/pre&gt;";
      }
  ?&gt;
  </code></pre>
  
  <p>But it doesn't work... I got this error message:</p>
  
  <pre><code>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-&gt;__construct('odbc:DSN=mydsn...') 
  #1 {main} thrown in /var/www/testemssql.php on line 17
  </code></pre>
  
  <p>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?</p>
  
  <p>Ps.: When I try to use the odbc_connect() PHP says the function doesn't exist.</p>