<?php
class 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;
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('QK_CHARSET'))
{
$charset = strtolower(str_replace('-', '', QK_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
);
/*
for($i=0;$i<count($this->settings);$i++)
{
$arr = each($this->settings);
echo "\$this->settings[\"$arr[key]\"] = $arr[value]<br>";
}
exit;
*/
}
}
function connect($dbhost, $dbuser, $dbpw, $dbname = '', $charset = 'utf8', $pconnect = 0, $quiet = 0)
{
if ($pconnect)
{
if (!($this->link_id = @mysql_pconnect($dbhost, $dbuser, $dbpw)))
{
if (!$quiet)
{
$this->ErrorMsg("Can't pConnect MySQL Server($dbhost)!");
}
return false;
}
}
else
{
if (PHP_VERSION >= '4.2')
{
$this->link_id = @mysql_connect($dbhost, $dbuser, $dbpw, true);
//echo $this->link_id; //Resource id #18
//exit;
}
else
{
$this->link_id = @mysql_connect($dbhost, $dbuser, $dbpw);
mt_srand((double)microtime() * 1000000); // 对 PHP 4.2 以下的版本进行随机数函数的初始化工作
}
if (!$this->link_id)
{
if (!$quiet)
{
$this->ErrorMsg("Can't Connect MySQL Server($dbhost)!");
}
return false;
}
}
//echo $this->root_path;
$this->dbhash = md5($this->root_path . $dbhost . $dbuser . $dbpw . $dbname);
$this->version = mysql_get_server_info($this->link_id);
//echo $this->version; //5.0.22-community-nt
/* 如果mysql 版本是 4.1+ 以上,需要对字符集进行初始化 */
if ($this->version > '4.1')
{
if ($charset != 'latin1')
{
mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary", $this->link_id);
}
if ($this->version > '5.0.1')
{
mysql_query("SET sql_mode=''", $this->link_id);
}
}
$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 = mysql_query("SHOW VARIABLES LIKE 'basedir'", $this->link_id);
$row = mysql_fetch_assoc($result);
if (!empty($row['Value']{1}) && $row['Value']{1} == ':' && !empty($row['Value']{2}) && $row['Value']{2} == "\\")
{
$this->platform = 'WINDOWS';
}
else
{
$this->platform = 'OTHER';
}
//echo $row['Value']; //D:\Program Files\MySQL\MySQL Server 5.0\
//echo $row['Value']{3}; //P
//exit;
}
else
{
$this->platform = 'WINDOWS';
}
if ($this->platform == 'OTHER' &&
($dbhost != '.' && strtolower($dbhost) != 'localhost:3306' && $dbhost != '127.0.0.1:3306') ||
(PHP_VERSION >= '5.1' && date_default_timezone_get() == 'UTC'))
{
$result = mysql_query("SELECT UNIX_TIMESTAMP() AS timeline, UNIX_TIMESTAMP('" . date('Y-m-d H:i:s', $this->starttime) . "') AS timezone", $this->link_id);
$row = mysql_fetch_assoc($result);
if ($dbhost != '.' && strtolower($dbhost) != 'localhost:3306' && $dbhost != '127.0.0.1:3306')
{
$this->timeline = $this->starttime - $row['timeline'];
}
if (PHP_VERSION >= '5.1' && 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)
{
//file_put_contents("t.txt", "高和");
if (mysql_select_db($dbname, $this->link_id) === false )
{
if (!$quiet)
{
$this->ErrorMsg("Can't select MySQL database($dbname)!");
}
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
function select_database($dbname)
{
return mysql_select_db($dbname, $this->link_id);
}
function set_mysql_charset($charset)
{
/* 如果mysql 版本是 4.1+ 以上,需要对字符集进行初始化 */
if ($this->version > '4.1')
{
if (in_array(strtolower($charset), array('gbk', 'big5', 'utf-8', 'utf8')))
{
$charset = str_replace('-', '', $charset);
}
if ($charset != 'latin1')
{
mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary", $this->link_id);
}
}
}
function fetch_array($query, $result_type = MYSQL_ASSOC)
{
return mysql_fetch_array($query, $result_type);
}
function query($sql, $type = '')
{
if ($this->link_id === NULL)
{
$this->connect($this->settings['dbhost'], $this->settings['dbuser'], $this->settings['dbpw'], $this->settings['dbname'], $thi