您的位置:首页 > 编程语言 > PHP开发

PHP基础封装简单的MysqliHelper类

2014-01-08 23:27 537 查看

MysqliHelper.class.php

[code]     [code] <?php


 


/**


* MysqliHelper


* [面向对象的MysqliHelper的简单封装]


*/


class MysqliHelper


{


private static $mysqli;


private static $host='localhost';


private static $uid='root';


private static $pwd='1234';


private static $dbName='test2';


 


    /**


* [connect 创建连接]


     */


public static function connect()


    {


if(isset(self::$mysqli))return;


self::$mysqli = new MySQLi(self::$host,self::$uid,self::$pwd,self::$dbName);


!empty(self::$mysqli) or die("数据库连接失败:".self::$mysqli->connect_error);


self::$mysqli->query("set names utf8");


}


 


    /**


* [execute_dml 执行增删改操作]


* @param  [string] $sql [DML语句]


* @return [int]      [操作成功返回受影响行数,否则返回-1]


     */


public static function execute_dml($sql)


    {


self::connect();


$result = self::$mysqli->query($sql) or die('执行DML语句时出错:'.self::$mysqli->error);


         if (!$result){


return -1;


  } else{


return self::$mysqli->affected_rows;


  }


}


 


    /**


* [execute_dql 执行查询操作]


* @param  [string] $sql [DQL语句]


* @return [mysqli_result]      [返回查询结果集]


     */


public static function execute_dql($sql)


    {


self::connect();


$result = self::$mysqli->query($sql) or die('执行DQL语句时出错:'.self::$mysqli->error);


return $result;


}


 


    /**


* [show_table_data 显示表数据]


* @param  [string] $tableName [表名]


* @return [string]            [HTML]


     */


public static function show_table_data($tableName)


    {


$result=self::execute_dql("select * from $tableName");


//显示表头信息:


echo "<br/>数据表:".$result->fetch_field()->table."  总行数:".$result->num_rows;


$tableData="<table border='1' cellpadding='5'><tr>";


$fieldsCount = $result->field_count;


         for ($i=0; $i <$fieldsCount; $i++){ 


$tableData.= "<th>".$result->fetch_field_direct($i)->name."</th>";


  }


$tableData.="</tr>";


 


//显示数据信息:


         while ($row = $result->fetch_object()){


$tableData.="<tr>";


             foreach ($row as $value){


$tableData.="<td>$value</td>";


 }


    $tableData.="</tr>";


  }


$tableData.="</table>";


 


$result->free();


self::$mysqli->close();


//或者 mysqli_close(self::$mysqli);


echo $tableData;


}


 


}


?>

[/code]
[/code]

 

调用示例:

 

[code]
[code] <?php


header("Content-Type:text/html; charset=utf8");


 


//自定义MysqliHelper的测试与使用


//============================================


 


//引用自定义的MysqliHelper类


require_once "MysqliHelper.class.php";


 


// 查


// $sql = "select * from userinfo where id>8";


// 增


// $sql = "insert into userinfo(uName,uAge,uPwd) values('测试08',25,MD5('1234'));";


// 删


// $sql = "delete from userinfo where id=24";


// 改


$sql = "update userinfo set uAge=28 where Id=21";


 


//执行查询语句


// $result = MysqliHelper::execute_dql($sql);


 


//执行增删改语句


$result = MysqliHelper::execute_dml($sql);


 if ($result==-1){


echo "操作失败:(";


} else{


echo "操作成功:".$result."行受影响";


}


 


//显示表数据


MysqliHelper::show_table_data("userinfo");


 


?>

[/code]
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: