MYSQLI操作类
<?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个)

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

会话劫持可以通过以下步骤实现:1.获取会话ID,2.使用会话ID,3.保持会话活跃。在PHP中防范会话劫持的方法包括:1.使用session_regenerate_id()函数重新生成会话ID,2.通过数据库存储会话数据,3.确保所有会话数据通过HTTPS传输。

SOLID原则在PHP开发中的应用包括:1.单一职责原则(SRP):每个类只负责一个功能。2.开闭原则(OCP):通过扩展而非修改实现变化。3.里氏替换原则(LSP):子类可替换基类而不影响程序正确性。4.接口隔离原则(ISP):使用细粒度接口避免依赖不使用的方法。5.依赖倒置原则(DIP):高低层次模块都依赖于抽象,通过依赖注入实现。

PHP8.1中的枚举功能通过定义命名常量增强了代码的清晰度和类型安全性。1)枚举可以是整数、字符串或对象,提高了代码可读性和类型安全性。2)枚举基于类,支持面向对象特性,如遍历和反射。3)枚举可用于比较和赋值,确保类型安全。4)枚举支持添加方法,实现复杂逻辑。5)严格类型检查和错误处理可避免常见错误。6)枚举减少魔法值,提升可维护性,但需注意性能优化。

在PHPStorm中如何进行CLI模式的调试?在使用PHPStorm进行开发时,有时我们需要在命令行界面(CLI)模式下调试PHP�...

如何在系统重启后自动设置unixsocket的权限每次系统重启后,我们都需要执行以下命令来修改unixsocket的权限:sudo...

使用PHP的cURL库发送JSON数据在PHP开发中,经常需要与外部API进行交互,其中一种常见的方式是使用cURL库发送POST�...
