Home  >  Article  >  php教程  >  php中使用sql server验证进行连接数据库

php中使用sql server验证进行连接数据库

WBOY
WBOYOriginal
2016-05-25 12:57:291234browse

当您连接到 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 时应采取以下预防措施,下面看一个简单的实例,代码如下:

<?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)); 
	}//开源代码phprm.com 
	 
	$row = sqlsrv_fetch_array($stmt); 
	echo "登录的用户: ".$row[0]; 
	 
	sqlsrv_free_stmt( $stmt); 
	sqlsrv_close( $conn); 
	

本文地址:

转载随意,但请附上文章地址:-)

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