现在的位置: 网页制作教程网站制作经验 >正文
php网上学习

PHP连接MSSQL2008/2005数据库(SQLSRV)配置方法

发表于2017/8/9 网站制作经验 0条评论 ⁄ 热度 2,988℃

PHP连接MSSQL数据库,原来使用的是MSSQL2000,配置相对简单一点,只需要在php.ini配置文件中修改extension=php_mssql.dll就可以了。现在将数据库升级到了SQL SERVER2008,就需要自己添加PHP对MSSQL连接的驱动扩展SQLSRV了。

1.下载SQLSRV扩展

(1)去官方下载一个SQL Server Driver for PHP的扩展包,我是在这里下载的http://www.microsoft.com/en-us/download/details.aspx?id=20098。

下载驱动程序,下载后安装释放程序,里面有以下文件:

php_pdo_sqlsrv_52_nts.dll

php_pdo_sqlsrv_52_ts.dll

php_pdo_sqlsrv_53_nts_vc6.dll

php_pdo_sqlsrv_53_nts_vc9.dll

php_pdo_sqlsrv_53_ts_vc6.dll

php_pdo_sqlsrv_53_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

SQLServerDriverForPHP.chm(手册,英文够好的话,可以看看,嘿嘿)

SQLServerDriverForPHP_License.rtf

SQLServerDriverForPHP_Readme.htm(自述文件)

2.添加扩展

根据(vc6/vc9)需要选择扩展,我的环境是WAMP(php5.2.6/apache2.2.8),我选用的是php_sqlsrv_52_ts_vc6.dll,php_pdo_sqlsrv_52_ts_vc6.dll这两个文件,复制到wamp安装目录下的ext目录下,我的ext目录是在wamp/bin/php/php5.2.6/ext/

3.配置php.ini

(1)在php.ini的Dynamic Extensions中添加如下两条扩展:

extension=php_sqlsrv_52_ts_vc6.dll

extension=php_pdo_sqlsrv_52_ts_vc6.dll

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

链接示例:

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);
  • 暂无评论