赞
踩
简单粗暴 ⛽️
// +---------------------------------------------------------------------- // | Author: 老掌 // +---------------------------------------------------------------------- // | 数据库操作类 // +---------------------------------------------------------------------- /** * 数据库操作类 * 数据库操作封装 * @author wangweichen */ /* connect($dbhost, $dbuser, $dbpw, $dbname = '', $charset) select_database('user') set_mysql_charset('utf8') fetch_array($res) query($sql) affected_rows() error() errno() result($res,0) num_rows($res) num_fields($res) free_result($res) insert_id() fetchRow($res) fetch_fields($res) version() ping() escape_string() close() error_msg('提示信息') selectlimit($sql,$num,$start) getone($sql,true) getrow($sql) getall($sql) */ // namespace Org\Util; class Mysqli{ var $link_id = NULL; var $settings = array(); var $queryCount = 0; var $queryTime = ''; var $queryLog = array(); var $max_cache_time = 0; // 最大的缓存时间,以秒为单位 var $cache_data_dir = 'temp/query_caches/'; //缓存位置 var $root_path = ''; var $error_message = array(); //报错信息 var $platform = ''; //操作系统 var $version = ''; //PHP版本号 var $dbhash = ''; var $starttime = 0; //连接时间 var $timeline = 0; var $timezone = 0; var $mysql_config_cache_file_time = 0; /* * 构造函数 * $dbhost : 主机地址 * $dbuser :用户名 * $dbpw :密码 * $dbname :数据库名 * $charset :编码 * $pconnect * $quiet : */ 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) { 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); } 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; } } $this->dbhash = md5($this->root_path . $dbhost . $dbuser . $dbpw . $dbname); $this->version = mysql_get_server_info($this->link_id); /* 如果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'; } } 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) { 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); } } } /* * 从结果集中取得一行数字或关联数组 * $query :结果集 * $result_type :返回哪种结果 MYSQL_ASSOC 关联数组 MYSQL_NUM 数字数组 MYSQL_BOTH 两者(默认) */ function fetch_array($query, $result_type = MYSQL_BOTH) { return mysql_fetch_array($query, $result_type); } /* * 执行一条SQL语句 * $sql :将要执行的 sql 语句 */ 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 (PHP_VERSION >= '4.3' && time() > $this->starttime + 1) { mysql_ping($this->link_id); } if (!($query = mysql_query($sql, $this->link_id)) && $type != 'SILENT') { $this->error_message[]['message'] = 'MySQL Query Error'; $this->error_message[]['sql'] = $sql; $this->error_message[]['error'] = mysql_error($this->link_id); $this->error_message[]['errno'] = mysql_errno($this->link_id); $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; } /* * 返回前一次 MySQL 操作所影响的记录行数。 */ function affected_rows() { return mysql_affected_rows($this->link_id); } /* * 返回上一个 SQL 操作产生的文本错误信息 */ function error() { return mysql_error($this->link_id); } /* * 返回上一个 SQL 操作产生的错误信息的数字编码 */ function errno() { return mysql_errno($this->link_id); } /* * 返回结果集中一个字段的值 * $query :结果集 * $row :行号。默认从0行开始 * $field :获取哪个字段。默认指定行第一个字段 */ function result($query, $row ,$field) { return @mysql_result($query, $row, $field); } /* * 返回结果集中行的数目 * $query :结果集 */ function num_rows($query) { return mysql_num_rows($query); } /* * 返回结果集中字段的数目 * $query :结果集 */ function num_fields($query) { return mysql_num_fields($query); } /* * 释放结果内存 * $query :结果集 */ function free_result($query){ return mysql_free_result($query); } /* * 返回上一个 SQL 操作产生的ID */ function insert_id() { return mysql_insert_id($this->link_id); } /* * 从结果集中取得一行做为关联数组返回 * $query :结果集 */ function fetchRow($query) { return mysql_fetch_assoc($query); } /* * 从结果集中取得列信息做为对象返回 */ function fetch_fields($query) { return mysql_fetch_field($query); } /* * 返回 PHP 版本号 */ function version() { return $this->version; } /* * 用于空闲很久的脚本来检查服务器是否关闭了连接 */ function ping() { if (PHP_VERSION >= '4.3') { return mysql_ping($this->link_id); } else { return false; } } /* * 转义 SQL 语句中使用的字符串中的特殊字符 */ function escape_string($unescaped_string){ if (PHP_VERSION >= '4.3') { return mysql_real_escape_string($unescaped_string); } else { return mysql_escape_string($unescaped_string); } } /* * 关闭连接 */ function close() { return mysql_close($this->link_id); } /* * 输出错误信息 * $message :提示信息 */ function error_msg($message = ''){ if($message){ echo "<b>出错了~</b> $message\n\n<br /><br />"; }else{ echo "<b>MySQL server error report:</b>"; print_r($this->error_message); } exit; } /* * 拼接limit * $sql :sql语句 * $num :数量 * $start :起始位置 */ function selectlimit($sql, $num ,$start = 0){ if($start){ $sql .= ' LIMIT ' . $start; }else{ $sql .= ' LIMIT ' . $start . ',' . $num; } } /* * 返回结果集中的第一条数据,一般用于查询某条件的某个字段值 * ps:select uid from user where username = '三';结果直接是 uid值 * $sql :要执行的 SQL 语句 * $limited :是否查询一条 */ function getone($sql, $limited = false) { if ($limited == true) { $sql = trim($sql . ' LIMIT 1'); } $res = $this->query($sql); if ($res !== false) { $row = mysql_fetch_row($res); if ($row !== false) { return $row[0]; } else { return ''; } } else { return false; } } /* * 从结果集中取出一行做为关联数组 */ function getrow($sql, $limited = false) { if ($limited == true) { $sql = trim($sql . ' LIMIT 1'); } $res = $this->query($sql); if ($res !== false) { return mysql_fetch_assoc($res); } else { return false; } } /* * 返回结果集所有内容 */ function getall($sql) { $res = $this->query($sql); if ($res !== false) { $arr = array(); while ($row = mysql_fetch_assoc($res)) { $arr[] = $row; } return $arr; } else { return false; } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。