ecshop适应在PHP7的修改方法解决报错的实现


Posted in PHP onNovember 01, 2016

ecshop这个系统,到目前也没见怎么推出新版本,如果是新项目,不太建议使用它。不过,因为我一直以来都在使用中,所以不得不更改让其适应PHP新版本。现在PHP 7已经出发行版了,所以更改来继续使用吧。具体的更改有以下方面:

(1)将mysql扩展的使用替换掉,改为使用mysqli或pdo:

从php5.5开始,mysql扩展将废弃了。

具体更改的文件在于includes/cls_mysql.php。这是个不小的工程,文件代码太长……

if (!defined('DITAN_ECS'))
{
  die('Hacking attempt');
}

class cls_mysql
{
  var $link_id  = NULL;

  var $settings  = array();

  var $queryCount = 0;
  var $queryTime = '';
  var $queryLog  = array();

  var $max_cache_time = 300; // 最大的缓存时间,以秒为单位

  var $cache_data_dir = 'temp/query_caches/';
  var $root_path   = '';

  var $error_message = array();
  var $platform    = '';
  var $version    = '';
  var $dbhash     = '';
  var $starttime   = 0;
  var $timeline    = 0;
  var $timezone    = 0;
  // 事务指令数
  protected $transTimes = 0;

  var $mysql_config_cache_file_time = 0;

  var $mysql_disable_cache_tables = array(); // 不允许被缓存的表,遇到将不会进行缓存

  function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $charset = 'gbk', $pconnect = 0, $quiet = 0)
  {
    $this->cls_mysql($dbhost, $dbuser, $dbpw, $dbname, $charset, $pconnect, $quiet);
  }

  function cls_mysql($dbhost, $dbuser, $dbpw, $dbname = '', $charset = 'gbk', $pconnect = 0, $quiet = 0)
  {
    if (defined('EC_CHARSET'))
    {
      $charset = strtolower(str_replace('-', '', EC_CHARSET));
    }

    if (defined('ROOT_PATH') && !$this->root_path)
    {
      $this->root_path = ROOT_PATH;
    }

    if ($quiet)
    {
      $this->connect($dbhost, $dbuser, $dbpw, $dbname, $charset, $pconnect, $quiet);
    }
    else
    {
      $this->settings = array(
                  'dbhost'  => $dbhost,
                  'dbuser'  => $dbuser,
                  'dbpw'   => $dbpw,
                  'dbname'  => $dbname,
                  'charset' => $charset,
                  'pconnect' => $pconnect
                  );
    }
  }

  function connect($dbhost, $dbuser, $dbpw, $dbname = '', $charset = 'utf8', $pconnect = 0, $quiet = 0)
  {
    if ($pconnect)
    {
      $this->link_id = new mysqli('p:'.$dbhost, $dbuser, $dbpw);
      if ($this->link_id->connect_error)
      {
        if (!$quiet)
        {
          $this->ErrorMsg("Can't pConnect MySQL Server($dbhost)!");
        }

        return false;
      }
    }
    else
    {
      $this->link_id = new mysqli($dbhost, $dbuser, $dbpw);

      if ($this->link_id->connect_error)
      {
        if (!$quiet)
        {
          $this->ErrorMsg("Can't Connect MySQL Server($dbhost)!");
        }

        return false;
      }
    }

    $this->dbhash = md5($this->root_path . $dbhost . $dbuser . $dbpw . $dbname);
    $this->version = $this->link_id->server_version;

    /* 对字符集进行初始化 */
    $this->link_id->set_charset($charset);
    
    $this->link_id->query("SET sql_mode=''");
    $sqlcache_config_file = $this->root_path . $this->cache_data_dir . 'sqlcache_config_file_' . $this->dbhash . '.php';

    @include($sqlcache_config_file);

    $this->starttime = time();

    if ($this->max_cache_time && $this->starttime > $this->mysql_config_cache_file_time + $this->max_cache_time)
    {
      if ($dbhost != '.')
      {
        $result = $this->link_id->query("SHOW VARIABLES LIKE 'basedir'");
        $row = $result->fetch_array(MYSQLI_ASSOC);
        $result->free();
        if (!empty($row['Value']{1}) && $row['Value']{1} == ':' && !empty($row['Value']{2}) && $row['Value']{2} == "/")
        {
          $this->platform = 'WINDOWS';
        }
        else
        {
          $this->platform = 'OTHER';
        }
      }
      else
      {
        $this->platform = 'WINDOWS';
      }

      if ($this->platform == 'OTHER' &&
        ($dbhost != '.' && strtolower($dbhost) != 'localhost:3306' && $dbhost != '127.0.0.1:3306') ||
        date_default_timezone_get() == 'UTC')
      {
        $result = $this->link_id->query("SELECT UNIX_TIMESTAMP() AS timeline, UNIX_TIMESTAMP('" . date('Y-m-d H:i:s', $this->starttime) . "') AS timezone");
        $row = $result->fetch_array(MYSQLI_ASSOC);
        $result->free();
        if ($dbhost != '.' && strtolower($dbhost) != 'localhost:3306' && $dbhost != '127.0.0.1:3306')
        {
          $this->timeline = $this->starttime - $row['timeline'];
        }
        if (date_default_timezone_get() == 'UTC')
        {
          $this->timezone = $this->starttime - $row['timezone'];
        }
      }

      $content = '<' . "?php\r\n" .
            '$this->mysql_config_cache_file_time = ' . $this->starttime . ";\r\n" .
            '$this->timeline = ' . $this->timeline . ";\r\n" .
            '$this->timezone = ' . $this->timezone . ";\r\n" .
            '$this->platform = ' . "'" . $this->platform . "';\r\n?" . '>';

      @file_put_contents($sqlcache_config_file, $content);
    }

    /* 选择数据库 */
    if ($dbname)
    {
      
      if ($this->link_id->select_db($dbname) === false )
      {
        if (!$quiet)
        {
          $this->ErrorMsg("Can't select MySQL database($dbname)!");
        }

        return false;
      }
      else
      {
        return true;
      }
    }
    else
    {
      return true;
    }
  }

  function select_database($dbname)
  {
    return $this->link_id->select_db($dbname);
  }

  function set_mysql_charset($charset)
  {
    if (in_array(strtolower($charset), array('gbk', 'big5', 'utf-8', 'utf8')))
    {
      $charset = str_replace('-', '', $charset);
    }
    $this->link_id->set_charset($charset);
  }

  function fetch_array($query, $result_type = MYSQLI_ASSOC)
  {
    $row = $query->fetch_array($result_type);
    $query->free();
    return $row;
  }

  function query($sql, $type = '')
  {
    if ($this->link_id === NULL)
    {
      $this->connect($this->settings['dbhost'], $this->settings['dbuser'], $this->settings['dbpw'], $this->settings['dbname'], $this->settings['charset'], $this->settings['pconnect']);
      $this->settings = array();
    }

    if ($this->queryCount++ <= 99)
    {
      $this->queryLog[] = $sql;
    }
    if ($this->queryTime == '')
    {
      if (PHP_VERSION >= '5.0.0')
      {
        $this->queryTime = microtime(true);
      }
      else
      {
        $this->queryTime = microtime();
      }
    }

    /* 当当前的时间大于类初始化时间的时候,自动执行 ping 这个自动重新连接操作 */
    if (time() > $this->starttime + 1)
    {
      $this->link_id->ping();
    }

    if (!($query = $this->link_id->query($sql)) && $type != 'SILENT')
    {
      $this->error_message[]['message'] = 'MySQL Query Error';
      $this->error_message[]['sql'] = $sql;
      $this->error_message[]['error'] = $this->link_id->error;
      $this->error_message[]['errno'] = $this->link_id->errno;

      $this->ErrorMsg();

      return false;
    }

    if (defined('DEBUG_MODE') && (DEBUG_MODE & 8) == 8)
    {
      $logfilename = $this->root_path . DATA_DIR . '/mysql_query_' . $this->dbhash . '_' . date('Y_m_d') . '.log';
      $str = $sql . "\n\n";

      if (PHP_VERSION >= '5.0')
      {
        file_put_contents($logfilename, $str, FILE_APPEND);
      }
      else
      {
        $fp = @fopen($logfilename, 'ab+');
        if ($fp)
        {
          fwrite($fp, $str);
          fclose($fp);
        }
      }
    }

    return $query;
  }

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

  function error()
  {
    return $this->link_id->error;
  }

  function errno()
  {
    return $this->link_id->errno;
  }

  function result($query, $row)
  {
    $query->data_seek($row);
    $result = $query->fetch_row();
    $query->free();
    return $result;
  }

  function num_rows($query)
  {
    return $query->num_rows;
  }

  function num_fields($query)
  {
    return $this->link_id->field_count;
  }

  function free_result($query)
  {
    return $query->free();
  }

  function insert_id()
  {
    return $this->link_id->insert_id;
  }

  function fetchRow($query)
  {
    return $query->fetch_assoc();
  }

  function fetch_fields($query)
  {
    return $query->fetch_field();
  }

  function version()
  {
    return $this->version;
  }

  function ping()
  {
    return $this->link_id->ping();
  }

  function escape_string($unescaped_string)
  {
    return $this->link_id->real_escape_string($unescaped_string);
  }

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

  function ErrorMsg($message = '', $sql = '')
  {
    if ($message)
    {
      echo "<b>DTXB info</b>: $message\n\n<br /><br />";
      //print('<a href="http://faq.comsenz.com/?type=mysql&dberrno=2003&dberror=Can%27t%20connect%20to%20MySQL%20server%20on" target="_blank">http://faq.comsenz.com/</a>');
    }
    else
    {
      echo "<b>MySQL server error report:";
      print_r($this->error_message);
      //echo "<br /><br /><a href='http://faq.comsenz.com/?type=mysql&dberrno=" . $this->error_message[3]['errno'] . "&dberror=" . urlencode($this->error_message[2]['error']) . "' target='_blank'>http://faq.comsenz.com/</a>";
    }

    exit;
  }

/* 仿真 Adodb 函数 */
  function selectLimit($sql, $num, $start = 0)
  {
    if ($start == 0)
    {
      $sql .= ' LIMIT ' . $num;
    }
    else
    {
      $sql .= ' LIMIT ' . $start . ', ' . $num;
    }

    return $this->query($sql);
  }

  function getOne($sql, $limited = false)
  {
    if ($limited == true)
    {
      $sql = trim($sql . ' LIMIT 1');
    }

    $res = $this->query($sql);
    if ($res !== false)
    {
      $row = $res->fetch_row();
      $res->free();
      if ($row !== false)
      {
        return $row[0];
      }
      else
      {
        return '';
      }
    }
    else
    {
      return false;
    }
  }

  function getOneCached($sql, $cached = 'FILEFIRST')
  {
    $sql = trim($sql . ' LIMIT 1');

    $cachefirst = ($cached == 'FILEFIRST' || ($cached == 'MYSQLFIRST' && $this->platform != 'WINDOWS')) && $this->max_cache_time;
    if (!$cachefirst)
    {
      return $this->getOne($sql, true);
    }
    else
    {
      $result = $this->getSqlCacheData($sql, $cached);
      if (empty($result['storecache']) == true)
      {
        return $result['data'];
      }
    }

    $arr = $this->getOne($sql, true);

    if ($arr !== false && $cachefirst)
    {
      $this->setSqlCacheData($result, $arr);
    }

    return $arr;
  }

  function getAll($sql)
  {
    $res = $this->query($sql);
    if ($res !== false)
    {
      $arr = $res->fetch_all(MYSQLI_ASSOC);
      $res->free();
       return $arr;
    }
    else
    {
      return false;
    }
  }

  function getAllCached($sql, $cached = 'FILEFIRST')
  {
    $cachefirst = ($cached == 'FILEFIRST' || ($cached == 'MYSQLFIRST' && $this->platform != 'WINDOWS')) && $this->max_cache_time;
    if (!$cachefirst)
    {
      return $this->getAll($sql);
    }
    else
    {
      $result = $this->getSqlCacheData

以上就是小编为大家带来的ecshop适应在PHP7的修改方法解决报错的实现全部内容了,希望大家多多支持三水点靠木~

PHP 相关文章推荐
php INI配置文件的解析实现分析
Jan 04 PHP
php cli模式学习(PHP命令行模式)
Jun 03 PHP
关于使用key/value数据库redis和TTSERVER的心得体会
Jun 28 PHP
php检测数组长度函数sizeof与count用法
Nov 17 PHP
php实现过滤UBB代码的类
Mar 12 PHP
PHP的Socket网络编程入门指引
Aug 11 PHP
PHP中list()函数用法实例简析
Jan 08 PHP
完美解决在ThinkPHP控制器中命名空间的问题
May 05 PHP
利用PHP获取访客IP、地区位置、浏览器及来源页面等信息
Jun 27 PHP
PHP机器学习库php-ml的简单测试和使用方法
Jul 14 PHP
WHOOPS PHP调试库的使用
Sep 29 PHP
PHP 图片处理
Sep 16 PHP
遍历echsop的region表形成缓存的程序实例代码
Nov 01 #PHP
CI框架无限级分类+递归的实现代码
Nov 01 #PHP
CI框架(ajax分页,全选,反选,不选,批量删除)完整代码详解
Nov 01 #PHP
PHP之十六个魔术方法详细介绍
Nov 01 #PHP
php有效防止图片盗用、盗链的两种方法
Nov 01 #PHP
php 实现一个字符串加密解密的函数实例代码
Nov 01 #PHP
PHP+Ajax异步带进度条上传文件实例
Nov 01 #PHP
You might like
php表单转换textarea换行符的方法
2010/09/10 PHP
php猴子选大王问题解决方法
2015/05/12 PHP
php实现mysql数据库分表分段备份
2015/06/18 PHP
php抽象方法和抽象类实例分析
2016/12/07 PHP
浅谈使用 Yii2 AssetBundle 中 $publishOptions 的正确姿势
2017/11/08 PHP
阻止JavaScript事件冒泡传递(cancelBubble 、stopPropagation)
2007/05/08 Javascript
一款JavaScript压缩工具:X2JSCompactor
2007/06/13 Javascript
Draggable Elements 元素拖拽功能实现代码
2011/03/30 Javascript
js获取客户端外网ip的简单实例
2013/11/21 Javascript
Jquery插件easyUi表单验证提交(示例代码)
2013/12/30 Javascript
javascript中数组的多种定义方法和常用函数简介
2014/05/09 Javascript
jQuery验证插件validate使用详解
2016/05/11 Javascript
详解JavaScript中this关键字的用法
2016/05/26 Javascript
JavaScript中的E-mail 地址格式验证
2018/03/28 Javascript
微信小程序使用map组件实现解析经纬度功能示例
2019/01/22 Javascript
Vue表单绑定的实例代码(单选按钮,选择框(单选时,多选时,用 v-for 渲染的动态选项)
2019/05/13 Javascript
VUE 自定义组件模板的方法详解
2019/08/30 Javascript
JavaScript字符串处理常见操作方法小结
2019/11/15 Javascript
解决idea开发遇到javascript动态添加html元素时中文乱码的问题
2020/09/29 Javascript
[01:05:12]2014 DOTA2国际邀请赛中国区预选赛 TongFu VS CIS-GAME
2014/05/21 DOTA
Python变量和数据类型详解
2017/02/15 Python
详解TensorFlow在windows上安装与简单示例
2018/03/05 Python
Python 中字符串拼接的多种方法
2018/07/30 Python
python 内置模块详解
2019/01/01 Python
Python中出现IndentationError:unindent does not match any outer indentation level错误的解决方法
2020/04/18 Python
基于python的selenium两种文件上传操作实现详解
2019/09/19 Python
Python实现图片批量加入水印代码实例
2019/11/30 Python
python的sys.path模块路径添加方式
2020/03/09 Python
django form和field具体方法和属性说明
2020/07/09 Python
如何通过python实现IOU计算代码实例
2020/11/02 Python
HTML5使用ApplicationCache接口实现离线缓存技术解决离线难题
2012/12/13 HTML / CSS
HTML5实现无刷新修改URL的方法
2019/11/14 HTML / CSS
协议书模板
2014/04/23 职场文书
2015年企业团支部工作总结
2015/05/21 职场文书
男方家长婚礼答谢词
2015/09/29 职场文书
Python 阶乘详解
2021/10/05 Python