標籤: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.測試代碼
<?phpheader("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