当前位置 : 主页 > 网络编程 > PHP >

php链接sqlserver类

来源:互联网 收集:自由互联 发布时间:2021-06-28
sqlsrv 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-co
sqlsrv
 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;$i
 
  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;  
    }  
     
    //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);  
    }  
}  
?>
 
网友评论