简体   繁体   中英

PHP unable to connect to SQL Server 2016

I've searched all available threads on this question here and none of the answers are working for me.

I've installed (Windows 10 64-bit) Apache2.4 (64-bit), PHP 7.4.2 (64-bit) and SQL Server 2016 (64-bit). I've updated php.ini to include

extension=php_sqlsrv.dll

which is from the package php_sqlsrv-5.7.1preview-7.4-ts-vc15-x64 and when I run php -m to list all the modules sqlsrv is listed.

However, when I try to connect to the SQL server from a PHP file, it fails with an error message .

<?php  
 $serverName = "MYPC\\SQLINSTANCE"; 
 $uid = "sa";   
 $pwd = "thePassword";  
 $databaseName = "TesterDB"; 

 $connectionInfo = array( "UID"=>$uid,                            
                     "PWD"=>$pwd,                            
                     "Database"=>$databaseName); 

 /* Connect using SQL Server Authentication. */  
 $conn = sqlsrv_connect( $serverName, $connectionInfo);  
 if( $conn )
 {
     echo "Connected";
 }
 else
 {
  echo "Error";
  die( print_r( sqlsrv_errors(), true));
 }
?>

error message:

Login failed for user 'sa'. ) [1] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Cannot open database "TesterDB" requested by the login. The login failed.

The login and password are correct, as are the PC and instance names. What else could cause the connection failure? Some formatting issue? Some incompatibility?

Check the list:

  1. Enable "Login by SQL Account" and "Windows Authentication" on MSSQL(properties/security)
  2. Enable "SA Account Login" in the properties menu
  3. Enable sql connection port on the firewall,(1433 default port) or disable firewall for only test
  4. Enable remote connection to MSSQL if the connecting from other network
  5. Troubleshoot the connect progress by profiler tools

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