简体   繁体   中英

PHP Can't Connect to MS SQL Express 2008

I'm struggling to connect to my MS SQL Express 2008 edition from my PHP files, but it seems virtually impossible.

I've found several guides/notes on teh intarweb solving the issues, but none of these have helped me any further.

I've taken a script from another site and entered my database information, but I still get errors:

<?php
$myServer = "localhost";
$myUser = "demo_basic";
$myPass = "1234";
$myDB = "demo_basic"; 

//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 nitid, nisname ";
$query .= "FROM navitems";

//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["nitid"] . $row["nisname"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>

I get this error:

Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: localhost in C:\\inetpub\\vhosts\\dexterholding.dk\\httpdocs_rip\\sql.php on line 8 Couldn't connect to SQL Server on localhost

You can see for yourself at: http://www.dehold.net/_rip/sql.php

Any ideas? I'm running Windows Server 2008 with PLESK and PHP5 as FastCGI.

I found this guide which actually made it work for me:

http://samsami2u.wordpress.com/2008/06/30/how-to-connect-mssql-with-php/

使用MSSQL,您的服务器名称将如下所示: machinenameoraddress\\serverinstancename ,示例为192.168.14.201\\MSSQLEXPRESSTESTMACHINE\\MYTESTDBSERVER

It could be that the user account does not have permission to access SQL Server.

Although looking at your code you are using SQL auth and not Windows authentication, presumably this account is set up in SQL Server and it is configured to allow SQL Auth?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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