我正在尝试使用PHP连接到SQL Server (MSSQL)数据库。我激活了DBO插件并试图使用它,但是当我定义对象并运行代码时,我得到了错误:Connection failed: could not find driver。正如您从我的代码中看到的,我已经验证了dbo驱动程序是否已加载。我下载了其中一个答案中链接的sqlsrv驱动程序,但仍然无法连接。我遗漏了什么?( index.php和submit.php这两个文件在同一个目录中,这就是整个项目)(我在一台Windows计算机上,但这可能是相关的,也可能不相关)
index.php:
<html>
<head>
</head>
<body>
<form class="my-form" action="submit.php">
<input type="text" name="field" />
<input type="submit" value="Submit" />
</form>
</body>
</html>submit.php:
<html>
<head>
</head>
<body>
<h1>page loaded</h1>
<h1><?php if (extension_loaded('pdo')) {
echo 'pdo extension loaded by php';
} ?></h1>
<?php
$myServer = "xxxxxxxxxxxxxxxx";
$myUser = "xxxxxxx";
$myPass = "xxxxxx";
$myDB = "xxxxxxxxxxxx";
$serverName = $myServer; //serverName\instanceName
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>$myDB, "UID"=>$myUser, "PWD"=>$myPass);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
</body>
</html>发布于 2019-10-08 02:57:14
在PHP 7中删除了mssql*扩展。
https://www.php.net/manual/en/function.mssql-connect.php
相反,您应该考虑PDO。
编辑:
PDO for MSSQL需要适当的客户端库,您可以在以下位置找到:https://docs.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-2017
不要被版本号搞糊涂了(它不是PHP版本...)请查看说明,有些驱动程序仅适用于特定的MSSQL服务器版本。
https://stackoverflow.com/questions/58275453
复制相似问题