返回顶部

收藏

sqlsrv.class.php 一个 php 连接 sql server的类

更多
<?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;
    }
}

?>
//该片段来自于http://outofmemory.cn

标签:php,数据库

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2014-07-10 19:23:33MySQL的备份 by jellyvice
  2. 2014-08-09 20:50:13php取出mysql数据库中所有数据 by Flio
  3. 2014-08-20 12:55:41PHP打开sqlite的两种方式 by 梁方
  4. 2015-03-24 14:18:30Quentin Zervaas's DBO by jellyvice
  5. 2014-03-24 16:38:31php链接到mysql的简单示例 by fengsweat
  6. 2014-11-30 12:58:18操作MySQL数据库的php类 by 傅小黑
  7. 2014-07-25 14:52:01判断表是否存在 by Linland
  8. 2014-11-09 21:22:44MysqPdo封装 by 南宫傲
  9. 2014-07-19 21:43:59关于mybb主从数据配置 by Linland
  10. 2014-08-20 21:51:21PHP 使用 ODBC 连接数据库 by jellyvice
  11. 2014-12-03 19:52:05批量操作数据库表 by lihuibng

发表评论