PHP简单封装MysqlHelper类
MysqlHelper.class.php
1: <?php
2:
3: /**
4: * Mysql数据帮助类
5: */
6: class MysqlHelper
7: {
8: function __construct()
9: {
10: if(isset($conn)){return;}
11: //创建连接对象
12: $this->conn=@mysql_connect($this->host,$this->uid,$this->pwd);
13: if(!$this->conn){
14: die('连接数据库失败:'.mysql_error());
15: }
16:
17: //选择数据库和设置编码
18: mysql_select_db($this->db,$this->conn);
19: mysql_query('set names utf8');
20: }
21:
22: private $conn;
23: private $host='localhost';
24: private $uid='root';
25: private $pwd='1234';
26: private $db='test2';
27:
28: /**
29: * [execute_dql 执行查询语句]
30: * @param [string] $sql [查询语句]
31: */
32: public function execute_dql($sql)
33: {
34: $result = mysql_query($sql,$this->conn) or die('执行DQL语句时出错:'.mysql_error());
35: return $result;
36: }
37:
38: /**
39: * [execute_dml 执行增删改语句]
40: * @param [string] $sql [查询语句]
41: * @return [失败返回-1,无影响返回0,成功返回受影响的行数]
42: */
43: public function execute_dml($sql)
44: {
45: $result = mysql_query($sql,$this->conn) or die('执行DML语句时出错:'.mysql_error());
46: if(!$result){
47: return -1; //操作失败
48: }else{
49: return mysql_affected_rows($this->conn);
50: }
51: }
52:
53: /**
54: * [show_table_data 显示表数据]
55: * @param [string] $tableName [表名]
56: * @return [string] [HTML表格]
57: */
58: public function show_table_data($tableName)
59: {
60: $result = $this->execute_dql("select * from $tableName");
61:
62: $this->show_table($result);
63:
64: mysql_free_result($result);
65: mysql_close($this->conn);
66: }
67:
68: /**
69: * [show_table_info 显示表结构]
70: * @param [string] $tableName [表名]
71: * @return [string] [HTML表格]
72: */
73: public function show_table_info($tableName)
74: {
75: $result = $this->execute_dql("desc $tableName");
76:
77: $this->show_table($result);
78:
79: mysql_free_result($result);
80: mysql_close($this->conn);
81: }
82:
83: /**
84: * [show_table 拼接表格]
85: * @param [resource] $result [结果集]
86: * @return [string] [HTML]
87: */
88: public function show_table($result)
89: {
90: //显示表头信息:
91: echo "<br/>数据表:".mysql_field_table($result, 0)." 总行数:".mysql_num_rows($result);
92: $tableData="<table border='1' cellpadding='5'><tr>";
93: $fieldsCount = mysql_num_fields($result);
94: for ($i=0; $i <$fieldsCount; $i++) {
95: $tableData.= "<th>".mysql_field_name($result, $i)."</th>";
96: }
97: $tableData.="</tr>";
98:
99: //显示数据信息:
100: while ($row = mysql_fetch_object($result)) {
101: $tableData.="<tr>";
102: foreach ($row as $value) {
103: $tableData.="<td>$value</td>";
104: }
105: $tableData.="</tr>";
106: }
107: $tableData.="</table>";
108:
109: echo $tableData;
110: }
111: }
112:
113: ?>
调用示例:
1: <?php
2: header("Content-Type:text/html; charset=utf8");
3:
4: //自定义MysqlHelper的测试与使用
5: //============================================
6:
7: //引用自定义的MysqlHelper类
8: require_once "MysqlHelper.class.php";
9:
10: //实例化MysqlHelper对象
11: $execute = new MysqlHelper();
12:
13: // 增
14: $sql = "insert into userinfo(uName,uAge,uPwd) values('测试04',18,MD5('1234'));";
15: // 删
16: // $sql = "delete from userinfo where id=20";
17: // 改
18: // $sql = "update userinfo set uAge=19 where Id=21";
19:
20: //对数据执行DML操作
21: $returnNum = $execute->execute_dml($sql);
22: if ($returnNum ==-1) {
23: echo "操作失败 :(";
24: } else {
25: echo "操作成功:) <b style='color:red;'>".$returnNum."</b>行受影响<br/>";
26: }
27:
28:
29: //显示表信息:
30: $execute->show_table_info("userinfo");
31:
32: //显示表数据:
33: $execute->show_table_data("userinfo");
34:
35:
36: ?>
PHP简单封装MysqlHelper类的更多相关文章
- PHP基础封装简单的MysqliHelper类
MysqliHelper.class.php 1: <?php 2: 3: /** 4: * MysqliHelper 5: * [面向对象的MysqliHelper的简单封装] 6: */ ...
- ado.net的简单数据库操作(二)之封装SqlHelperl类
今天我书接上回,接着昨天的ado.net的数据库操作的相关知识来讲哈! 从上篇文章给出的实例来看,你一定会发现,操作数据库其实还挺麻烦的,就连一个最简单的数据库操作语句都要包括 定义数据库连接字符串. ...
- 超简单的okhttp封装工具类(上)
版权声明:转载请注明出处:http://blog.csdn.net/piaomiao8179 https://blog.csdn.net/piaomiao8179/article/details/ ...
- python+selenium之自定义封装一个简单的Log类
python+selenium之自定义封装一个简单的Log类 一. 问题分析: 我们需要封装一个简单的日志类,主要有以下内容: 1. 生成的日志文件格式是 年月日时分秒.log 2. 生成的xxx.l ...
- Python之自定义封装一个简单的Log类
参考:http://www.jb51.net/article/42626.htm 参考:http://blog.csdn.net/u011541946/article/details/70198676 ...
- .net core 中简单封装Dapper.Extensions 并使用sqlsuger自动生成实体类
引言 由公司需要使用dapper 同时支持多数据库 又需要支持实体类 又需要支持sql 还需要支持事务 所以采用了 dapper + dapperExtensions 并配套 生成实体类小工具的方 ...
- swift开发之--简单封装Alamofire请求类以及简单使用SnapKit
以前在swift3的时候,写过类似的,那个时候还没有很成熟的网络请求类库,在这里,还是衷心感谢大神们的付出! 具体效果如下,先上图: 点击按钮的时候,请求数据,数据结构如下: { ; reason = ...
- Python+Selenium中级篇之8-Python自定义封装一个简单的Log类《转载》
Python+Selenium中级篇之8-Python自定义封装一个简单的Log类: https://blog.csdn.net/u011541946/article/details/70198676
- web自动化框架—BasePage 类的简单封装
优秀的框架都有属于自己的思想,在搭建web自动化测试框架时,我们通常都遵循 PO(Page Object)思想. 简单理解就是我们会把每个页面看成一个对象,一切皆对象,面向对象编码,这样会让我们更好的 ...
随机推荐
- SQL查询——同一张表的横向与纵向同时比较
表名:student 表结构及数据: +----+--------+---------+------+------------+--------------+---------+ | id | nam ...
- JPA事务总结
http://www.soso.io/article/65405.html 事务管理是JPA中另一项重要的内容,了解了JPA中的事务管理,能够进一步掌握JPA的使用.事务管理是对一系列操作的管理,它最 ...
- HTML5学习总结-08 应用缓存(Application Cache)
一 应用缓存(Application Cache) 1 应用缓存 HTML5 引入了应用程序缓存,这意味着 web 应用可进行缓存,并可在没有因特网连接时进行访问. 应用程序缓存为应用带来三个优势: ...
- 2 NFS高可用解决方案之NFS的搭建
preface 我们紧接着上一篇博文的基础(drbd+heartbeat的正常工作,http://www.cnblogs.com/liaojiafa/p/6129499.html)来搭建NFS的服务. ...
- CentOS下vsftp安装、配置、卸载(转载)
看过多个教程,只有这个能完整成功配置: http://www.centoscn.com/image-text/config/2015/0122/4543.html
- 【原】javascript数组操作
继续我的第二遍<javascript高级程序设计第三版>,今天要做的笔记是array 一.数组的操作 1.数组的创建: var colors= new Array(); //创建一个数组 ...
- 自然语言15.1_Part of Speech Tagging 词性标注
QQ:231469242 欢迎喜欢nltk朋友交流 https://en.wikipedia.org/wiki/Part-of-speech_tagging In corpus linguistics ...
- Wget命令下载、备份博客
-np http://www.cnblogs.com/memory4young/p/ 参考资料: http://www.cnblogs.com/memory4young/p/wget-backup-b ...
- python的简洁是shell无法代替的
之前线上服务器分发配置都是用shell和expect脚本分发,脚本写了很长,上周换了ansible,现在自己用python写一个,就30行代码就可以实现需求,之前的shell写了快200行了,蛋疼,代 ...
- git 删除远程分支
http://www.cnblogs.com/shiningrise/archive/2013/03/12/2956779.html 一不小心把本地的临时分支push到server上去了,想要删除.一 ...