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

1.源码
<?php
/**
 * @desc   A simple and convenient php sqlsrv  class
 * @author Yaseng  WwW.Yaseng.Me [Yaseng@UAUC.NET]
 * @link   http://yaseng.me/sqlsrv-class.html
 */
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;
    }
}
  
?>

2.测试

<?php
  
include "sqlsrv.class.php";
  
$sql=new  sqlsrv("192.168.1.109","sa","xxx","test");
  
$strsql="select @@version";
echo "<pre><h2>sqlsrv test</h2>";
echo "<h3> rows:</h3>";
var_dump($sql->count($strsql));
echo "<h3> sql:</h3>";
print_r($sql->sql_log);
echo "<h3> errors:</h3>";
print_r($sql->error_log);
echo "<h3> sqlsrv:</h3>";
print_r($sql);
  
?>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值