SQL server Mixed Mode authentication must be enabled for the server.
You must set the UID and PWD connection properties when attempting to establish a connection. UID and PWD must be mapped to a valid SQL Server user and password.
Attention:
The password containing the closing brace (}) must be escaped with another closing brace. For example, if the SQL Server password is "Pass}word", the value of the PWD connection property must be set to "Pass}}word".
The following precautions should be taken when connecting to SQL Server using SQL Server Authentication:
Let's look at a simple example:
The code is as follows |
Copy Code |
<?php $serverName = "(local)"; $uid = ' xxxx '; $pwd = ' xxxx '; $connectionInfo = Array ("UID" => $uid, "PWD" => $pwd, "Database" => "AdventureWorks"); $conn = Sqlsrv_connect ($serverName, $connectionInfo); if ($conn = = False) { echo "Could not connect to the database."; Die (Print_r (Sqlsrv_errors (), true)); } $tsql = "Select CONVERT (varchar), SUSER_SNAME ())"; $stmt = sqlsrv_query ($conn, $tsql); if ($stmt = = False) { echo "Query error." Die (Print_r (Sqlsrv_errors (), true)); } $row = Sqlsrv_fetch_array ($stmt); echo "Logged in User:". $row [0]; Sqlsrv_free_stmt ($stmt); Sqlsrv_close ($conn); ?> |