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类的更多相关文章

  1. PHP基础封装简单的MysqliHelper类

    MysqliHelper.class.php 1: <?php 2:  3: /** 4: * MysqliHelper 5: * [面向对象的MysqliHelper的简单封装] 6: */ ...

  2. ado.net的简单数据库操作(二)之封装SqlHelperl类

    今天我书接上回,接着昨天的ado.net的数据库操作的相关知识来讲哈! 从上篇文章给出的实例来看,你一定会发现,操作数据库其实还挺麻烦的,就连一个最简单的数据库操作语句都要包括 定义数据库连接字符串. ...

  3. 超简单的okhttp封装工具类(上)

      版权声明:转载请注明出处:http://blog.csdn.net/piaomiao8179 https://blog.csdn.net/piaomiao8179/article/details/ ...

  4. python+selenium之自定义封装一个简单的Log类

    python+selenium之自定义封装一个简单的Log类 一. 问题分析: 我们需要封装一个简单的日志类,主要有以下内容: 1. 生成的日志文件格式是 年月日时分秒.log 2. 生成的xxx.l ...

  5. Python之自定义封装一个简单的Log类

    参考:http://www.jb51.net/article/42626.htm 参考:http://blog.csdn.net/u011541946/article/details/70198676 ...

  6. .net core 中简单封装Dapper.Extensions 并使用sqlsuger自动生成实体类

    引言 由公司需要使用dapper  同时支持多数据库 又需要支持实体类 又需要支持sql 还需要支持事务 所以采用了 dapper + dapperExtensions  并配套 生成实体类小工具的方 ...

  7. swift开发之--简单封装Alamofire请求类以及简单使用SnapKit

    以前在swift3的时候,写过类似的,那个时候还没有很成熟的网络请求类库,在这里,还是衷心感谢大神们的付出! 具体效果如下,先上图: 点击按钮的时候,请求数据,数据结构如下: { ; reason = ...

  8. Python+Selenium中级篇之8-Python自定义封装一个简单的Log类《转载》

    Python+Selenium中级篇之8-Python自定义封装一个简单的Log类: https://blog.csdn.net/u011541946/article/details/70198676

  9. web自动化框架—BasePage 类的简单封装

    优秀的框架都有属于自己的思想,在搭建web自动化测试框架时,我们通常都遵循 PO(Page Object)思想. 简单理解就是我们会把每个页面看成一个对象,一切皆对象,面向对象编码,这样会让我们更好的 ...

随机推荐

  1. CSS基础知识真难啊-background-渐变

    文章参考 http://www.zhangxinxu.com/wordpress/?p=727 http://www.uqu8.com/html/2014/html-css_1105/176.html ...

  2. linux安装Mac的默认Monaco字体

    Monaco字体是我最喜欢的编程字体,如果你想在linux上面安装,只需要在terminal中执行: curl -kL https://raw.github.com/cstrap/monaco-fon ...

  3. PTA 链表删除结点的题目测试

    链表删除结点 题目描述 输入一个正整数repeat(0 < repeat < 10),做repeat次下列运算: 输入一个正整数n(0 < n < 10)和一组( n 个 )整 ...

  4. java Base64算法的使用

    Base64是常见的网络加密算法,Base64编码可用于在HTTP环境下传递较长的标识信息.详见 Base64介绍 1 自定义的base64算法 Base64Encrypt.java public c ...

  5. linux系统性能监视命令

    preface as a linux engineer,you should know how to use these command of monitor system,so let's lear ...

  6. MVVM

    MVVM 是 Model-View-ViewModel 的简写,MVVM 模式和 MVC 模式一样,主要目的是分离视图(View)和模型(Model) 接下来给大家分享一个总结的MVVM,来吧---- ...

  7. uC/OS-II中includes块

    /*************************************************************************************************** ...

  8. angularjs中ng-selected使用方法

    ng-selected只能应用在option标签上,就像ng-submit只能应用在form标签上一样. ng-selected指令为select设置了指定的选中值,HTML规范不允许浏览器保存类似s ...

  9. MSDeploy 同步时不删除原有文件

    在 jenkins里  Execute Windows batch command "C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\m ...

  10. CookContainer 序列化保存

    using System;using System.Collections;using System.Globalization;using System.IO;using System.Net;us ...