• DiscuzX的mysql类


    class db_mysql
    {
        var $tablepre;
        var $version = '';
        var $querynum = 0;
        var $curlink;
        var $link = array();
        var $config = array();
        var $sqldebug = array();
        var $map = array();

        function db_mysql($config = array()) {
            if(!empty($config)) {
                $this->set_config($config);
            }
        }

        function set_config($config) {
            $this->config = &$config;
            $this->tablepre = $config['1']['tablepre'];
            if(!empty($this->config['map'])) {
                $this->map = $this->config['map'];
            }
        }

        function connect($serverid = 1) {

            if(empty($this->config) || empty($this->config[$serverid])) {
                $this->halt('config_db_not_found');
            }

            $this->link[$serverid] = $this->_dbconnect(
                $this->config[$serverid]['dbhost'],
                $this->config[$serverid]['dbuser'],
                $this->config[$serverid]['dbpw'],
                $this->config[$serverid]['dbcharset'],
                $this->config[$serverid]['dbname'],
                $this->config[$serverid]['pconnect']
                );
            $this->curlink = $this->link[$serverid];

        }

        function _dbconnect($dbhost, $dbuser, $dbpw, $dbcharset, $dbname, $pconnect) {
            $link = null;
            $func = empty($pconnect) ? 'mysql_connect' : 'mysql_pconnect';
            if(!$link = @$func($dbhost, $dbuser, $dbpw, 1)) {
                $this->halt('notconnect');
            } else {
                $this->curlink = $link;
                if($this->version() > '4.1') {
                    $dbcharset = $dbcharset ? $dbcharset : $this->config[1]['dbcharset'];
                    $serverset = $dbcharset ? 'character_set_connection='.$dbcharset.', character_set_results='.$dbcharset.', character_set_client=binary' : '';
                    $serverset .= $this->version() > '5.0.1' ? ((empty($serverset) ? '' : ',').'sql_mode=\'\'') : '';
                    $serverset && mysql_query("SET $serverset", $link);
                }
                $dbname && @mysql_select_db($dbname, $link);
            }
            return $link;
        }

        function table_name($tablename) {
            if(!empty($this->map) && !empty($this->map[$tablename])) {
                $id = $this->map[$tablename];
                if(!$this->link[$id]) {
                    $this->connect($id);
                }
                $this->curlink = $this->link[$id];
            } else {
                $this->curlink = $this->link[1];
            }
            return $this->tablepre.$tablename;
        }

        function select_db($dbname) {
            return mysql_select_db($dbname, $this->curlink);
        }

        function fetch_array($query, $result_type = MYSQL_ASSOC) {
            return mysql_fetch_array($query, $result_type);
        }

        function fetch_first($sql) {
            return $this->fetch_array($this->query($sql));
        }

        function result_first($sql) {
            return $this->result($this->query($sql), 0);
        }

        function query($sql, $type = '') {

            if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {
                $starttime = dmicrotime();
            }
            $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
            'mysql_unbuffered_query' : 'mysql_query';
            if(!($query = $func($sql, $this->curlink))) {
                if(in_array($this->errno(), array(2006, 2013)) && substr($type, 0, 5) != 'RETRY') {
                    $this->connect();
                    return $this->query($sql, 'RETRY'.$type);
                }
                if($type != 'SILENT' && substr($type, 5) != 'SILENT') {
                    $this->halt('query_error', $sql);
                }
            }

            if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {
                $this->sqldebug[] = array($sql, number_format((dmicrotime() - $starttime), 6), debug_backtrace());
            }

            $this->querynum++;
            return $query;
        }

        function affected_rows() {
            return mysql_affected_rows($this->curlink);
        }

        function error() {
            return (($this->curlink) ? mysql_error($this->curlink) : mysql_error());
        }

        function errno() {
            return intval(($this->curlink) ? mysql_errno($this->curlink) : mysql_errno());
        }

        function result($query, $row = 0) {
            $query = @mysql_result($query, $row);
            return $query;
        }

        function num_rows($query) {
            $query = mysql_num_rows($query);
            return $query;
        }

        function num_fields($query) {
            return mysql_num_fields($query);
        }

        function free_result($query) {
            return mysql_free_result($query);
        }

        function insert_id() {
            return ($id = mysql_insert_id($this->curlink)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
        }

        function fetch_row($query) {
            $query = mysql_fetch_row($query);
            return $query;
        }

        function fetch_fields($query) {
            return mysql_fetch_field($query);
        }

        function version() {
            if(empty($this->version)) {
                $this->version = mysql_get_server_info($this->curlink);
            }
            return $this->version;
        }

        function close() {
            return mysql_close($this->curlink);
        }

        function halt($message = '', $sql = '') {
            require_once libfile('class/error');
            discuz_error::db_error($message, $sql);
        }

    }

  • 相关阅读:
    升级salt导致进程kill问题记录
    记一次centos6升级salt-minion启动失败的问题
    3.ElasticSearch的倒排索引
    4.ElasticSearch的基本api操作
    1.ElasticSearch介绍及基本概念
    10.openldap备份与恢复
    1.Python3.6环境部署
    8.openldap mirrormode(主主同步)
    7.openldap使用ssl加密认证
    Python json解析
  • 原文地址:https://www.cnblogs.com/yangjian/p/1932543.html
Copyright © 2020-2023  润新知