我已经尝试通过使用PHP连接SQL Server以进行PDO和简单连接。但有些人总是会出现错误。这是我的脚本使用PDO和简单的连接。
使用PDO:
$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));
}
使用简单连接:
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() ) );
}
以下是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.
))
简单连接:
SQLSTATE[08001]: [Microsoft][ODBC Driver 13 for SQL Server]Encryption not supported on the client.
我仍然不明白为什么我无法连接因为我使用最新的SQL Server驱动程序PHP 7.2.5。
发布于 2018-08-24 09:32:21
https://stackoverflow.com/questions/-100002390
复制相似问题