Mssql数据库驱动类,兼容各大版本

代码如下:

<?php
/**
 * Author Albert Zhan
 * Author Email: 2654709623@qq.com
 * Author URL: http://www.5lazy.cn
 * Date 20160128
 */
//Mssql数据库操作路由
class Mssql{

    protected $link_id;

    public function __construct()
    {
        $array=array(
            "UID"=>DB_USER,
            "PWD"=>DB_PASSWD,
            "Database"=>DB_NAME,
        );
        $this->link_id=sqlsrv_connect(DB_HOST,$array);
        if($this->link_id==false)
        {
            die( print_r( sqlsrv_errors(), true));
        }
    }

    /**
     * 执行语句
     * @access public
     * @param string $sql 数据语句
     * @return mixed
     */
    public function query($sql) {
        $query = sqlsrv_query($this->link_id,$sql);
        if($query==false)
        {
            return false;
        }
        else
        {
            return $query;
        }
    }

    /**
     * 获取一条记录
     * @access public
     * @param string $sql 数据语句
     * @return mixed
     */
    public function get_one($sql) {
        $query=$this->query($sql);
        return sqlsrv_fetch_array($query);
    }

    /**
     * 获取全部记录
     * @access public
     * @param string $sql 数据语句
     * @return mixed
     */
    public function select($sql)
    {
        $query=$this->query($sql);
        $i = 0;
        $rt = array();
        while ($row =& sqlsrv_fetch_array($query)) {
            $rt[$i] = $row;
            $i++;
        }
        return $rt;
    }
    /**
     * 插入语句
     * @access public
     * @param string $table 数据表
     * @param array $dataArray 插入数据
     * @return boolean
     */
    public function insert($table,$dataArray) {
        $field = "";
        $value = "";
        if( !is_array($dataArray) || count($dataArray)<=0) {
            return false;
        }
        while(list($key,$val)=each($dataArray)) {
            $field .="$key,";
            $value .="'$val',";
        }
        $field = substr( $field,0,-1);
        $value = substr( $value,0,-1);
        $sql = "insert into $table($field) values($value)";
        if(!$this->query($sql))
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    /**
     * 更新语句
     * @access public
     * @param string $table 数据表
     * @param array $dataArray 插入数据
     * @param string $condition 更新条件
     * @return boolean
     */
    public function update( $table,$dataArray,$condition="") {
        if( !is_array($dataArray) || count($dataArray)<=0) {
            return false;
        }
        $value = "";
        while( list($key,$val) = each($dataArray)) {
            $value .= "$key = '$val',";
        }
        $value .= substr( $value,0,-1);
        $sql = "update $table set $value where 1=1 and $condition";
        if(!$this->query($sql))
        {
            return false;
        }
        else {
            return true;
        }
    }

    /**
     * 删除语句
     * @access public
     * @param string $table 数据表
     * @param string $condition 删除条件
     * @return boolean
     */
    public function delete( $table,$condition="") {
        if( empty($condition) ) {
            return false;
        }
        $sql = "delete from $table where 1=1 and $condition";
        if(!$this->query($sql))
        {
            return false;
        }
        else {
            return true;
        }
    }

    /**
     * 获取记录条数
     * @access public
     * @param string $sql 查询语句
     * @return mixed
     */
    public function num_rows($sql) {
        $query=$this->query($sql);
        $num=sqlsrv_num_rows($query);
        return count($num);
    }

    //获取数据库版本号
    public function version()
    {
        $version=sqlsrv_server_info($this->link_id);
        return $version['SQLServerVersion'];
    }

    //析构方法
    public function __destruct() {
       sqlsrv_close($this->link_id);
    }
}
代码下载:Mssql.class.zip

Comments : 0

有问题可在下面发表评论,当然没事也可以在下面吹吹牛皮、扯扯淡!

发表评论

*