PHP uses sql server to verify the method of connecting to the database

高洛峰
Release: 2023-03-04 16:14:01
Original
1172 people have browsed it

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 = &#39;xxxx&#39;; 
$pwd = &#39;xxxx&#39;; 
$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); 
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template