php mssql server 2005数据库连接类

php中文网
发布: 2016-06-07 17:47:13
原创
1196人浏览过

mssql server 2005连接类
class msDriver{
    public $error;
    public $querynum=0;
    public $link;
    public $lastQuery;
    public $lastInsert_id=0;
    public $error_callback=array('record');    //record,ignore,report,abort
    public $error_report_file=ROOT."/webdata/db.error.log";
    function __construct($server='',$connectionInfo=array()){
        if ($server){
            $this->connect($server,$connectionInfo);
        }
    }
    function __destruct(){
        !$link && $link= &$this->link;
        if ($link) sqlsrv_close( $link );
    }
    function connect($dbhost='(local)', $connectionInfo=array()){
        $this->link=sqlsrv_connect($dbhost,$connectionInfo);   
        if (!$this->link) $this->halt();
    }
    /**
     *查询过程
     *
     * @param string $sql
     * @param resource $link
     * @return resource
     */
    function query($sql="",$link=''){
        !$link && $link= &$this->link;
        );
        $this->lastQuery=&$query;
        if (!$query) $this->halt();
        $reg = "#insert into#";
        if(preg_match($reg,$sql)){
            $res = sqlsrv_query($link,"select @@IDENTITY as id");
            $this->lastInsert_id = sqlsrv_get_field($res,0);
        }
        return $query;
    }
    /**
     * 返回字段总数
     *
     * @param resource $result
     * @return number
     */
    function num_fields($result=''){
        if (!$result) $this->halt();
        return @sqlsrv_num_fields($result);
    }
    /**
     * 返回字段值
     *
     * @param resource $result
     * @param Int $fieldIndex
     * @return value
     */
    function result($result='',$fieldIndex=0){
        if (!$result) $this->halt();
        return sqlsrv_get_field($result,$fieldIndex);
    }
    /**
     * 返回行数组
     *
     * @param unknown_type $result
     * @return unknown
     */
   function fetch_array($result,$type=SQLSRV_FETCH_ASSOC){           //SQLSRV_FETCH_NUMERIC,SQLSRV_FETCH_ASSOC,SQLSRV_FETCH_BOTH
        !$result && $result=$this->lastQuery;
        if (!$result) $this->halt();
        );
        return is_array($r)?$r:0;
    }
    /**
     * 返回影响的记录数
     *
     * @param resource $result
     * @return number
     */
    function affected_rows($result) {
        if (!$result) $this->halt();
        return sqlsrv_rows_affected($result);
    }
    function insert_id(){
        return $this->lastInsert_id;
    }
    function freeResult($result){
        !$result && $result=$this->lastQuery;
        sqlsrv_free_stmt($result);
    }
    /**
     * 错误的处理方式
     *
     */
    function halt(){
        $err=$this->getEroor();
        //var_dump($err['']);
        if (in_array('record',$this->error_callback)){
            $fp=fopen($this->error_report_file,'a+');
            if ($fp){
               fwrite($fp,"[".date("Y//m/dH:i:s")."](".$err['code'].")".$err['message'].",URL:".$_SERVER["REQUEST_URI"]."?".$_SERVER["QUERY_STRING"]."rn");
                fclose($fp);
            }
        }
        if (in_array('report',$this->error_callback)){
            echo "
错误:[".date("Y//m/d H:i:s")."](".$err['code'].")".$err['message'];
        }
        if (in_array('abort',$this->error_callback)){
            exit;
        }
    }
    /**
     * 返回查询错误
     *
     * @return array
     */
    function getEroor(){
        $err=sqlsrv_errors();
        //return array('code'=>$err['code'],'message'=>$err['message']);
        return is_array($err)?$err[0]:array();
    }
   
}

相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号