PHP基础封装简单的MysqliHelper类

摘要:
emptyordie;23:self::$mysqli-˃query;24:}25:26:/**27:*[execute_dml执行增删改操作]28:*@param[string]$sql[DML语句]29:*@return[int][操作成功返回受影响行数,否则返回-1]30:*/31:publicstaticfunctionexecute_dml32:{33:self::connect();34:$result=self::$mysqli-˃queryordie;35:if(!$result){36:return-1;37:}else{38:returnself::$mysqli-˃affected_rows;39:}40:}41:42:/**43:*[execute_dql执行查询操作]44:*@param[string]$sql[DQL语句]45:*@return[mysqli_result][返回查询结果集]46:*/47:publicstaticfunctionexecute_dql48:{49:self::connect();50:$result=self::$mysqli-˃queryordie;51:return$result;52:}53:54:/**55:*[show_table_data显示表数据]56:*@param[string]$tableName[表名]57:*@return[string][HTML]58:*/59:publicstaticfunctionshow_table_data60:{61:$result=self::execute_dql;62://显示表头信息:63:echo"数据表:".$result-˃fetch_field()-˃table."总行数:".$result-˃num_rows;64:$tableData="";65:$fieldsCount=$result-˃field_count;66:for{67:$tableData.="".$result-˃fetch_field_direct($i)-˃name."";68:}69:$tableData.="";70:71://显示数据信息:72:while{73:$tableData.="";74:foreach{75:$tableData.="$value";76:}77:$tableData.="";78:}79:$tableData.="";80:81:$result-˃free();82:self::$mysqli-˃close();83://或者mysqli_close;84:echo$tableData;85:}86:87:}88:?
MysqliHelper.class.php
1: <?php
2: 
3: /**
4: * MysqliHelper
5: * [面向对象的MysqliHelper的简单封装]
6: */
7: class MysqliHelper
8: {
9:     private static $mysqli;
10:     private static $host='localhost';
11:     private static $uid='root';
12:     private static $pwd='1234';
13:     private static $dbName='test2';
14: 
15:     /**
16: * [connect 创建连接]
17: */
18:     public static function connect()
19:     {
20:         if(isset(self::$mysqli))return;
21:         self::$mysqli = new MySQLi(self::$host,self::$uid,self::$pwd,self::$dbName);
22:         !empty(self::$mysqli) or die("数据库连接失败:".self::$mysqli->connect_error);
23:         self::$mysqli->query("set names utf8");
24:     }
25: 
26:     /**
27: * [execute_dml 执行增删改操作]
28: * @param  [string] $sql [DML语句]
29: * @return [int]      [操作成功返回受影响行数,否则返回-1]
30: */
31:     public static function execute_dml($sql)
32:     {
33:         self::connect();
34:         $result = self::$mysqli->query($sql) or die('执行DML语句时出错:'.self::$mysqli->error);
35:         if (!$result) {
36:             return -1;
37:         } else {
38:             return self::$mysqli->affected_rows;
39:         }
40:     }
41: 
42:     /**
43: * [execute_dql 执行查询操作]
44: * @param  [string] $sql [DQL语句]
45: * @return [mysqli_result]      [返回查询结果集]
46: */
47:     public static function execute_dql($sql)
48:     {
49:         self::connect();
50:         $result = self::$mysqli->query($sql) or die('执行DQL语句时出错:'.self::$mysqli->error);
51:         return $result;
52:     }
53: 
54:     /**
55: * [show_table_data 显示表数据]
56: * @param  [string] $tableName [表名]
57: * @return [string]            [HTML]
58: */
59:     public static function show_table_data($tableName)
60:     {
61:         $result=self::execute_dql("select * from $tableName");
62:         //显示表头信息:
63:         echo "<br/>数据表:".$result->fetch_field()->table."  总行数:".$result->num_rows;
64:         $tableData="<table border='1' cellpadding='5'><tr>";
65:         $fieldsCount = $result->field_count;
66:         for ($i=0; $i <$fieldsCount; $i++) { 
67:             $tableData.= "<th>".$result->fetch_field_direct($i)->name."</th>";
68:         }
69:         $tableData.="</tr>";
70: 
71:         //显示数据信息:
72:         while ($row = $result->fetch_object()) {
73:             $tableData.="<tr>";
74:             foreach ($row as $value) {
75:                 $tableData.="<td>$value</td>";
76:             }
77:             $tableData.="</tr>";
78:         }
79:         $tableData.="</table>";
80: 
81:         $result->free();
82:         self::$mysqli->close();
83:         //或者 mysqli_close(self::$mysqli);
84:         echo $tableData;
85:     }
86: 
87: }
88: ?>
调用示例:
1: <?php
2: header("Content-Type:text/html; charset=utf8");
3: 
4: //自定义MysqliHelper的测试与使用
5: //============================================
6: 
7: //引用自定义的MysqliHelper类
8: require_once "MysqliHelper.class.php";
9: 
10: // 查
11: // $sql = "select * from userinfo where id>8";
12: // 增
13: // $sql = "insert into userinfo(uName,uAge,uPwd) values('测试08',25,MD5('1234'));";
14: // 删
15: // $sql = "delete from userinfo where id=24";
16: // 改
17: $sql = "update userinfo set uAge=28 where Id=21";
18: 
19: //执行查询语句
20: // $result = MysqliHelper::execute_dql($sql);
21: 
22: //执行增删改语句
23: $result = MysqliHelper::execute_dml($sql);
24: if ($result==-1) {
25:     echo "操作失败:(";
26: } else {
27:     echo "操作成功:".$result."行受影响";
28: }
29: 
30: //显示表数据
31: MysqliHelper::show_table_data("userinfo");
32: 
33: ?>

免责声明:文章转载自《PHP基础封装简单的MysqliHelper类》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇VxWorks固件分析方法总结Minecraft类游戏地形生成机制下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

nginx和fpm的进程数配置和502,504错误

 502 和 php-fpm.conf 1.php-cgi进程数不够用。php执行时间长,导致没有空闲进程处理新请求。 2.php-cgi进程死掉。php-fpm超时时间短,当前进程执行超时关闭连接。 实例: 1.request_terminate_timeout引起的资源问题 request_terminate_timeout默认值为 0 秒,也就是说,...

mysql数据库编码问题

一:插入数据乱码 ①:数据库的字符集不对,需要修改成utf-8;如果解决不了走第二步 ②: var cmd = new MySqlCommand(“set names utf8”);如果解决不了走第三步 ③:把“Charset=utf8”加载数据库连接字符串后边即可。在解决不了没辙了,换数据库吧!  二:获取有乱码问题  Incorrect strin...

数据库基本概念及简单的单表操作

一:MySQL的基本介绍:MySQL是一种关系型数据库mysql是属于Oracle旗下的一款数据库产品。分为商业版和社区版 技术角度分析:MySQL数据库是一种C/S(客户端/服务器)模型的服务     B/S(浏览器/服务器)MySQL的网络通信模型为:NIO+连接池来实现,支持高并发的应用场景   SQL: Structred Query Langu...

mysql只显示表名和备注

查看某个数据下的表及其备注: select table_name,table_comment from information_schema.tables where table_schema='db' ; 只要改后面的table_schema为你的数据库名 结果: 查看某个表下的字段及其备注 desc 表名; show columns from表名...

最全的Windows Azure学习教程汇总

Windows Azure 是微软基于云计算的操作系统,能够为开发者提供一个平台,帮助开发可运行在云服务器、数据中心、Web 和 PC 上的应用程序。 Azure 是一种灵活和支持互操作的平台,能够将处于云端的开发者个人能力,同微软全球数据中心网络托管的服务,比如存储、计算和网络基础设施服务,紧密结合起来。帮助开发者在“云端”和“客户端”同时部署应用,使得...

springboot备份mysql后发送邮件并删除备份文件,支持win和Linux

首先加入springboot的邮箱依赖 <!--邮箱依赖--> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail --> <dependency> <groupId>...