Home  >  Article  >  Backend Development  >  php使用sql server验证连接数据库的方法_PHP

php使用sql server验证连接数据库的方法_PHP

WBOY
WBOYOriginal
2016-06-01 11:06:35997browse

本文实例讲述了php使用sql server验证连接数据库的方法。分享给大家供大家参考。具体分析如下:

当您连接到 SQL Server 时,SQL Server Driver for PHP 支持 SQL Server 身份验证,在使用 SQL Server 身份验证连接到 SQL Server 时必须考虑以下几点.

必须对服务器启用 SQL Server 混合模式身份验证,在尝试建立连接时必须设置 UID 和 PWD 连接属性,UID 和 PWD 必须映射到有效的 SQL Server 用户和密码.

注意:包含右大括号 (}) 的密码必须使用另一个右大括号进行转义,例如,如果 SQL Server 密码为“pass}word”,则 PWD 连接属性的值必须设置为“pass}}word”.

在使用 SQL Server 身份验证连接到 SQL Server 时应采取以下预防措施,下面看一个简单的实例,代码如下:

代码如下:

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

希望本文所述对大家的php程序设计有所帮助。

Statement:
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