我正在使用当前代码尝试访问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你认为问题出在哪里?
发布于 2009-01-22 17:19:02
在我看来,您的某个DLL版本不正确。在从SQL2000迁移到SQL2005的过程中出现了一些问题,PHP的创建者并没有自己解决这个问题。这里有很多关于它的帖子:the following link
我相信DLL是ntwdblib.dll,版本至少需要是2000.80.194.0。如果您运行的是Apache或WampServer,则存储Apache的位置有一个相同的dll需要被覆盖。
注意:几天前我遇到了这个问题,找到正确的DLL和覆盖都允许它工作。
另外:您可能需要设置远程连接。默认情况下,Sql Server2005禁用远程连接。您可以通过运行SQL外围应用配置器实用程序来允许远程连接。
发布于 2009-01-22 17:12:37
尝试调用mssql_get_last_message()以获取最后一个错误消息:
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer. Error: " . mssql_get_last_message());发布于 2014-03-08 02:31:17
停止使用
mssql_connect
并开始使用
sqlsrv_connect
这会帮你省去很多麻烦。此外,函数*mssql_connect*已被弃用。
要使用sqlsrv_connect,必须下载驱动程序并将其作为扩展安装,以便识别sqlsrv函数。从微软下载中心下载驱动程序(搜索"sql server php driver"),在这篇文章中,下载地址是:http://www.microsoft.com/en-us/download/details.aspx?id=20098
安装的正确步骤由微软自己在http://www.microsoft.com/en-us/download/details.aspx?id=20098上清楚地解释
安装完sql server驱动程序后,只需按照http://www.php.net/manual/en/function.sqlsrv-connect.php的说明进行操作即可。下面是一个简短的代码片段:
<?php
$serverName = "serverName\sqlexpress"; //serverName\instanceName
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"dbName");
$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));
}
?>投赞成票!
https://stackoverflow.com/questions/469964
复制相似问题