PHP基础封装简单的MysqliHelper类 MysqliHelper.class.php   调用示例:

1: <?php
   2:  
/**
* MysqliHelper
* [面向对象的MysqliHelper的简单封装]
*/
class MysqliHelper
   8: {
static $mysqli;
'localhost';
'root';
'1234';
'test2';
  14:  
/**
     * [connect 创建连接]
     */
function connect()
  19:     {
return;
new MySQLi(self::$host,self::$uid,self::$pwd,self::$dbName);
.self::$mysqli->connect_error);
);
  24:     }
  25:  
/**
     * [execute_dml 执行增删改操作]
     * @param  [string] $sql [DML语句]
     * @return [int]      [操作成功返回受影响行数,否则返回-1]
     */
function execute_dml($sql)
  32:     {
  33:         self::connect();
'执行DML语句时出错:'.self::$mysqli->error);
if (!$result) {
return -1;
else {
return self::$mysqli->affected_rows;
  39:         }
  40:     }
  41:  
/**
     * [execute_dql 执行查询操作]
     * @param  [string] $sql [DQL语句]
     * @return [mysqli_result]      [返回查询结果集]
     */
function execute_dql($sql)
  48:     {
  49:         self::connect();
'执行DQL语句时出错:'.self::$mysqli->error);
return $result;
  52:     }
  53:  
/**
     * [show_table_data 显示表数据]
     * @param  [string] $tableName [表名]
     * @return [string]            [HTML]
     */
function show_table_data($tableName)
  60:     {
);
//显示表头信息:
.$result->num_rows;
;
  65:         $fieldsCount = $result->field_count;
for ($i=0; $i <$fieldsCount; $i++) { 
;
  68:         }
;
  70:  
//显示数据信息:
while ($row = $result->fetch_object()) {
;
as $value) {
;
  76:             }
;
  78:         }
;
  80:  
  81:         $result->free();
  82:         self::$mysqli->close();
//或者 mysqli_close(self::$mysqli);
echo $tableData;
  85:     }
  86:  
  87: }
  88: ?>

 

调用示例:

   1: <?php
);
   3:  
//自定义MysqliHelper的测试与使用
//============================================
   6:  
//引用自定义的MysqliHelper类
;
   9:  
// 查
// 增
// 删
// 改
;
  18:  
//执行查询语句
// $result = MysqliHelper::execute_dql($sql);
  21:  
//执行增删改语句
  23: $result = MysqliHelper::execute_dml($sql);
if ($result==-1) {
;
else {
;
  28: }
  29:  
//显示表数据
);
  32:  
  33: ?>