Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to SQL Server 2008 through PHP

I need to connect to a SQL Server 2008 through PHP (WAMP, latest version). I have the sqlsrv drivers installed and set up and they do show up in phpinfo().

I'm using the following lines to connect to my DB, using Windows Authentication:

$serverName = "(local)";
$connectionOptions = array("Database"=>"MyTestDatabase");
$conn = sqlsrv_connect( $serverName, $connectionOptions) or die("Error!");

And I'm getting the following error:

Array 
( 
    [0] => Array 
    ( 
        [0] => IMSSP 
        [SQLSTATE] => IMSSP 
        [1] => -49
        [code] => -49 
        [2] => This extension requires the Microsoft SQL Server 2011 Native Client. Access the following URL to download the Microsoft SQL Server 2011 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 
        [message] => This extension requires the Microsoft SQL Server 2011 Native Client. Access the following URL to download the Microsoft SQL Server 2011 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712
    ) 
    [1] => Array 
    ( 
        [0] => IM002 
        [SQLSTATE] => IM002 
        [1] => 0 
        [code] => 0 
        [2] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified 
        [message] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    )
)

Any help would be great, but please be specific since I really don't know my way around WAMP except for a few basics.

like image 754
Eugen Avatar asked Feb 13 '12 01:02

Eugen


People also ask

Can PHP connect to SQL Server?

Connecting to a MS SQL Server database with PHP is very similar to connecting to a MySQL database. The following example demonstrates how to connect to a MS SQL database from PHP. Note that the function names contain mssql, not mysql.

How do you create PHP connectivity with SQL database?

php $servername = "localhost"; $username = "username"; $password = "password"; $db = "dbname"; // Create connection $conn = mysqli_connect($servername, $username, $password,$db); // Check connection if (!$ conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?>

How enable Sqlsrv in PHP INI?

The SQLSRV extension is enabled by adding appropriate DLL file to your PHP extension directory and the corresponding entry to the php. ini file. The SQLSRV download comes with 8 driver files, four of which are for PDO support. The most recent version of the driver is available for download here: » SQLSRV download.


1 Answers

The error is caused by a permission issue in the registry. There is a key where the path to the native client is stored and the identity you are using in the application pool is getting denied access.

  • Download Process Monitor and follow the instructions on this post: http://www.iislogs.com/articles/processmonitorw3wp/ (This tutorial shows how to do it on IIS, with WAMP you will need to find the .exe process that runs on memory)
  • Find the registry key where the process w3wp.exe is being denied access. That in the case of IIS, not sure what's the name of the process in WAMP but the procedure is the same. In my case:

    HKLM\Software\ODBC\ODBCINST.INI\SQL Native Client 10.0
    
  • Run regedit and find the key
  • Right click and go to Permissions
  • Add Network Service to the list and give it Read permissions.
  • Recycle the app pool and it should work
like image 130
Federico Giust Avatar answered Oct 19 '22 23:10

Federico Giust