PHP連接MSSQL2008/2005資料庫與以往的連接mssql2000是不一樣的,連接mssql2008/2005是需要自己添加PHP對MSSQL連接的驅動擴充了,而我們常用的hp.ini中的extension=php_mssql.dll擴充只適用連接於MSSQL2000,下面我們就來看看對此的解決方案。
1.下載擴充功能
(1)去官方下載一個SQL Server Driver for PHP的擴充包,我是在這裡下載的http://www.microsoft.com/en- us/download/details.aspx?id=20098【記得下載後好像是要先安裝然後再解壓縮】
(2)您也可以直接從本站下載(我之前下載的,來自microsoft官方)【點擊此處直接下載】
將下載下來的rar文件解壓縮後你就會得到一堆的.dll文件
下載驅動程序,下載後安裝釋放程序,裡面有以下文件:
php_pdo_sqlsrv_52_nts.dll
php_pdo_sqlsrv_52_ts.dll
php_pdo_sqlsrv_53_nts_vc6.dll
php_pdo_sqlsrv_53_nts_nts_vc6.dll
php_pdo_llsqlsrv_53_nts_nts_vc6.dll
php_pdo_llsqlsrv_53_nts_nts_vc6。 3_ts_vc9.dll
php_sqlsrv_52_nts.dll
php_sqlsrv_52_ts.dll
php_sqlsrv_53_nts_vc6.dll
php_sqlsrv_53_nts_vc9.dll
php_sqlsrv_53_ts_vc6.dll
php_sqlsrv_53_ts_vc9.dll
SerServerDriverForPHfchmrv_53_ts_vc9.dll
ServerDrive ##SQLServerDriverForPHP_Readme.htm(自述文件)
extension=php_sqlsrv_52_ts_vc6.dll
extension=php_pdo_sqlsrv_5_ll
extension=php_pdo_sqlsrv_5_ll
extension=php_pdo_sqlsrv_5_llts_#6. 2)將;extension=php_pdo.dll前面的;去掉,開啟pdo連線擴充
(3)重新啟動apache
4.連線資料庫(pdo連線)
<?php $servern="SFKFK27EL8FJ\SQLTRY"; $coninfo=array("Database"=>"try2","UID"=>"sa","PWD"=>"123"); $conn=sqlsrv_connect($servern,$coninfo) or die ("连接失败!"); $val=sqlsrv_query($conn,"select * from usertable"); while($row=sqlsrv_fetch_array($val)){ echo $row[1]."<br />"; } sqlsrv_close($conn); ?>
5.範例
連結範例:
mssql_lib.php檔案如下:
<?php class DB { var $con = null; function __construct($dbhost,$dbuser,$dbpass,$dbname) { $connectionInfo = array("UID"=>$dbuser,"PWD"=>$dbpass,"Database"=>$dbname); $this->con = sqlsrv_connect($dbhost,$connectionInfo); } function query($sql){ $result = sqlsrv_query($this->con, $sql); } function getRow($sql){ $result = sqlsrv_query($this->con, $sql); $arr = array(); while($row = sqlsrv_fetch_array($result)) { $arr[] = $row; } return $arr[0]; } function getAll($sql){ $result = sqlsrv_query($this->con, $sql); $arr = array(); while($row = sqlsrv_fetch_array($result)) { $arr[] = $row; } return $arr; } function __destruct() { unset($con); } }
test.php頁面如下:
//简单调用 $db = new DB(DB_HOST, DB_USER, DB_PASS, DB_NAME); $sql = "select * from crm_order_batch where (status=0 or status is null) and lock_id is not null "; $orders_add_list = $db->getAll($sql);
相關推薦:
以上是PHP連接MSSQL資料庫(SQLSRV)實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!