PHP连接SQLServer实例,PHP链接SQLServer函数类
PHP连接SQLServer实例,PHP链接SQLServer函数类。代码如下:
<?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); } } ?>
本文为「本站原创」,未经我们许可,严谨任何人或单位以任何形式转载或刊载本文章,我们保留依法追究侵权的权力!
微信联系我们
使用微信扫一扫
昵称:亿百天技术
公司:星空体育·(StarSky Sports)官方网站
电话:027-88773336
手机:15342213852
邮箱:serviceebaitian.cn
我来说两句