Rumah > Artikel > pembangunan bahagian belakang > php SQL Server Authentication连接部分代码_PHP教程
php教程 SQL Server Authentication连接部分代码
*/
$serverName = "(local)"; //数据库教程服务器地址
$uid = "pandao"; //数据库用户名
$pwd = "1987"; //数据库密码
$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>"test");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn == false)
{
echo "连接失败!";
die( print_r( sqlsrv_errors(), true));
}
$query = sqlsrv_query($conn, "SELECT TOP 10 nid,title,content FROM test.dbo.news");
while($row = sqlsrv_fetch_array($query)){
echo $row['nid']."-----".$row['title']."
";
}
?>
sqlserver2005或者sqlserver2008
请先到此处下载驱动
http://www.microsoft.com/downloads/details.asp教程x?FamilyId=61BF87E0-D031-466B-B09A-6597C21A2E2A&displaylang=en
下载后解压文件
配置:
1.将解压的php_sqlsrv.dll 和 php_sqlsrv_ts.dll 放到PHP的扩展目录中(PHPEXT)。
2.编辑php.ini文件(在windows文件夹下),添加以下扩展:
extension=php_sqlsrv.dll
extension=php_sqlsrv_ts.dll
3.去掉;extension=php_mssql.dll前的分号
SQL Server常用的有两种认证方式,一种是本地系统账户认证(Windows Authentication ),一种是使用用户名和密码(SQL Server Authentication ),第二种认证方式必须启用SQL Server的混合模式。
1.Windows Authentication连接部分代码段:
$serverName = "(local)";
$connectionInfo = array("Database"=>"TestingInfo","ConnectionPooling"=>false);
$conn = sqlsrv_connect( $serverName,$connectionInfo);
if(! $conn){
echo "o no!!!!!";
die( print_r( sqlsrv_errors(), true));
}else{
echo "yes done";
}?>
2.SQL Server Authentication连接部分代码段:
$serverName = "(local)";
$uid = "dbusername";//数据库用户名
$pwd = "dbuserpass";//数据库用户密码
//下面的Database为数据库名
$connectionInfo = array("UID"=>$uid,"PWD"=>$pwd,"Database"=>"dbname");
$conn = sqlsrv_connect( $serverName,$connectionInfo);
if(! $conn){
echo "o no!!!!!!!";
die( print_r( sqlsrv_errors(), true));
}else{
echo "yes done";
}
?>