找回密码
 立即注册

QQ登录

只需一步,快速开始

Discuz!X2-X2.5 Mysqli扩展支持案例

mysql和Mysqli扩展区别说明:
mysql是非持继连接函数而mysqli是永远连接函数。
也就是说
mysql每次链接都会打开一个连接的进程而mysqli多次运行mysqli将使用同一连接进程,从而减少了服务器的开销

适用版本:Discuz!x2-x2.5
Discuz!x2使用方法
将以下代码粘贴至source/class/class_core.php中的
  1. class db_mysqli
  2. {
  3.         var $tablepre;
  4.         var $version = '';
  5.         var $querynum = 0;
  6.         var $slaveid = 0;
  7.         var $curlink;
  8.         var $link = array();
  9.         var $config = array();
  10.         var $sqldebug = array();
  11.         var $map = array();

  12.         function db_mysqli($config = array()) {
  13.                 if(!function_exists('mysqli_connect')) exit('mysqli extension not found!');
  14.                 if(!empty($config)) {
  15.                         $this->set_config($config);
  16.                 }
  17.         }

  18.         function set_config($config) {
  19.                 $this->config = &$config;
  20.                 $this->tablepre = $config['1']['tablepre'];
  21.                 if(!empty($this->config['map'])) {
  22.                         $this->map = $this->config['map'];
  23.                 }
  24.         }

  25.         function connect($serverid = 1) {

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

  29.                 $this->link[$serverid] = $this->_dbconnect(
  30.                         $this->config[$serverid]['dbhost'],
  31.                         $this->config[$serverid]['dbuser'],
  32.                         $this->config[$serverid]['dbpw'],
  33.                         $this->config[$serverid]['dbcharset'],
  34.                         $this->config[$serverid]['dbname'],
  35.                         $this->config[$serverid]['pconnect']
  36.                         );
  37.                 $this->curlink = $this->link[$serverid];

  38.         }

  39.         function _dbconnect($dbhost, $dbuser, $dbpw, $dbcharset, $dbname, $pconnect) {
  40.                 $link = null;
  41.                 if(!$link = new mysqli($dbhost, $dbuser, $dbpw, $dbname)) {
  42.                         $this->halt('notconnect');
  43.                 } else {
  44.                         $this->curlink = $link;
  45.                         if($this->version() > '4.1') {
  46.                                 $dbcharset = $dbcharset ? $dbcharset : $this->config[1]['dbcharset'];
  47.                                 $serverset = $dbcharset ? 'character_set_connection='.$dbcharset.', character_set_results='.$dbcharset.', character_set_client=binary' : '';
  48.                                 $serverset .= $this->version() > '5.0.1' ? ((empty($serverset) ? '' : ',').'sql_mode=\'\'') : '';
  49.                                 $serverset && $link->query("SET $serverset");
  50.                         }
  51.                 }
  52.                 return $link;
  53.         }

  54.         function table_name($tablename) {
  55.                 if(!empty($this->map) && !empty($this->map[$tablename])) {
  56.                         $id = $this->map[$tablename];
  57.                         if(!$this->link[$id]) {
  58.                                 $this->connect($id);
  59.                         }
  60.                         $this->curlink = $this->link[$id];
  61.                 } else {
  62.                         $this->curlink = $this->link[1];
  63.                 }
  64.                 return $this->tablepre.$tablename;
  65.         }

  66.         function select_db($dbname) {
  67.                 return $this->curlink->select_db($dbname);
  68.         }

  69.         function fetch_array($query, $result_type = MYSQLI_ASSOC) {
  70.                 return $query->fetch_array($result_type);
  71.         }

  72.         function fetch_first($sql) {
  73.                 return $this->fetch_array($this->query($sql));
  74.         }

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

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

  79.                 if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {
  80.                         $starttime = dmicrotime();
  81.                 }
  82.                $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query';
  83.                 if(!($query = $this->curlink->query($sql))) {
  84.                         if(in_array($this->errno(), array(2006, 2013)) && substr($type, 0, 5) != 'RETRY') {
  85.                                 $this->connect();
  86.                                 return $this->query($sql, 'RETRY'.$type);
  87.                         }
  88.                         if($type != 'SILENT' && substr($type, 5) != 'SILENT') {
  89.                                 $this->halt('query_error', $sql);
  90.                         }
  91.                 }

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

  95.                 $this->querynum++;
  96.                 return $query;
  97.         }

  98.         function affected_rows() {
  99.                 return $this->curlink->affected_rows;
  100.         }

  101.         function error() {
  102.                 return $this->curlink->error;
  103.         }

  104.         function errno() {
  105.                 return intval($this->curlink->errno);
  106.         }

  107.         function result($query, $row = 0) {
  108.                 $query->data_seek($row);
  109.                 list($query) = $query->fetch_row();
  110.                 return $query;
  111.         }

  112.         function num_rows($query) {
  113.                 $query = $query->num_rows;
  114.                 return $query;
  115.         }

  116.         function num_fields($query) {
  117.                 return $query->field_count;
  118.         }

  119.         function free_result($query) {
  120.                 return $query->free_result();
  121.         }

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

  125.         function fetch_row($query) {
  126.                 $query = $query->fetch_row();
  127.                 return $query;
  128.         }

  129.         function fetch_fields($query) {
  130.                 return $query->fetch_field();
  131.         }

  132.         function version() {
  133.                 if(empty($this->version)) {
  134.                         $this->version = $this->curlink->client_version;
  135.                 }
  136.                 return $this->version;
  137.         }

  138.         function close() {
  139.                 return $this->curlink->close();
  140.         }

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

  145. }
复制代码


  1. class db_mysql
复制代码
上面
查找
  1. $class = 'db_mysql';
复制代码

改为
  1. $class = 'db_mysqli';
复制代码

保存就可以了


Discuz!x2.5 使用方法

1.创建文件db_driver_mysqli.php 上传到 网站目录source/class/db/下面
  1. <?php

  2. /**
  3. *      [Discuz!] (C)2001-2099 Comsenz Inc.
  4. *      This is NOT a freeware, use is subject to license terms
  5. *
  6. *      $Id: db_driver_mysqli.php 33333 2013-05-28 09:10:48Z kamichen $
  7. */

  8. if(!defined('IN_DISCUZ')) {
  9.         exit('Access Denied');
  10. }

  11. class db_driver_mysqli
  12. {
  13.         var $tablepre;
  14.         var $version = '';
  15.         var $drivertype = 'mysqli';
  16.         var $querynum = 0;
  17.         var $slaveid = 0;
  18.         var $curlink;
  19.         var $link = array();
  20.         var $config = array();
  21.         var $sqldebug = array();
  22.         var $map = array();

  23.         function db_mysql($config = array()) {
  24.                 if(!empty($config)) {
  25.                         $this->set_config($config);
  26.                 }
  27.         }

  28.         function set_config($config) {
  29.                 $this->config = &$config;
  30.                 $this->tablepre = $config['1']['tablepre'];
  31.                 if(!empty($this->config['map'])) {
  32.                         $this->map = $this->config['map'];
  33.                         for($i = 1; $i <= 100; $i++) {
  34.                                 if(isset($this->map['forum_thread'])) {
  35.                                         $this->map['forum_thread_'.$i] = $this->map['forum_thread'];
  36.                                 }
  37.                                 if(isset($this->map['forum_post'])) {
  38.                                         $this->map['forum_post_'.$i] = $this->map['forum_post'];
  39.                                 }
  40.                                 if(isset($this->map['forum_attachment']) && $i <= 10) {
  41.                                         $this->map['forum_attachment_'.($i-1)] = $this->map['forum_attachment'];
  42.                                 }
  43.                         }
  44.                         if(isset($this->map['common_member'])) {
  45.                                 $this->map['common_member_archive'] =
  46.                                 $this->map['common_member_count'] = $this->map['common_member_count_archive'] =
  47.                                 $this->map['common_member_status'] = $this->map['common_member_status_archive'] =
  48.                                 $this->map['common_member_profile'] = $this->map['common_member_profile_archive'] =
  49.                                 $this->map['common_member_field_forum'] = $this->map['common_member_field_forum_archive'] =
  50.                                 $this->map['common_member_field_home'] = $this->map['common_member_field_home_archive'] =
  51.                                 $this->map['common_member_validate'] = $this->map['common_member_verify'] =
  52.                                 $this->map['common_member_verify_info'] = $this->map['common_member'];
  53.                         }
  54.                 }
  55.         }

  56.         function connect($serverid = 1) {

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

  60.                 $this->link[$serverid] = $this->_dbconnect(
  61.                         $this->config[$serverid]['dbhost'],
  62.                         $this->config[$serverid]['dbuser'],
  63.                         $this->config[$serverid]['dbpw'],
  64.                         $this->config[$serverid]['dbcharset'],
  65.                         $this->config[$serverid]['dbname'],
  66.                         $this->config[$serverid]['pconnect']
  67.                         );
  68.                 $this->curlink = $this->link[$serverid];

  69.         }

  70.         function _dbconnect($dbhost, $dbuser, $dbpw, $dbcharset, $dbname, $pconnect, $halt = true) {

  71.                 $link = new mysqli();
  72.                 if(!$link->real_connect($dbhost, $dbuser, $dbpw, $dbname, null, null, MYSQLI_CLIENT_COMPRESS)) {
  73.                         $halt && $this->halt('notconnect', $this->errno());
  74.                 } else {
  75.                         $this->curlink = $link;
  76.                         if($this->version() > '4.1') {
  77.                                 $link->set_charset($dbcharset ? $dbcharset : $this->config[1]['dbcharset']);
  78.                                 $serverset = $this->version() > '5.0.1' ? 'sql_mode=\'\'' : '';
  79.                                 $serverset && $link->query("SET $serverset");
  80.                         }
  81.                 }
  82.                 return $link;
  83.         }

  84.         function table_name($tablename) {
  85.                 if(!empty($this->map) && !empty($this->map[$tablename])) {
  86.                         $id = $this->map[$tablename];
  87.                         if(!$this->link[$id]) {
  88.                                 $this->connect($id);
  89.                         }
  90.                         $this->curlink = $this->link[$id];
  91.                 } else {
  92.                         $this->curlink = $this->link[1];
  93.                 }
  94.                 return $this->tablepre.$tablename;
  95.         }

  96.         function select_db($dbname) {
  97.                 return $this->curlink->select_db($dbname);
  98.         }

  99.         function fetch_array($query, $result_type = MYSQLI_ASSOC) {
  100.                 if($result_type == 'MYSQL_ASSOC') $result_type = MYSQLI_ASSOC;
  101.                 return $query ? $query->fetch_array($result_type) : null;
  102.         }

  103.         function fetch_first($sql) {
  104.                 return $this->fetch_array($this->query($sql));
  105.         }

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

  109.         public function query($sql, $silent = false, $unbuffered = false) {
  110.                 if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {
  111.                         $starttime = microtime(true);
  112.                 }

  113.                 if('UNBUFFERED' === $silent) {
  114.                         $silent = false;
  115.                         $unbuffered = true;
  116.                 } elseif('SILENT' === $silent) {
  117.                         $silent = true;
  118.                         $unbuffered = false;
  119.                 }

  120.                 $resultmode = $unbuffered ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT;

  121.                 if(!($query = $this->curlink->query($sql, $resultmode))) {
  122.                         if(in_array($this->errno(), array(2006, 2013)) && substr($silent, 0, 5) != 'RETRY') {
  123.                                 $this->connect();
  124.                                 return $this->curlink->query($sql, 'RETRY'.$silent);
  125.                         }
  126.                         if(!$silent) {
  127.                                 $this->halt($this->error(), $this->errno(), $sql);
  128.                         }
  129.                 }

  130.                 if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {
  131.                         $this->sqldebug[] = array($sql, number_format((microtime(true) - $starttime), 6), debug_backtrace(), $this->curlink);
  132.                 }

  133.                 $this->querynum++;
  134.                 return $query;
  135.         }

  136.         function affected_rows() {
  137.                 return $this->curlink->affected_rows;
  138.         }

  139.         function error() {
  140.                 return (($this->curlink) ? $this->curlink->error : mysqli_error());
  141.         }

  142.         function errno() {
  143.                 return intval(($this->curlink) ? $this->curlink->errno : mysqli_errno());
  144.         }

  145.         function result($query, $row = 0) {
  146.                 if(!$query || $query->num_rows == 0) {
  147.                         return null;
  148.                 }
  149.                 $query->data_seek($row);
  150.                 $assocs = $query->fetch_row();
  151.                 return $assocs[0];
  152.         }

  153.         function num_rows($query) {
  154.                 $query = $query ? $query->num_rows : 0;
  155.                 return $query;
  156.         }

  157.         function num_fields($query) {
  158.                 return $query ? $query->field_count : null;
  159.         }

  160.         function free_result($query) {
  161.                 return $query ? $query->free() : false;
  162.         }

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

  166.         function fetch_row($query) {
  167.                 $query = $query ? $query->fetch_row() : null;
  168.                 return $query;
  169.         }

  170.         function fetch_fields($query) {
  171.                 return $query ? $query->fetch_field() : null;
  172.         }

  173.         function version() {
  174.                 if(empty($this->version)) {
  175.                         $this->version = $this->curlink->server_info;
  176.                 }
  177.                 return $this->version;
  178.         }

  179.         function escape_string($str) {
  180.                 return $this->curlink->escape_string($str);
  181.         }

  182.         function close() {
  183.                 return $this->curlink->close();
  184.         }

  185.         function halt($message = '', $code = 0, $sql = '') {
  186.                 throw new DbException($message, $code, $sql);
  187.         }

  188. }

  189. ?>
复制代码
2.修改source\class\discuz\discuz_application.php
查找
  1. $driver = 'db_driver_mysql';
复制代码
修改为
  1. $driver = function_exists('mysql_connect') ? 'db_driver_mysql' : 'db_driver_mysqli';
复制代码
保存,搞定。使用mysqli可能出现的问题(x2.5下)
游客,如果您要查看本帖隐藏内容请回复


本教程有无限星辰工作室独家整理发布,转载请注明出处,谢谢。

本教程由无限星辰工作室CRX349独家整理和提供,转载请注明地址,谢谢。本文地址:https://www.xmspace.net/thread-138-1-1.html
无限星辰工作室  好集导航 Discuz全集下载  星辰站长网  集热爱361  一品文学  手机小游戏合集   海外空间网 星辰api  星辰支付二维码管理平台 阿里云服务器 腾讯云服务器
服务Discuz!建站|DiscuzQ配置|二开|小程序|APP|搬家|挂马清理|防护|Win/Linux环境搭建|优化|运维|
服务理念:专业 诚信 友好QQ842062626 服务项目 Q群315524225

发表于 2014-3-5 11:43:02 | 显示全部楼层 |阅读模式

回复 | 使用道具 举报

该帖共收到 0 条回复!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

美图秀

    • fastadmin 后台界面使用字段数组类型
    • Discuz!x3.5 修改标题高亮颜色
    • Discuz!x3.5 应用中心 下载应用一直下载中
    • 帖子定时显示
    • 论坛辅助审核
拖动客服框
Online Service
点击这里给我发消息
点击这里联系我们
微信扫一扫
在线客服
快速回复 返回顶部 返回列表