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)思想. 简单理解就是我们会把每个页面看成一个对象,一切皆对象,面向对象编码,这样会让我们更好的 ...
随机推荐
- 使用 BASH 作为 CGI 进行 HTTP 文件上传
憋半天憋出这么点代码来,暂时凑合可以用...需要手动删除文件末尾的分隔符,还有一个windows 换行: #!/bin/bash newline="" while true; do ...
- HDU 1710 Binary Tree Traversals(二叉树遍历)
传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...
- vc++ 中 IntelliSense: 无法打开 源 文件 "xxx.h"
类似无法找到文件的问题都可以用这个方法解决,就是路径的问题.vc++2008的项目转到vc++2010也可能出现类似的问题. 解决方法: 在 项目属性=>配置属性=>C/C++ =&g ...
- BZOJ3224 普通平衡树
传送门: Treap 版本: //OJ 1999 //by Cydiater //2016.8.30 #include <iostream> #include <cstdio> ...
- Ubuntu下忘记MySQL密码重设方法
今天在做Python的实验,要用到MySQL数据库,但是密码是啥捏~~~果断重新设置密码啦啦啦 http://www.cnblogs.com/yuxc/archive/2012/07/25/26075 ...
- How to set up an FTP server on Ubuntu 14.04
How to set up an FTP server on Ubuntu 14.04 Setting up a fully-functional and highly secure FTP serv ...
- mybatis int 类型判断<if>
如果数据类型是integer或者int,也就是数据类型的,在用<if>标签做动态语句的时候 不用判断是否为"''" <if test="sex != n ...
- hadoop2.6.4运行wordcount
hadoop用户登录,启动服务: start-dfs.sh && start-yarn.sh 创建输入目录: hadoop df -mkdir /input 把测试文件导入/input ...
- Spring浅探
热度最大的框架,它也称为业务层框架.Spring这个框架的诞生,给程序员揭示了两个主要的思想:Ioc,Aop: 最近的网页架构可以分为这样. 传统结构中,每个层都得new出依赖层的类进行一些本层操作, ...
- 10月16日上午MySQL数据库作业设计表解析
作业设计表:多张表存储学生成绩及各种信息 需要从表里面体现: 关于学生的:代号 姓名 性别 年龄 班级 关于课程的:代号 名称 关于老师的:代号 姓名 关于成绩的:例如:闫超--网页--90 要能查看 ...