- 首頁
- 開發(fā)者
- 技術(shù)學(xué)習(xí)
- 閱讀詳情
PHP連接SQLServer實(shí)例,PHP鏈接SQLServer函數(shù)類
PHP連接SQLServer實(shí)例,PHP鏈接SQLServer函數(shù)類。代碼如下:
<?php class sqlsrv{ private $error_log = array(); private $sql_log = array(); private $query_id; private $num_rows; private $conn; private $server="."; private $userid="sa"; private $password="sa"; private $database="db"; function sqlsrv() { } function open(){ $this->conn = @sqlsrv_connect($this->server, array('UID' => $this->userid ,'PWD'=> $this->password, 'Database' => $this->database)); if($this->conn === false) { $this->error_log[] = sqlsrv_errors(); die(); } } function close(){ sqlsrv_close($this->conn); } function query($sql,$params=array(),$open_close=1){ if($open_close){ $this->open(); } $array=array(); for($i=0;$i<count($params);$i++) { if(is_array($params[$i])){ $array[$i]=array($this->convert2gbk($params[$i][0]),$params[$i][1]); } else{ $array[$i] =array($this->convert2gbk($params[$i]),SQLSRV_PARAM_IN); } } $stmt = sqlsrv_query($this->conn, $sql,$array); $this->sql_log[] = $sql; $res=false; if($stmt === false) { $this->error_log[] = sqlsrv_errors(); } else { $this->query_id = $stmt; $res=$this->num_rows = $this->affectedRows(); } if($open_close){ $this->close(); } return $res; } function fetch_all($sql,$params=array(),$open_close=1) { if($open_close){ $this->open(); } $this->query($sql,$params,0); $data = array(); while($row = @sqlsrv_fetch_array($this->query_id)) { $data[] = $row; } foreach ($data as $key => $value) { foreach ($value as $key2 => $value2) { @$data[$key][$key2] = $this->convert2utf8($value2); } } if($open_close){ $this->close(); } return $data; } function fetch_one($sql,$params=array(),$open_close=1){ if($open_close){ $this->open(); } $this->query($sql,$params,0); @$res= sqlsrv_fetch_array($this->query_id); if(is_array($res)){ foreach ($res as $key => $value) { @$res[$key] = $this->convert2utf8($value); } } if($open_close){ $this->close(); } return $res; } function count($sql,$params=array(),$open_close=1){ if($open_close){ $this->open(); } $count=$this->fetch_one($sql,$params,0); $res= $count[0]; if($open_close){ $this->close(); } return $res; } function affectedRows() { $res=($this->query_id) ? @sqlsrv_num_rows($this->query_id) : false; if($res==false){ $res=($this->query_id) ? @sqlsrv_rows_affected($this->query_id) : false; } return $res; } function convert2utf8($str) { return iconv("gbk","utf-8",$str); } function convert2gbk($str) { return iconv("utf-8","gbk",$str); } } ?>
本文為「本站原創(chuàng)」,未經(jīng)我們許可,嚴(yán)謹(jǐn)任何人或單位以任何形式轉(zhuǎn)載或刊載本文章,我們保留依法追究侵權(quán)的權(quán)力!
微信聯(lián)系我們
使用微信掃一掃
昵稱:億百天技術(shù)
公司:湖北億百天信息技術(shù)有限公司
電話:027-88773336
手機(jī):15342213852
郵箱:serviceebaitian.cn
我來說兩句