搜索

MYSQLI操作类

Nov 24, 2016 am 09:31 AM

<?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    = &#39;./cache/&#39;; 
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 &#39;<h2>&#39;.mysqli_connect_error().&#39;</h2>&#39;; 
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 &#39;./cache.php&#39;;//若框架中使用__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, &#39;getOne&#39;); 
} 
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, &#39;getRow&#39;); 
} 
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, &#39;getAll&#39;); 
} 
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(&#39;localhost&#39;, &#39;root&#39;, 111222, &#39;dict&#39;); 
$db->set_cache_time(10); 
$db->set_cache_dir(&#39;./cache/sql/&#39;); 
$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(),&#39;<br>&#39;; 
?>

mysqli类的对象主要控制PHP和MySQL数据库服务器之间的连接、选择数据库、向MySQL服务器发送SQL语句,以及设置字符集等,这些 任务都是通过该类中声明的构造方法、成员方法和成员属性完成的。在表13-1和表13-2两个表格中,分别列出了mysqli类中声明的成员方法和成员属 性。

表13-1  mysqli类中的成员方法(共33个)

QQ截图20161124093110.png

QQ截图20161124093123.png

QQ截图20161124093140.png


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP的完整形式是什么?PHP的完整形式是什么?Apr 28, 2025 pm 04:58 PM

文章讨论了PHP,详细介绍了其完整形式,在We​​b开发中的主要用途,与Python和Java的比较以及对初学者的学习便利性。

PHP如何处理形式数据?PHP如何处理形式数据?Apr 28, 2025 pm 04:57 PM

PHP使用$ \ _ post和$ \ _获取超级全局的php处理数据,并通过验证,消毒和安全数据库交互确保安全性。

PHP和ASP.NET有什么区别?PHP和ASP.NET有什么区别?Apr 28, 2025 pm 04:56 PM

本文比较了PHP和ASP.NET,重点是它们对大规模Web应用程序,性能差异和安全功能的适用性。两者对于大型项目都是可行的,但是PHP是开源和无关的,而ASP.NET,

PHP是对病例敏感的语言吗?PHP是对病例敏感的语言吗?Apr 28, 2025 pm 04:55 PM

PHP的情况敏感性各不相同:功能不敏感,而变量和类是敏感的。最佳实践包括一致的命名和使用对案例不敏感的功能进行比较。

您如何重定向PHP中的页面?您如何重定向PHP中的页面?Apr 28, 2025 pm 04:54 PM

本文讨论了PHP中针对页面重定向的各种方法,重点关注header()函数,并解决了诸如“标题已经发送”错误之类的常见问题。

解释PHP中的类型暗示解释PHP中的类型暗示Apr 28, 2025 pm 04:52 PM

文章讨论了PHP中的类型暗示,这是一个用于指定功能中预期数据类型的功能。主要问题是通过类型执法提高代码质量和可读性。

PHP中的PDO是什么?PHP中的PDO是什么?Apr 28, 2025 pm 04:51 PM

本文讨论了PHP数据对象(PDO),这是PHP中数据库访问的扩展名。它通过准备好的语句及其对MySQLI的好处,包括数据库抽象和更好的错误处理,强调了PDO在增强安全性方面的作用。

如何在PHP中创建API?如何在PHP中创建API?Apr 28, 2025 pm 04:50 PM

文章讨论了创建和保护PHP API,详细介绍了从端点定义到使用Laravel和最佳安全实践等框架优化性能优化的步骤。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器