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]
相关文章推荐
- PHP基础Mysql扩展库
- man bootparam 内核启动参数文档
- 2014.01.08php循环语句学习
- PHP简单封装MysqlHelper类
- php编译完成后,module追加编译进php
- PHP 文件与文件夹的创建和删除操作
- PHP文件的上传与下载
- PHP文件操作 读取与写入
- lftp与ftp的数据传输模式
- [转]XCache 3.0.0 发布,PHP 性能提升方案
- PHP 文件与目录操作函数总结
- ubuntu 13.10 LAMP + PhpMyAdmin 配置
- PHP设计模式之:单例模式
- PHP封装一个通用好用的文件上传处理类
- php读取flash文件高宽帧数背景颜色代码
- php通过COM类调用组件
- php 截取字符串 解决html实体的问题
- php.ini 变量功能标注
- 收集的二十一个实用便利的PHP函数代码
- 开发的第一个PHP扩展