简体   繁体   English

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

[英]Unable to connect to msSQL database via PHP

I am using the current code in attempt to access a msSQL 2005 db: 我正在使用当前代码尝试访问msSQL 2005 db:

<?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);
?>

It is returning the following: 它返回以下内容:

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

What do you think the problem is? 您认为问题是什么?

It sounds to me like one of your DLLs is the wrong version. 听起来像你的一个DLL是错误的版本。 There was an issue of some sort with the move from SQL2000 to SQL2005 that the creators of PHP didn't resolve themselves. 从SQL2000到SQL2005的转变存在某种问题,即PHP的创建者没有解决自己。 There are a variety of posts about it here: the following link 这里有各种各样的帖子: 以下链接

I believe the DLL is ntwdblib.dll and the version needs to be version 2000.80.194.0 at the very least. 我相信DLL是ntwdblib.dll,版本至少需要版本2000.80.194.0。 If you're running Apache or WampServer, there is an identical dll where the Apache DLLs are stored that needs to be overwritten. 如果您正在运行Apache或WampServer,则存在一个相同的dll,其中存储需要覆盖的Apache DLL。

Note: I was having this issue a few days ago and finding the correct DLLs and overwriting both allowed it to work. 注意:几天前我遇到了这个问题并找到了正确的DLL并覆盖它们都允许它工作。

Also: You may need to setup remote connections. 另外:您可能需要设置远程连接。 Sql Server 2005 has remote connections disabled by default. Sql Server 2005默认禁用远程连接。 You can allow remote connections by running the SQL Surface Area Configuration utility. 您可以通过运行SQL Surface Area Configuration实用程序来允许远程连接。

Try calling mssql_get_last_message() to get the last error message: 尝试调用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());

stop using 停止使用

mssql_connect mssql_connect

and start using 并开始使用

sqlsrv_connect sqlsrv_connect

That will save you a lot of headaches. 这将为您节省很多麻烦。 Plus, the function *mssql_connect* has been deprecated. 另外,函数* mssql_connect *已被弃用。

For using sqlsrv_connect, you must download the driver and install it as an extension for PHP to recognize the sqlsrv functions. 要使用sqlsrv_connect,必须下载驱动程序并将其安装为PHP的扩展,以识别sqlsrv函数。 Download the driver from Microsoft Download Center (search for "sql server php driver"), at the time of this post the download url was: http://www.microsoft.com/en-us/download/details.aspx?id=20098 从Microsoft下载中心下载驱动程序(搜索“sql server php driver”),在本文发布时,下载URL为: http//www.microsoft.com/en-us/download/details.aspx?id = 20098

The right steps for installation are clearly explained by Microsoft itself at http://www.microsoft.com/en-us/download/details.aspx?id=20098 Microsoft自己在http://www.microsoft.com/en-us/download/details.aspx?id=20098上清楚地解释了正确的安装步骤

  1. download the driver. 下载驱动程序。 2. put it in the PHP ext folder. 2.将它放在PHP ext文件夹中。 3. update the php.ini 4. restart the server. 3.更新php.ini 4.重启服务器。

After installing the sql server driver, then simply follow the instructions from http://www.php.net/manual/en/function.sqlsrv-connect.php . 安装sql server驱动程序后,只需按照http://www.php.net/manual/en/function.sqlsrv-connect.php中的说明操作即可。 Below a quick snippet: 下面是一个快速片段:

<?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));
}
?>

Vote up! 投票!

I had some difficulties with this a few months ago, and I found that the only way to make it work was to include the instance name when specifying the server. 几个月前我遇到了一些困难,我发现使其工作的唯一方法是在指定服务器时包含实例名称。 For example: 例如:

$myServer = "SERVER\INSTANCENAME";

Specifying just the server would not work, even with TCP/IP enabled. 即使启用了TCP / IP,仅指定服务器也不起作用。

First try to do something like phpinfo() on your browser, than see which databases are allowed. 首先尝试在浏览器上执行类似phpinfo()操作,而不是查看允许的数据库。

If you don't see anything like mssql then it is not configured. 如果你没有看到像mssql这样的东西,那么就没有配置它。 So use the odbc_connect() which will be configured...your connection string will be like this: 所以使用将配置的odbc_connect() ...你的连接字符串将是这样的:

$server = '';
$user = '';
$password = '';
$database = '';
$connection = odbc_connect("Driver={SQL Server Native Client `11.0};Server=$server;Database=$database;", $user, $password);`

If you are using mssql 2005 or 2008, then change d 11.0 to 10.0 and for other versions just change to the version of your mssql. 如果您使用的是mssql 2005或2008,则将d 11.0更改为10.0,而对于其他版本,只需更改为mssql的版本。

在此站点下载驱动程序http://www.microsoft.com/en-us/download/details.aspx?id=20098 ,然后打开您的php.ini文件并添加您下载的DLL。

Late response, but it might help someone. 迟到的回应,但它可能会帮助某人。 we are nearly there, it is the problem with your connection parameters, probably you need to give servername as IP:port 我们差点就在那里,这是你的连接参数的问题,可能你需要将servername作为IP:port

servername:: The MS SQL server. servername :: MS SQL服务器。 eg hostname:port (Linux), or hostname,port (Windows). 例如hostname:port (Linux), or hostname,port (Windows).

Server Name should be "server\\instance" An instance is nothing more than specific port address different than 1433... so just discover the port on mssql server and then try to connect using: ip:port 服务器名称应该是“server \\ instance”一个实例只不过是不同于1433的特定端口地址...所以只需发现mssql服务器上的端口,然后尝试连接使用: ip:port

Example code that works completely fine for me:: 示例代码对我来说完全正常::

    ini_set('display_errors', '1');
    // $myServer = "winsrv22.somedns.co.uk:22320";//this style works as well
    $servername = "123.21.47.23:22320";
    $myUser = "UserName";
    $myPass = 'xxxxxxxx';
    $myDB = "[database]"; 

    //connection to the database
    $dbhandle = mssql_connect($servername, $myUser, $myPass)
        or die("Couldn'tt connect to SQL Server on $myServer"); 
    if($dbhandle) {
     echo "Success, Connected\r\n";
    } else {
        echo "problem :( \r\n";
    }

Hope this helps some one :) Happy coding!! 希望这有助于一个人:)快乐的编码!

无效凭据,如果您未使用localhost,请确保将服务器的ip添加到安全列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM