zoukankan      html  css  js  c++  java
  • 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: ?>
     
     
     
     
     
  • 相关阅读:
    I NEED A OFFER!
    水题 Codeforces Round #303 (Div. 2) A. Toy Cars
    模拟 HDOJ 5099 Comparison of Android versions
    模拟 HDOJ 5095 Linearization of the kernel functions in SVM
    贪心 HDOJ 5090 Game with Pearls
    Kruskal HDOJ 1863 畅通工程
    Kruskal HDOJ 1233 还是畅通工程
    并查集 HDOJ 1232 畅通工程
    DFS/并查集 Codeforces Round #286 (Div. 2) B
    水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift
  • 原文地址:https://www.cnblogs.com/lt-style/p/3511494.html
Copyright © 2011-2022 走看看