首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将PHP连接到windows上的SQL Server

问将PHP连接到windows上的SQL Server
EN

Stack Overflow用户
提问于 2019-10-08 02:46:39
回答 1查看 2.1K关注 0票数 0

我正在尝试使用PHP连接到SQL Server (MSSQL)数据库。我激活了DBO插件并试图使用它,但是当我定义对象并运行代码时,我得到了错误:Connection failed: could not find driver。正如您从我的代码中看到的,我已经验证了dbo驱动程序是否已加载。我下载了其中一个答案中链接的sqlsrv驱动程序,但仍然无法连接。我遗漏了什么?( index.php和submit.php这两个文件在同一个目录中,这就是整个项目)(我在一台Windows计算机上,但这可能是相关的,也可能不相关)

index.php:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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://docs.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-2017

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58275453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档