php链接sqlserver类 不指定

jed , 2018-3-14 12:34 , 代码编程 , 评论(0) , 阅读(52741) , Via 本站原创 | |
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";  
    
    //connection  
    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);  
    }  
    
    //query source  
    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;  
    }  
    
    //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);  
    }  
}  
?>  
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]