加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码网 (https://www.900php.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2016-11-27 06:47:52 所属栏目:大数据 来源:站长网
导读:ecshop这个系统,到目前也没见怎么推出新版本,如果是新项目,不太建议使用它。不过,因为我一直以来都在使用中,所以不得不更改让其适应PHP新版本。现在PHP 7已经出发行版了,所以更改来继续使用吧。具体的更改有以下方面: (1)将mysql扩展的使用替换掉

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-gt;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-gt;root_path)
    {
      $this-gt;root_path = ROOT_PATH;
    }

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

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

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

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

        return false;
      }
    }

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

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

    @include($sqlcache_config_file);

    $this-gt;starttime = time();

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

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

      $content = 'lt;' . "#63;phprn" .
            '$this-gt;mysql_config_cache_file_time = ' . $this-gt;starttime . ";rn" .
            '$this-gt;timeline = ' . $this-gt;timeline . ";rn" .
            '$this-gt;timezone = ' . $this-gt;timezone . ";rn" .
            '$this-gt;platform = ' . "'" . $this-gt;platform . "';rn#63;" . 'gt;';

      @file_put_contents($sqlcache_config_file, $content);
    }

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

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

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

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

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

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

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

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

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

      $this-gt;ErrorMsg();

      return false;
    }

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

      if (PHP_VERSION gt;= '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-gt;link_id-gt;affected_rows;
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

  function ErrorMsg($message = '', $sql = '')
  {
    if ($message)
    {
      echo "lt;bgt;DTXB infolt;/bgt;: $messagennlt;br /gt;lt;br /gt;";
      //print('lt;a href="http://faq.comsenz.com/#63;type=mysqldberrno=2003dberror=Can%27t%20connect%20to%20MySQL%20server%20on" target="_blank"gt;http://faq.comsenz.com/lt;/agt;');
    }
    else
    {
      echo "lt;bgt;MySQL server error report:";
      print_r($this-gt;error_message);
      //echo "lt;br /gt;lt;br /gt;lt;a href='http://faq.comsenz.com/#63;type=mysqldberrno=" . $this-gt;error_message[3]['errno'] . "dberror=" . urlencode($this-gt;error_message[2]['error']) . "' target='_blank'gt;http://faq.comsenz.com/lt;/agt;";
    }

    exit;
  }

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

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

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

    $res = $this-gt;query($sql);
    if ($res !== false)
    {
      $row = $res-gt;fetch_row();
      $res-gt;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-gt;platform != 'WINDOWS'))  $this-gt;max_cache_time;
    if (!$cachefirst)
    {
      return $this-gt;getOne($sql, true);
    }
    else
    {
      $result = $this-gt;getSqlCacheData($sql, $cached);
      if (empty($result['storecache']) == true)
      {
        return $result['data'];
      }
    }

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

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

    return $arr;
  }

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

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

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

(编辑:源码网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读