PHP SQL Server Couldn't connect

2019-05-26 08:28发布

I want to ask you about how to connect to MSSQL Server 2005 using PHP?

I got error like this :

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

and here is my PHP code :

<?php
    $serverName = "192.168.183.249\MSSQLSERVER, 1542"; //serverName\instanceName, portNumber default is 1433)
    $connectionInfo = array( "Database"=>"SEI_AproCS", "UID"=>"sa", "PWD"=>"");
    $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));
    }
?>

Please help to advice.

THanks

4条回答
姐就是有狂的资本
2楼-- · 2019-05-26 08:31

To connect with sql server you need to add dll file for sql server in the directory

wamp\bin\php\php5.3.0\ext
查看更多
乱世女痞
3楼-- · 2019-05-26 08:39

You instal driver. ODBC

  • msodbcsql_x64.msi #System Operation 63
  • msodbcsql_x86.msi #System Operation 86/32

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

good luck

查看更多
女痞
4楼-- · 2019-05-26 08:50

You just need to install driver from this link PHP Driver

You can follow this link for tutorial PHP MSSQL Tutorial

An other option is to connect to mssql using DSN ,
and this easy way you just to enable mssql extension in php

And follow this link to create DSN Create DSN in windows

Also follow this link to connect using php and DSN Connect php using DSN

查看更多
走好不送
5楼-- · 2019-05-26 08:54

Try PDO:

//$pdo = new PDO("sqlsrv:Server=$hostname;Database=$dbname;", $username, $password);  // works with proper driver for PHP.
$pdo = new PDO("odbc:Driver={SQL Server};Server=$hostname;Database=$dbname;", $username, $password);  // works with proper driver for ODBC and PHP ODBC.

I could not get the first line to work due to weird compiler version incompatibilities, but the second one worked fine after installing Microsoft ODBC Driver 11 for SQL Server

PHP Version 5.3.0 has built-in ODBC support, according to php.ini, but that still lists an active extension=php_pdo_odbc.dll here.

查看更多
登录 后发表回答