title: PHP 5.3 not recognizing Native Client to connect to MS SQL
body: <p>We have a server running on </p>
  
  <p>Windows Server 2003 32 Bit IIS6.0 (We have some Classic ASP apps that uses 32bit components so we can't upgrade to 2008 64bit)</p>
  
  <p>Our DB Server is on a separate computer running
  Windows Server 2008 64 Bit
  MS SQL 2008 R2 64 Bit</p>
  
  <p>I have installed the following version of PHP
  PHP 5.3.10
  Build Date  Feb 2 2012 20:26:31
  Compiler    MSVC9 (Visual C++ 2008)</p>
  
  <p>PHP works fine on static pages, but the issue is when trying to connect to MSSQL. We need to connect to MSSQL due to a lot of legacy code in ASP and an existing MSSQL DB.</p>
  
  <p>I have SQL Native Client 2008 installed, I've installed it more than once.
  I checked and the DLLs are in system32 folder.</p>
  
  <p>This is the extensions I'm loading</p>
  
  <pre><code>[PHP_SQLSRV_53_NTS_VC9]
  extension=php_sqlsrv_53_nts_vc9.dll
  [PHP_PDO_SQLSRV_53_NTS_VC9]
  extension=php_pdo_sqlsrv_53_nts_vc9.dll
  </code></pre>
  
  <p>When I run a phpinfo I can't see the sqlsrv driver in the list</p>
  
  <p>I checked the PHP error log and have the following error</p>
  
  <pre><code>[22-Mar-2012 14:04:27 UTC] PHP Warning:  PHP Startup: sqlsrv: Unable to initialize module
  Module compiled with build ID=API20090626,NTS,VC9
  PHP    compiled with build ID=API20090626,TS,VC9
  These options need to match
   in Unknown on line 0
  [22-Mar-2012 14:04:27 UTC] PHP Warning:  PHP Startup: pdo_sqlsrv: Unable to initialize     module
  Module compiled with build ID=API20090626,NTS,VC9
  PHP    compiled with build ID=API20090626,TS,VC9
  These options need to match
   in Unknown on line 0
  </code></pre>
  
  <p>If I change the extension to use Threaded Safe and recycle the application pool, still get this error.</p>
  
  <p>I tried reinstalling Native Client and PHP a few times now but with no luck.</p>
  
  <p>I was getting a different error before that sqlsrv couldn't recognize native client but now php doesn't recognize sqlsrv.</p>
  
  <p><em><strong>UPDATE</em></strong></p>
  
  <p>I managed to make it with the correct sqlsrv driver and no errors on the php errorlog</p>
  
  <p>But when using this script to connect or give me error to sql</p>
  
  <pre><code>&lt;?php
  $serverName = 'DBSERVER';
  $connParams = array('UID'=&gt;'UID', 'PWD'=&gt;'PASSWORD', 'Database'=&gt;'DATABASENAME','ReturnDatesAsStrings'=&gt; true);
  $conn = sqlsrv_connect($serverName, $connParams);
  if(!$conn){
      $errors = sqlsrv_errors();
      die(var_dump($errors));
  }
  sqlsrv_connect($conn);
  die('connected');
  ?&gt;
  </code></pre>
  
  <p>And I'm getting this error</p>
  
  <pre><code>&gt; array(2) { [0]=&gt; array(6) { [0]=&gt; string(5) "IMSSP" ["SQLSTATE"]=&gt;
  &gt; string(5) "IMSSP" [1]=&gt; int(-49) ["code"]=&gt; int(-49) [2]=&gt; string(390)
  &gt; "This extension requires either the Microsoft SQL Server 2008 Native
  &gt; Client (SP1 or later) or the Microsoft SQL Server 2008 R2 Native
  &gt; Client ODBC Driver to communicate with SQL Server. Neither of those
  &gt; ODBC Drivers are currently installed. Access the following URL to
  &gt; download the Microsoft SQL Server 2008 R2 Native Client ODBC driver
  &gt; for x86: http://go.microsoft.com/fwlink/?LinkId=163712" ["message"]=&gt;
  &gt; string(390) "This extension requires either the Microsoft SQL Server
  &gt; 2008 Native Client (SP1 or later) or the Microsoft SQL Server 2008 R2
  &gt; Native Client ODBC Driver to communicate with SQL Server. Neither of
  &gt; those ODBC Drivers are currently installed. Access the following URL
  &gt; to download the Microsoft SQL Server 2008 R2 Native Client ODBC driver
  &gt; for x86: http://go.microsoft.com/fwlink/?LinkId=163712" } [1]=&gt;
  &gt; array(6) { [0]=&gt; string(5) "IM002" ["SQLSTATE"]=&gt; string(5) "IM002"
  &gt; [1]=&gt; int(0) ["code"]=&gt; int(0) [2]=&gt; string(91) "[Microsoft][ODBC
  &gt; Driver Manager] Data source name not found and no default driver
  &gt; specified" ["message"]=&gt; string(91) "[Microsoft][ODBC Driver Manager]
  &gt; Data source name not found and no default driver specified" } }
  </code></pre>
  
  <p><em><strong>UPDATE 2</em></strong></p>
  
  <p>I installed SQLCMD tools and it connects correctly to the server.</p>
  
  <p>The DLL versions are</p>
  
  <p>SQLNCLI10.DLL 2009.100.1600.1</p>
  
  <p>SQLSRV32.DLL 2000.85.1117.0
  (I Updated this one with the DLL from another server but still didn't help)
  It is now Version 6.1.7600.16385</p>
  
  <p>As stated above, Native Client 2008 R2 is installed (the dlls are in windows\system32) and the DB is up and running. If I run the same script from another server it works.</p>
  
  <p>I tried with different permissions on the application pool, the native client dlls and php extension folder with no luck.</p>
  
  <p>Any ideas would be great.</p>
  
  <p><em><strong>UPDATE 3</em></strong></p>
  
  <p>As it turned out it was a permission issue!</p>
  
  <p>I downloaded Process Monitor and followed the instructions on this post</p>
  
  <p><a href="http://www.iislogs.com/articles/processmonitorw3wp/" rel="nofollow">http://www.iislogs.com/articles/processmonitorw3wp/</a></p>
  
  <p>Then I saw the process w3wp.exe was getting access denied on this registry key</p>
  
  <p>HKLM\Software\ODBC\ODBCINST.INI\SQL Native Client 10.0</p>
  
  <p>I opened RegEdit and went to that key.</p>
  
  <p>I did right click - > Permissions and added Network Service to the list and gave it Read permissions.</p>
  
  <p>Recycled the app pool and it is now working!</p>
  
  <p>Cheers,
  Fede</p>