码迷,mamicode.com
首页 > 数据库 > 详细

PHP5.4连接sqlserver

时间:2014-09-01 09:14:22      阅读:402      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   strong   ar   for   div   

1.下载微软的php连接驱动:SQLSRV30.EXE(5.4对应,后面的native client要用2012)/SQLSRV20.EXE(5.3对应,native client要用2008)

2.解压SQLSERV30.EXE,拷贝对应extension到php的ext目录

3.配置php.ini

extension=php_sqlsrv_54_ts.dll(54为5.4版本,ts为线程安全,nts为非线程安全)

mssql.secure_connection = Off改为on 很多教程没写这个

4.重启IIS/Apache

5.在sqlserver服务器配置TCP/IP连接

6.在php服务器下载安装sqlserver native client

7.测试代码

 

<?php
header("Content-Type: text/html;charset=utf-8");

class mssql{
    private $host;
    private $username;
    private $password;
    private $database;
    private $handle;
    private function __construct (Array $config){
        $this -> host = $config[‘host‘];
        $this -> username = $config[‘username‘];
        $this -> password = $config[‘password‘];
        $this -> database = $config[‘database‘];
        $this -> init();
    }
    private function init() {
        $dsn = ‘sqlsrv:server = ‘.$this -> host.‘;database = ‘.$this->database;
        $this -> handle = new PDO($dsn,$this -> username, $this -> password);
    }
    public static function GetInstance(array $config = null) {
        if (null == $config) {
            return NULL;
        } 
        static $db = null;
        if (null == $db) {
            $c = __CLASS__;
            $db = new $c($config);
        };
        return $db -> handle;
    }
}

$config = array(
    ‘host‘ => ‘192.168.0.152,1433‘,
    ‘database‘ => ‘TimeTracker‘,
    ‘username‘ => ‘sa‘,
    ‘password‘ => ‘123‘
);
$mssql = mssql::GetInstance($config);
$result = $mssql->query(‘SELECT * FROM T_Sys_UserInfo‘);
foreach($result as  $row){
    echo $row[2];
}
?>

 

PHP5.4连接sqlserver

标签:style   blog   color   os   io   strong   ar   for   div   

原文地址:http://www.cnblogs.com/punkrocker/p/3948539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!