简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM