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']."<br/>";
}
?>
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連接部分代碼段:
<?php
$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連接部分代碼段:
<?php
$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";
}
?>