php - 无法通过 PHP 连接到 msSQL 数据库

标签 php sql sql-server sql-server-2005 connection

我正在使用当前代码尝试访问 msSQL 2005 数据库:

<?php
$myServer = "[server]";
$myUser = "[username]";
$myPass = "[password]";
$myDB = "[db]";

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer");

//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
  or die("Couldn't open database $myDB");

//declare the SQL statement that will query the database
$query = "SELECT id, name, year ";
$query .= "FROM cars ";
$query .= "WHERE name='BMW'";

//execute the SQL query and return records
$result = mssql_query($query);

$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";

//display the results
while($row = mssql_fetch_array($result))
{
  echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>

它返回以下内容:

Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: XXXXXXX in D:\xxxxx.xxx\xxxx.php on line 16
Couldn't connect to SQL Server on XXXXXXX

您认为问题出在哪里?

最佳答案

在我看来,您的某个 DLL 版本错误。从 SQL2000 到 SQL2005 的迁移存在某种问题,PHP 的创建者没有自行解决。这里有各种关于它的帖子:the following link

我相信 DLL 是 ntwdblib.dll,版本至少需要是 2000.80.194.0 版本。如果您正在运行 Apache 或 WampServer,则存在一个相同的 dll,其中存储 Apache DLL 需要被覆盖。

注意:几天前我遇到了这个问题,找到正确的 DLL 并进行覆盖后,它就可以正常工作了。

此外:您可能需要设置远程连接。 SQL Server 2005 默认禁用远程连接。您可以通过运行 SQL 外围应用配置实用程序来允许远程连接。

关于php - 无法通过 PHP 连接到 msSQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/469964/

相关文章:

sql - 如何使用 postgres 将 json 数据插入到我的表中

sql - SQL中 "GO"语句的真正作用?

SQL Server IF NOT EXISTS 用法?

php - 具有现有值的 PDO 更新表?

php - 何时适合使用 ArrayObject 而不是 Array

sql - Laravel 5.3 DB 选择不同的数据库

sql - Oracle 中的匿名 TABLE 或 VARRAY 类型

sql-server - 授予用户创建新数据库的权限,但拒绝现有数据库的权限

php 安全地检索 session 数据

php - Symfony2 : How do I use in a form (query builder) left join and isnull?