PHP简单封装MysqlHelper类

   1: <?php
   2:  
/**
 * Mysql数据帮助类
 */
class MysqlHelper
   7: {
function __construct()
   9:     {
return;}
//创建连接对象
  12:         $this->conn=@mysql_connect($this->host,$this->uid,$this->pwd);
if(!$this->conn){
'连接数据库失败:'.mysql_error());
  15:         }
  16:  
//选择数据库和设置编码
  18:         mysql_select_db($this->db,$this->conn);
'set names utf8');
  20:     }
  21:  
private $conn;
'localhost';
'root';
'1234';
'test2';
  27:  
/**
     * [execute_dql 执行查询语句]
     * @param [string] $sql [查询语句]
     */
function execute_dql($sql)
  33:     {
'执行DQL语句时出错:'.mysql_error());
return $result;
  36:     }
  37:  
/**
     * [execute_dml 执行增删改语句]
     * @param [string] $sql [查询语句]
     * @return [失败返回-1,无影响返回0,成功返回受影响的行数]
     */
function execute_dml($sql)
  44:     {
'执行DML语句时出错:'.mysql_error());
if(!$result){
//操作失败
else{
return mysql_affected_rows($this->conn);
  50:         }
  51:     }
  52:  
/**
     * [show_table_data 显示表数据]
     * @param  [string] $tableName [表名]
     * @return [string]            [HTML表格]
     */
function show_table_data($tableName)
  59:     {
);
  61:  
  62:         $this->show_table($result);
  63:  
  64:         mysql_free_result($result);
  65:         mysql_close($this->conn);
  66:     }
  67:  
/**
     * [show_table_info 显示表结构]
     * @param  [string] $tableName [表名]
     * @return [string]            [HTML表格]
     */
function show_table_info($tableName)
  74:     {
);
  76:  
  77:         $this->show_table($result);
  78:  
  79:         mysql_free_result($result);
  80:         mysql_close($this->conn);
  81:     }
  82:  
/**
     * [show_table 拼接表格]
     * @param  [resource] $result [结果集]
     * @return [string]         [HTML]
     */
function show_table($result)
  89:     {
//显示表头信息:
.mysql_num_rows($result);
;
  93:         $fieldsCount = mysql_num_fields($result);
for ($i=0; $i <$fieldsCount; $i++) { 
;
  96:         }
;
  98:  
//显示数据信息:
while ($row = mysql_fetch_object($result)) {
;
as $value) {
;
 104:             }
;
 106:         }
;
 108:  
echo $tableData;
 110:     }
 111: }
 112:  
 113: ?>

调用示例:

   1: <?php
);
   3:  
//自定义MysqlHelper的测试与使用
//============================================
   6:  
//引用自定义的MysqlHelper类
;
   9:  
//实例化MysqlHelper对象
new MysqlHelper();
  12:  
// 增
;
// 删
// 改
  19:  
//对数据执行DML操作
  21: $returnNum = $execute->execute_dml($sql);
if ($returnNum ==-1) {
;
else {
;
  26: }
  27:  
  28:  
//显示表信息:
);
  31:  
//显示表数据:
);
  34:  
  35:  
  36: ?>