Home > Backend Development > PHP Tutorial > Use sql server verification to connect to the database in php_PHP tutorial

Use sql server verification to connect to the database in php_PHP tutorial

WBOY
Release: 2016-07-13 10:46:36
Original
719 people have browsed it

SQL Server Driver for PHP supports SQL Server Authentication when you connect to SQL Server. The following points must be considered when connecting to SQL Server using SQL Server Authentication:

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. The UID and PWD must map to a valid SQL Server user and password.
Note:

Passwords containing a 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".
You should take the following precautions when connecting to SQL Server using SQL Server Authentication:
Let’s look at a simple example:

The code is as follows
 代码如下 复制代码

$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);
?>

Copy code
$serverName = "(local)";

$uid = 'xxxx';

$pwd = 'xxxx';
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