可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am using the current code in attempt to access a 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?
回答1:
It sounds to me like one of your DLLs is the wrong version. There was an issue of some sort with the move from SQL2000 to SQL2005 that the creators of PHP didn't resolve themselves. 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. If you're running Apache or WampServer, there is an identical dll where the Apache DLLs are stored that needs to be overwritten.
Note: I was having this issue a few days ago and finding the correct DLLs and overwriting both allowed it to work.
Also: You may need to setup remote connections. Sql Server 2005 has remote connections disabled by default. You can allow remote connections by running the SQL Surface Area Configuration utility.
回答2:
Try calling mssql_get_last_message()
to get the last error message:
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer. Error: " . mssql_get_last_message());
回答3:
stop using
mssql_connect
and start using
sqlsrv_connect
That will save you a lot of headaches. Plus, the function *mssql_connect* has been deprecated.
For using sqlsrv_connect, you must download the driver and install it as an extension for PHP to recognize the sqlsrv functions. 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
The right steps for installation are clearly explained by Microsoft itself at http://www.microsoft.com/en-us/download/details.aspx?id=20098
- download the driver. 2. put it in the PHP ext folder. 3. update the php.ini 4. restart the server.
After installing the sql server driver, then simply follow the instructions from 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!
回答4:
Invalid credentials, if your not using localhost be sure you add the server's ip to the safe list.
回答5:
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.
回答6:
First try to do something like phpinfo()
on your browser, than see which databases are allowed.
If you don't see anything like mssql
then it is not configured. So use the odbc_connect()
which will be configured...your connection string will be like this:
$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.
回答7:
Download the driver in this site http://www.microsoft.com/en-us/download/details.aspx?id=20098, then open your php.ini file and add the DLL that you download.
回答8:
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:: The MS SQL server. e.g. 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
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!!