sqlsrv_connect: Data source name not found and no default driver specified

百般思念 提交于 2019-12-11 19:33:17

问题


I am trying to connect to SQL Server 2008 on machine-1 from PHP code on another machine-2. I am using PHP 5.4.7, XAMPP 1.8.1 and I have copied the sql server dlls into PHP/ext folder and modified the php.ini file.

Now, when I try to connect to SQL server I am getting following error.

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 ) )

My PHP code is as follows -

<?php
//phpinfo(); 
$server = "sql server\express,1433"; //serverName\instanceName, portNumber (default is 1433)
$uid="username";
$pwd="password";
$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database"=>"rod_prd_tmart");
$conn = sqlsrv_connect( $server, $connectionInfo) ;

if($conn) 
{
   echo "Connection established.\n";
} else
{
   echo "Connection could not be established in this text.\n";
   die( print_r( sqlsrv_errors(), true));
}

sqlsrv_close( $conn);
?>

Please help me resolve this issue. Thanks.


回答1:


Consider using PDO for more flexibility:

//$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 here that still lists an active extension=php_pdo_odbc.dll



来源:https://stackoverflow.com/questions/18589942/sqlsrv-connect-data-source-name-not-found-and-no-default-driver-specified

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!