无法通过 XAMPP 连接到 SQL 数据库 - 驱动程序的 SQLSetConnectAttr 失败
Cannot connect to a SQL database via XAMPP - Driver's SQLSetConnectAttr failed
我正在尝试从我的 PHP 代码访问 SQL 数据库。我目前正在使用 XAMPP 8.0.6 和 PHP 8.0.6。这在安装了 PHP 7.4.19 和相关扩展的机器上也不起作用。
已安装的驱动程序/扩展:
- sql-srv: php_sqlsrv_80_ts_x64.dll
- sql-pdo: php_pdo_sqlsrv_80_ts_x64.dll
- OBDC 驱动程序:10.0.19041.1
sqlsrv 和 sql-pdo 正确放置在 php.ini 中。使用 OBDC gui 连接到数据库,我连接成功。
我试图从数据库中获取数据的代码是:
function mssqlConnect(){
$ServerName = "xxx";
$ConnectionTimeout = 30;
$connectionOptions = ["Database"=>"database","UID"=>"user","PWD"=>"password", "LoginTimeout" => $ConnectionTimeout];
$sqlconn = sqlsrv_connect($ServerName,$connectionOptions);
if( $sqlconn ) {
echo "Connection established";
} else {
echo "Connection could not be established";
die( print_r( sqlsrv_errors(), true));
}
}
尝试 运行 时,我收到以下消息:
Connection could not be established.
Array (
[0] => Array (
[0] => IM006
[SQLSTATE] => IM006
[1] => 0
[code] => 0
[2] =>
[Microsoft]
[ODBC Driver Manager] Driver\'s SQLSetConnectAttr failed
)
[1] => Array (
[0] => 01000
[SQLSTATE] => 01000
[1] => 5701
[code] => 5701
[2] =>
[Microsoft]
[ODBC Driver 11 for SQL Server]
[SQL Server]Changed database context to \'database\'.
)
[2] => Array (
[0] => 01000
[SQLSTATE] => 01000
[1] => 5703
[code] => 5703
[2] =>
[Microsoft]
[ODBC Driver 11 for SQL Server]
[SQL Server]Changed language setting to us_english.
)
它能够很好地访问数据库。当我尝试使用伪造的 user/pass 时,出现 401 错误 [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'other user'
我发现一个页面说要禁用池化,我在 $conectionOptions 数组中设置了一个 ConnectionPooling 选项,同时设置了 0 和 1,并且得到了相同的结果。我也尝试通过 OBDC GUI 将其更改为相同的结果。
我无法成功连接到数据库。我也找不到与我收到的错误代码相关的任何文档。谢谢
尝试更新 SQL Server
驱动程序!!
您可能可以通过谷歌搜索“Download ODBC Driver for SQL Server
”找到这些驱动程序。
就我而言,版本 17 或更高版本解决了这个问题。
此外,尝试 PDO-SQL-Server 示例(也许 PDO 实现有效)。
示例:
<?php
function mssqlConnect() {
$ServerName = "xxx";
try {
$conn = new PDO( "sqlsrv:server=$ServerName;Database = database", 'user', 'password');
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch( PDOException $e ) {
die( "Connection could not be established - " . $e->getMessage() );
}
echo "Connection established";
return $conn;
}
function findEmployee($name) {
$conn = mssqlConnect();
$stmt = $conn->prepare('SELECT * FROM employees WHERE name = :name');
if ($stmt->execute(array('name' => $name))) {
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
// do something with $row
}
} else {
echo 'Query error - ' . join(' ', $stmt->errorInfo());
}
// Close connection which execute created.
$stmt->closeCursor();
}
?>
尝试安装最新版本的 ODBC 驱动程序
我正在尝试从我的 PHP 代码访问 SQL 数据库。我目前正在使用 XAMPP 8.0.6 和 PHP 8.0.6。这在安装了 PHP 7.4.19 和相关扩展的机器上也不起作用。
已安装的驱动程序/扩展:
- sql-srv: php_sqlsrv_80_ts_x64.dll
- sql-pdo: php_pdo_sqlsrv_80_ts_x64.dll
- OBDC 驱动程序:10.0.19041.1
sqlsrv 和 sql-pdo 正确放置在 php.ini 中。使用 OBDC gui 连接到数据库,我连接成功。
我试图从数据库中获取数据的代码是:
function mssqlConnect(){
$ServerName = "xxx";
$ConnectionTimeout = 30;
$connectionOptions = ["Database"=>"database","UID"=>"user","PWD"=>"password", "LoginTimeout" => $ConnectionTimeout];
$sqlconn = sqlsrv_connect($ServerName,$connectionOptions);
if( $sqlconn ) {
echo "Connection established";
} else {
echo "Connection could not be established";
die( print_r( sqlsrv_errors(), true));
}
}
尝试 运行 时,我收到以下消息:
Connection could not be established.
Array (
[0] => Array (
[0] => IM006
[SQLSTATE] => IM006
[1] => 0
[code] => 0
[2] =>
[Microsoft]
[ODBC Driver Manager] Driver\'s SQLSetConnectAttr failed
)
[1] => Array (
[0] => 01000
[SQLSTATE] => 01000
[1] => 5701
[code] => 5701
[2] =>
[Microsoft]
[ODBC Driver 11 for SQL Server]
[SQL Server]Changed database context to \'database\'.
)
[2] => Array (
[0] => 01000
[SQLSTATE] => 01000
[1] => 5703
[code] => 5703
[2] =>
[Microsoft]
[ODBC Driver 11 for SQL Server]
[SQL Server]Changed language setting to us_english.
)
它能够很好地访问数据库。当我尝试使用伪造的 user/pass 时,出现 401 错误 [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'other user'
我发现一个页面说要禁用池化,我在 $conectionOptions 数组中设置了一个 ConnectionPooling 选项,同时设置了 0 和 1,并且得到了相同的结果。我也尝试通过 OBDC GUI 将其更改为相同的结果。
我无法成功连接到数据库。我也找不到与我收到的错误代码相关的任何文档。谢谢
尝试更新 SQL Server
驱动程序!!
您可能可以通过谷歌搜索“Download ODBC Driver for SQL Server
”找到这些驱动程序。
就我而言,版本 17 或更高版本解决了这个问题。
此外,尝试 PDO-SQL-Server 示例(也许 PDO 实现有效)。
示例:
<?php
function mssqlConnect() {
$ServerName = "xxx";
try {
$conn = new PDO( "sqlsrv:server=$ServerName;Database = database", 'user', 'password');
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch( PDOException $e ) {
die( "Connection could not be established - " . $e->getMessage() );
}
echo "Connection established";
return $conn;
}
function findEmployee($name) {
$conn = mssqlConnect();
$stmt = $conn->prepare('SELECT * FROM employees WHERE name = :name');
if ($stmt->execute(array('name' => $name))) {
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
// do something with $row
}
} else {
echo 'Query error - ' . join(' ', $stmt->errorInfo());
}
// Close connection which execute created.
$stmt->closeCursor();
}
?>
尝试安装最新版本的 ODBC 驱动程序