$serverName = "Server"; //serverName\instanceName $connectionInfo = array( "Database"=>"Database", "UID"=>"Username", "PWD"=>"Password"); $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn ) { echo "Connection established.<br />"; $resultfile = fopen("E:\\result.txt", "w") or die("Unable to open file!"); fwrite($resultfile, "wew"); fclose($resultfile); }else{ echo "Connection could not be established.<br />"; $resultfile = fopen("E:\\result.txt", "w") or die("Unable to open file!"); fwrite($resultfile , print_r( sqlsrv_errors(), true)); fclose($resultfile ); die( print_r( sqlsrv_errors(), true)); }
Using simple connection :
try { $conn = new PDO( "sqlsrv:server = $serverName; Database = Database", "password", "username"); $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); echo "Connection established.<br />"; $resultfile = fopen("E:\\result.txt", "w") or die("Unable to open file!"); fwrite($resultfile, "wew"); fclose($resultfile); } catch(Exception $e) { $resultfile = fopen("E:\\result.txt", "w") or die("Unable to open file!"); fwrite($resultfile, "wew"); fclose($resultfile); die( print_r( $e->getMessage() ) ); }
And here are the error result by PDO:
Array( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 21 [code] => 21 [2] => [Microsoft][ODBC Driver 13 for SQL Server]Encryption not supported on the client. [message] => [Microsoft][ODBC Driver 13 for SQL Server]Encryption not supported on the client. ) [1] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 87 [code] => 87 [2] => [Microsoft][ODBC Driver 13 for SQL Server]SQL Server Network Interfaces: The parameter is incorrect. [message] => [Microsoft][ODBC Driver 13 for SQL Server]SQL Server Network Interfaces: The parameter is incorrect. ) [2] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 21 [code] => 21 [2] => [Microsoft][ODBC Driver 13 for SQL Server]Client unable to establish connection [message] => [Microsoft][ODBC Driver 13 for SQL Server]Client unable to establish connection ) [3] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 87 [code] => 87 [2] => [Microsoft][ODBC Driver 13 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. [message] => [Microsoft][ODBC Driver 13 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. ))
Simple connection:
SQLSTATE[08001]: [Microsoft][ODBC Driver 13 for SQL Server]Encryption not supported on the client.
I still don't understand why I cannot connect because I am using the latest SQL Server Driver for PHP 7.2.5.