sql server 2008 login using php
What I need to do is to be able to connect to a sql database on a sql server 2008 database using a php script. The script is running on IIS 6 on the same server as the sql server. I am using php 5.3.2 and the command sqlsrv_connect to connect. I am passing this method a user name, password, database, and server to connect to. However when I do I get the error "Login failed for user ......." I do not know if the sql server 2008 is set up correctly or not to accept a connection like this.
I have IIS using an anon connection because the php script has a user name a password being passed into the script. I need to be able to use these values to confirm a login for the the sql server.
Actual error:
Array (
[0] => Array (
[0] => 28000 [SQLSTATE] => 28000
[1] => 18456 [code] => 18456
[2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'ecriss'.
[message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user '..........'.
)
[1] => Array (
[0] => 28000 [SQLSTATE] => 28000
[1] => 18456 [code] => 18456
[2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user '...........'.
开发者_运维问答 [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user '..........'.
)
)
Thank you for the help
Your password looks to be incorrect, have you checked that the user exists and the password is valid? Is the MSSQL extension available, have you checked phpinfo()?
Try conncting like so (http://www.webcheatsheet.com/php/connect_mssql_database.php)
<?php
$myServer = "localhost";
$myUser = "your_name";
$myPass = "your_password";
$myDB = "examples";
//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);
?>
精彩评论