When you connect to SQL Server, SQL Server Driver for PHP supports SQL Server Authentication. When using SQL Server authentication to connect to SQL Server, you must consider the following:
You must enable SQL Server Hybrid Authentication on the Server.
When trying to establish a connection, you must set the UID and PWD Connection Properties. UID and PWD must be mapped to valid SQL Server users and passwords.
Note:
The password containing the right braces (}) must be escaped using another right braces. For example, if the SQL Server password is "pass} word", the value of the PWD connection attribute must be set to "pass} word ".
When using SQL Server authentication to connect to SQL Server, take the following precautions:
Here is 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 "unable to connect to database ."; Die (print_r (sqlsrv_errors (), true )); } $ Tsql = "select convert (varchar (32), 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 "Login User:". $ row [0]; Sqlsrv_free_stmt ($ stmt ); Sqlsrv_close ($ conn ); ?> |