首页 >php教程 >PHP源码 >sqlsrv.class.php 一个 php 连接 sql server的类

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

PHP中文网
PHP中文网原创
2016-05-25 17:00:471457浏览

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

  1. sqlsrv.class.php   

<?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(&#39;UID&#39; => $user ,&#39;PWD&#39;=> $pass, &#39;Database&#39; => $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. [代码]测试  

count($strsql));
echo "sql:";
print_r($sql->sql_log);
echo "errors:";
print_r($sql->error_log);
echo "sqlsrv:";
print_r($sql);
  
?>


3. [图片] 4FC8ADB91A2746089187E5DD909222E2.jpgsqlsrv.class.php  一个 php 连接 sql server的类    

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

                   

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn