一个 php 连接 sql server的类

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
 
<?php
/**
 * @desc   A simple and convenient php sqlsrv  class
 */
class sqlsrv{
   
    var $error_log = array();
    var $sql_log = array();
    var $query_id;
    var $num_rows;
    var $conn;
   
    //connection
    function sqlsrv($server, $user, $pass, $dbname) {
        $this->conn = @sqlsrv_connect($server, array('UID' => $user ,'PWD'=> $pass, 'Database' => $dbname));
        if($this->conn === false) {
            $this->error_log[] = sqlsrv_errors();
            die();
        }
    }
   
    //query source
    function query($sql){
        $stmt = sqlsrv_query($this->conn, $sql);
        $this->sql_log[] = $sql;
        if($stmt === false) {
            $this->error_log[] = sqlsrv_errors();
        } else {
            $this->query_id = $stmt;
            $this->num_rows = $this->affectedRows();
        }
    }
   
    //fetch data
    function fetch_all($sql) {
        $this->query($sql);
        $data = array();
        while($row = @sqlsrv_fetch_array($this->query_id, SQLSRV_FETCH_ASSOC)) {
            $data[] = $row;
        }
        return $data;
    }
    // $DB->count(select   *   from  users)
    function fetch_one($sql){
   
        $this->query($sql);
        return  sqlsrv_fetch_array($this->query_id, SQLSRV_FETCH_ASSOC);
   
    }
    // $DB->count(select   count(*)   from  users)
    function count($sql){
   
        $count=$this->fetch_one($sql);
        return $count[""];
   
    }
   
    function affectedRows() {
        return ($this->query_id) ? @sqlsrv_num_rows($this->query_id) : false;
    }
}
   
?>
 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:PHP获取两个时间相差的年数、月数和天数

下一篇:通过php的ftp函数上传文件