【发布时间】:2019-10-07 18:46:39
【问题描述】:
我正在尝试使用 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>
提交.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>
【问题讨论】:
-
检查服务器错误日志中是否有任何错误消息。
-
您确定在服务器上安装了
mssql_connect扩展吗?如果没有,请与您的网络管理员联系以进行安装。 -
Lars 在下面的答案中引用的 php.net 文章说 mssql 扩展已在 php7 中删除。他推荐使用 PDO...
-
@ivan_pozdeev 不,这是 pdo 和 ms sql,不是 mysql。
标签: php sql sql-server windows