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 字符串分割和比较
Oct 06 PHP
批量修改RAR文件注释的php代码
Nov 20 PHP
PHP+Mysql+jQuery实现动态展示信息
Oct 08 PHP
PHP中运用jQuery的Ajax跨域调用实现代码
Feb 21 PHP
比较简单的百度网盘文件直链PHP代码
Mar 24 PHP
PHP根据传入参数合并多个JS和CSS文件的简单实现
Jun 13 PHP
PHP遍历XML文档所有节点的方法
Mar 12 PHP
如何正确配置Nginx + PHP
Jul 15 PHP
PHP基于堆栈实现的高级计算器功能示例
Sep 15 PHP
PHP基于双向链表与排序操作实现的会员排名功能示例
Dec 26 PHP
ThinkPHP3.2.3框架实现执行原生SQL语句的方法示例
Apr 03 PHP
PHP 实现重载
Mar 09 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 获取远程网页内容的代码(fopen,curl已测)
2011/06/06 PHP
ThinkPHP5&amp;5.1框架关联模型分页操作示例
2019/08/03 PHP
php中用unset销毁变量并释放内存
2020/05/10 PHP
javascript 清空form表单中某种元素的值
2009/12/26 Javascript
收集的一些Array及String原型对象的扩展实现代码
2010/12/05 Javascript
jQuery源码分析-02正则表达式 RegExp 常用正则表达式
2011/11/14 Javascript
一些常用弹出窗口/拖放/异步文件上传等实用代码
2013/01/06 Javascript
JavaScript DOM进阶方法
2015/04/13 Javascript
探究JavaScript函数式编程的乐趣
2015/12/14 Javascript
全面理解JavaScript中的继承(必看)
2016/06/16 Javascript
HTML5 JS压缩图片并获取图片BASE64编码上传
2020/11/16 Javascript
jQuery实现在HTML文档加载完毕后自动执行某个事件的方法
2017/05/08 jQuery
详解AngularJS脏检查机制及$timeout的妙用
2017/06/19 Javascript
Vue使用vux-ui自定义表单验证遇到的问题及解决方法
2018/05/10 Javascript
vue + any-touch实现一个iscroll 实现拖拽和滑动动画效果
2019/04/08 Javascript
vue 动态设置img的src地址无效,npm run build 后找不到文件的解决
2020/07/26 Javascript
使用Vue-scroller页面input框不能触发滑动的问题及解决方法
2020/08/08 Javascript
vue使用exif获取图片经纬度的示例代码
2020/12/11 Vue.js
[04:29]2014DOTA2国际邀请赛 主赛事第三日TOPPLAY
2014/07/21 DOTA
从零学python系列之教你如何根据图片生成字符画
2014/05/23 Python
Python中urllib2模块的8个使用细节分享
2015/01/01 Python
python Flask实现restful api service
2017/12/04 Python
python使用xpath中遇到:到底是什么?
2018/01/04 Python
Flask和pyecharts实现动态数据可视化
2020/02/26 Python
Python3使用 GitLab API 进行批量合并分支
2020/10/15 Python
canvas画布实现手写签名效果的示例代码
2019/04/23 HTML / CSS
HTML5 Canvas的事件处理介绍
2015/04/24 HTML / CSS
前端H5 Video常见使用场景简介
2020/08/21 HTML / CSS
护理职业应聘自荐书
2013/09/29 职场文书
会计专业毕业生求职信分享
2014/01/03 职场文书
药学专业学生的自我评价分享
2014/02/06 职场文书
《画杨桃》教学反思
2014/04/13 职场文书
贷款收入证明格式
2015/06/24 职场文书
《蜜蜂引路》教学反思
2016/02/22 职场文书
《海上日出》教学反思
2016/02/23 职场文书
教你怎么用Python处理excel实现自动化办公
2021/04/30 Python