标题:php链接sqlserver类 出处:沧海一粟 时间:Wed, 14 Mar 2018 12:34:46 +0000 作者:jed 地址:http://www.dzhope.com/post/1123/ 内容: php链接sqlserver类 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); } //query source function query($sql,$params=array(),$open_close=1){ if($open_close){ $this->open(); } $array=array(); for($i=0;$iconvert2gbk($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; } //fetch data 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; } // $DB->count(select * from users) 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; } // $DB->count(select count(*) from users) 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); } } ?> Generated by Bo-blog 2.1.1 Release