转自:http://hudeyong926.iteye.com/blog/902785
?
<?php 
/** 
* Mysqli类 
* 
* @author 废墟 
* @version v1.0 2009-08-18 
* @link http://anerg.cn/ 
*/ 
class db_mysqli { 
	protected $mysqli; 
	protected $sql; 
	protected $rs; 
	protected $query_num    = 0; 
	protected $fetch_mode    = MYSQLI_ASSOC; 
	protected $cache_dir    = './cache/'; 
	protected $cache_time    = 1800; 
	public function  __construct($dbhost, $dbuser, $dbpass, $dbname) { 
		$this->mysqli    = new mysqli($dbhost, $dbuser, $dbpass, $dbname); 
		if(mysqli_connect_errno()) { 
			$this->mysqli    = false; 
			echo '<h2>'.mysqli_connect_error().'</h2>'; 
			die(); 
		} else { 
			$this->mysqli->set_charset("utf8"); 
		} 
	} 
	public function  __destruct() { 
		$this->free(); 
		$this->close(); 
	} 
	protected function free() { 
		@$this->rs->free(); 
	} 
	protected function close() { 
		$this->mysqli->close(); 
	} 
	protected function fetch() { 
		return $this->rs->fetch_array($this->fetch_mode); 
	} 
	protected function getQuerySql($sql, $limit = null) { 
		if (@preg_match("/[0-9]+(,[ ]?[0-9]+)?/is", $limit) && !preg_match("/ LIMIT [0-9]+(,[ ]?[0-9]+)?$/is", $sql)) { 
			$sql .= " LIMIT " . $limit; 
		} 
		return $sql; 
	} 
	protected function get_cache($sql,$method) { 
		include_once './cache.php';//若框架中使用__autoload(),这里可以不用加载文件 
		$cache    = new cache($this->cache_dir,$this->cache_time); 
		$cache_file    = md5($sql.$method); 
		$res    = $cache->get_cache($cache_file); 
		if(!$res) { 
			$res    = $this->$method($sql); 
			$cache->set_cache($cache_file, $res); 
		} 
		return $res; 
	} 
	public function query_num() { 
		return $this->query_num; 
	} 
	public function set_cache_dir($cache_dir) { 
		$this->cache_dir    = $cache_dir; 
	} 
	public function set_cache_time($cache_time) { 
		$this->cache_time    = $cache_time; 
	} 
	public function query($sql, $limit = null) { 
		$sql    = $this->getQuerySql($sql, $limit); 
		$this->sql    = $sql; 
		$this->rs    = $this->mysqli->query($sql); 
		if (!$this->rs) { 
			echo "<h2>".$this->mysqli->error."</h2>"; 
			die(); 
		} else { 
		$this->query_num++; 
			return $this->rs; 
		} 
	} 
	public function getOne($sql) { 
		$this->query($sql, 1); 
		$this->fetch_mode    = MYSQLI_NUM; 
		$row = $this->fetch(); 
		$this->free(); 
		return $row[0]; 
	} 
	public function get_one($sql) { 
		return $this->getOne($sql); 
	} 
	public function cache_one($sql) { 
		$sql    = $this->getQuerySql($sql, 1); 
		return $this->get_cache($sql, 'getOne'); 
	} 
	public function getRow($sql, $fetch_mode = MYSQLI_ASSOC) { 
		$this->query($sql, 1); 
		$this->fetch_mode    = $fetch_mode; 
		$row = $this->fetch(); 
		$this->free(); 
		return $row; 
	} 
	public function get_row($sql, $fetch_mode = MYSQLI_ASSOC) { 
		return $this->getRow($sql);
	} 
	public function cache_row($sql) { 
		$sql    = $this->getQuerySql($sql, 1); 
		return $this->get_cache($sql, 'getRow'); 
	} 
	public function getAll($sql, $limit = null, $fetch_mode = MYSQLI_ASSOC) { 
		$this->query($sql, $limit); 
		$all_rows = array(); 
		$this->fetch_mode    = $fetch_mode; 
		while($rows = $this->fetch()) { 
			$all_rows[] = $rows; 
		} 
		$this->free(); 
		return $all_rows; 
	} 
	public function get_all($sql, $limit = null, $fetch_mode = MYSQLI_ASSOC) { 
		return $this->getAll($sql); 
	} 
	
	public function cache_all($sql, $limit = null) { 
		$sql    = $this->getQuerySql($sql, $limit); 
		return $this->get_cache($sql, 'getAll'); 
	} 
	public function insert_id() { 
		return $this->mysqli->insert_id(); 
	} 
	public function escape($str) { 
		if(is_array($str)) { 
			foreach($str as $key=>$val) { 
				$str[$key] = $this->escape($val); 
			} 
		} else { 
			$str = addslashes(trim($str)); 
		} 
		return $str; 
	} 
} 
//用法 
$db    = new db_mysqli('localhost', 'root', 111222, 'dict'); 
$db->set_cache_time(10); 
$db->set_cache_dir('./cache/sql/'); 
$sql = "select * from words order by word_id limit 10,10"; 
$res1 = $db->get_all($sql); 
$res2 = $db->cache_all($sql); 
echo $db->query_num(),'<br>'; 
?>
?
mysqli类的对象主要控制PHP和MySQL数据库服务器之间的连接、选择数据库、向MySQL服务器发送SQL语句,以及设置字符集等,这些 任务都是通过该类中声明的构造方法、成员方法和成员属性完成的。在表13-1和表13-2两个表格中,分别列出了mysqli类中声明的成员方法和成员属 性。
表13-1? mysqli类中的成员方法(共33个)
成员方法名
描 ???述
__construct()
构造方法,用于创建一个新的mysqli对象,也
可以建立一个连接
autocommit()
开启或关闭数据库修改自动提交
change_user
改变了数据库连接所指定的用户
character_set_name()
返回数据库连接默认的字符集
close()
关闭先前打开连接
commit()
提交当前的事务
connect()
打开一个新的连接到MySQL数据库服务器
debug()
执行调试操作
dump_debug_info()
转储调试信息
get_client_info()
返回客户端版本
get_host_info()
返回一个字符串代表的连接使用类型,
如:Localhost via UNIX socket
get_server_info()
返回MySQL服务器的版本,如:4.1.2-alpha-debug
get_server_version()
返回整数形式的MySQL服务器版本,如40102
init()
初始化MySQLi并返回一个资源
info()
检索有关最近执行的查询
kill()
要求服务器去杀死一个MySQL线程
续表
成员方法名
描 ???述
multi_query()
执行多个查询语句
more_results()
从多查询语句中检查是否有任何更多的查询结果
Next_result()
从当前执行的多查询中读取下一个结果
options()
设置选项
ping()
如果没有连接,ping一台服务器连接或重新连接
prepare()
准备一个SQL语句的执行,返回mysqli_stmt对象
query()
与数据库的任何交互都是通过查询进行的,
该方法向数据库发送查询来执行
real_connect()
试图打开一个连接到MySQL数据库服务器
escape_string()
转义特殊字符的字符串,用于在一个SQL语句,
并考虑到当前的字符集的连接
rollback()
回滚当前的事务
select_db()
为数据库查询选择默认的数据库
set_charset()
设置默认客户端字符集
ssl_set()
使用SSL用于建立安全连接
stat()
获取当前的系统状态
stmt_init()
初始化一个声明,并返回一个mysqli_stmt对象
store_result()
从最后查询中转让结果集
thread_safe()
是否考虑返回安全的线程
表13-2? mysqli类中的成员属性(共13个)
成员属性名
描???述
$affected_rows
在前一个MySQL操作中获取影响的行数
$client_info
MySQL客户端版本为一个字符串返回
$client_version
MySQL客户端版本为一个整数返回
$errno
返回最近函数调用的错误代码
$error
返回最近函数调用的错误信息字符串
$field_count
传回最近查询获取的列数
$host_info
返回一个字符串的连接类型使用
$info
检索有关最近执行的查询
$insert_id
返回使用最后查询自动生成的编号
$protocol_version
返回MySQL协议使用的版本
$sqlstate
返回一个字符串包含SQLSTATE错误码的最后一个错
$thread_id
为当前连接返回线程ID
$warning_count
返回前一个SQL语句执行过程中产生的警告数量
上面两个表格列出的mysqli类全部的成员属性和成员方法,当成功创建该类对象以后,就可以调用对象中的成员完成上面两个表格所列出来的功能。下面介绍mysqli类中常见的成员应用。
 相关文章
                            相关文章