Home >Backend Development >PHP Tutorial >PHP uses sql server to verify the method of connecting to the database
The example in this article describes how PHP uses sql server to verify the connection to the database. Share it with everyone for your reference. The specific analysis is as follows:
When you connect to SQL Server, the SQL Server Driver for PHP supports SQL Server authentication. You must consider the following points when using SQL Server authentication to connect to SQL Server.
SQL Server mixed mode authentication must be enabled on the server, the UID and PWD connection properties must be set when trying to establish a connection, and the UID and PWD must be mapped to a valid SQL Server user and password.
Note: Include the right Passwords enclosed in braces (}) 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 using SQL Server authentication to connect to SQL Server. Let’s look at a simple example. The code is as follows:
<?php $serverName = "(local)"; $uid = 'xxxx'; $pwd = 'xxxx'; $connectionInfo = array( "UID"=>$uid, "PWD"=>$pwd, "Database"=>"AdventureWorks"); $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn === false ) { echo "无法连接数据库."; die( print_r( sqlsrv_errors(), true)); } $tsql = "SELECT CONVERT(varchar(32), SUSER_SNAME())"; $stmt = sqlsrv_query( $conn, $tsql); if( $stmt === false ) { echo "查询错误."; die( print_r( sqlsrv_errors(), true)); } $row = sqlsrv_fetch_array($stmt); echo "登录的用户: ".$row[0]; sqlsrv_free_stmt( $stmt); sqlsrv_close( $conn); ?>
I hope this article will be helpful to everyone’s PHP programming. .
For more articles related to how PHP uses sql server to verify the connection to the database, please pay attention to the PHP Chinese website!