fishmong3r
fishmong3r

Reputation: 1434

Can't connect to SQL Server from PHP

I use XAMPP. I need to establish connection from PHP code to my MS SQL Server. Therefore, I installed the sqlsrv driver from here. According to phpinfo() it's being loaded properly: enter image description here

I get this error message:

Connection could not be established. Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP => -49 [code] => -49 => This extension requires the Microsoft ODBC Driver 11 for SQL Server. Access the following URL to download the ODBC Driver 11 for SQL Server for x86: http://go.microsoft.com/fwlink/?LinkId=163712 [message] => This extension requires the Microsoft ODBC Driver 11 for SQL Server. Access the following URL to download the ODBC Driver 11 for SQL Server for x86: http://go.microsoft.com/fwlink/?LinkId=163712 ) => Array ( [0] => IM002 [SQLSTATE] => IM002 => 0 [code] => 0 => [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 ) )

The links point to x86 client, but I can only install 64-bit version, which I did so from

https://www.microsoft.com/en-us/download/details.aspx?id=29065

Still I get the same error.

Specs:

OS: Windows Server 2012 64-bit

PHP: 5.6.19

Code is simple as that:

$serverName = "servername";
$connectionInfo = array( "Database"=>"DBname", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}

Now what? Shall I register that installed SQL Native Client in any of the PHP/Apache conf files?

Upvotes: 2

Views: 8334

Answers (2)

Masilo
Masilo

Reputation: 53

I had the same problem...

check which version of php you use by using phpinfo() then download the extensions related to your version of php. Then also go to your php.ini file and add those file like so

extension=<extension>.dll

Then one vital step I was always missing and was stressing me is that you need to download the driver found on the link below... Or else your php script wont connect to your SQL Server .... Download and install this on the machine which actually has SQL Server installed... If it is a remote server this has to be installed on a remotre server... If it is on your local machine this has to be installed on your local machine...

https://www.microsoft.com/en-us/download/details.aspx?id=36434

Can't connect to SQL Server from PHP

Then you should be good to go...

Upvotes: 0

ImClarky
ImClarky

Reputation: 1953

According to this blog, Microsoft's SQL Server 2012 Feature Pack does not contain ODBC Driver 11 for SQL Server; which is required for sqlsrv extensions 3.1 and above.

Upvotes: 2

Related Questions