一个基于PDO的数据库操作类(新) 一个PDO事务实例
<?php
/*
* 作者:胡睿
* 日期:2011/03/19
* 电邮:hooray0905@foxmail.com
*
* 20110319
* 常用数据库操作,如:增删改查,获取单条记录、多条记录,返回最新一条插入记录id,返回操作记录行数等
* 20110630
* 整体修改方法,合并部分参数
* 规范代码,一个方法里只有1个return语句
*/
/*
参数说明
int $debug 是否开启调试,开启则输出sql语句
int $mode 0 返回数组
1 返回单条记录
2 返回行数
string $table 数据库表
string $fields 需要查询的数据库字段,允许为空,默认为查找全部
string $sqlwhere 查询条件,允许为空
string $orderby 排序,允许为空,默认为id倒序
*/
function hrSelect($debug, $mode, $table, $fields="*", $sqlwhere="", $orderby="id desc"){
global $pdo;
if($debug){
if($mode == 2){
echo "select count(*) from $table where 1=1 $sqlwhere order by $orderby";
}elseif($mode == 1){
echo "select $fields from $table where 1=1 $sqlwhere";
}else{
echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";
}
exit;
}else{
if($mode == 2){
$rs = $pdo->query("select count(*) from $table where 1=1 $sqlwhere order by $orderby");
$return = $rs->fetchColumn();
}elseif($mode == 1){
$rs = $pdo->query("select $fields from $table where 1=1 $sqlwhere");
$return = $rs->fetch();
}else{
$rs = $pdo->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
$return = $rs->fetchAll();
}
return $return;
}
}
/*
参数说明
int $debug 是否开启调试,开启则输出sql语句
int $mode 0 默认insert,无返回信息
1 返回执行条目数
2 返回最后一次插入记录的id
string $table 数据库表
string $fields 需要插入数据库的字段
string $values 需要插入数据库的信息,必须与$fields一一对应
*/
function hrInsert($debug, $mode, $table, $fields, $values){
global $pdo;
if($debug){
echo "insert into $table ($fields) values ($values)";
exit;
}else{
if($mode == 2){
$return = $pdo->lastInsertId("insert into $table ($fields) values ($values)");
}elseif($mode == 1){
$return = $pdo->exec("insert into $table ($fields) values ($values)");
}else{
$pdo->query("insert into $table ($fields) values ($values)");
exit;
}
return $return;
}
}
/*
参数说明
int $debug 是否开启调试,开启则输出sql语句
int $mode 0 默认update,无返回信息
1 返回执行条目数
string $table 数据库表
string $set 需要更新的字段及内容,格式:a='abc',b=2,c='2010-10-10 10:10:10'
string $sqlwhere 修改条件,允许为空
*/
function hrUpdate($debug, $mode, $table, $set, $sqlwhere=""){
global $pdo;
if($debug){
echo "update $table set $set where 1=1 $sqlwhere";
exit;
}else{
if($mode==1){
$return = $pdo->exec("update $table set $set where 1=1 $sqlwhere");
}else{
$pdo->query("update $table set $set where 1=1 $sqlwhere");
exit;
}
return $return;
}
}
/*
参数说明
int $debug 是否开启调试,开启则输出sql语句
int $mode 0 默认delete,无返回信息
1 返回执行条目数
string $table 数据库表
string $sqlwhere 删除条件,允许为空
*/
function hrDelete($debug, $mode, $table, $sqlwhere=""){
global $pdo;
if($debug){
echo "delete from $table where 1=1 $sqlwhere";
exit;
}else{
if($mode == 1){
$return = $pdo->exec("delete from $table where 1=1 $sqlwhere");
}else{
$pdo->query("delete from $table where 1=1 $sqlwhere");
exit;
}
return $return;
}
}
?> 另外一段代码是基于我这个数据库操作类的事务实例: 复制代码 代码如下: /*
注意,数据库操作表类型必须为InnoDB,其他类型不支持事务
PDO事务机制
$pdo->beginTransaction(); --开启事务
$pdo->commit(); --结束事务
$pdo->rollBack(); --回滚操作 示例,用try/catch包住db操作,当事务内的db操作出现中断,则执行回滚并抛出异常信息。
*/
try{
$pdo->beginTransaction();
hrInsert(0,1,"class","name,parentid","'god',0"); //可以正常执行
hrInsert(0,0,0,"tb_searchlog","userid,code","4"); //出错
$pdo->commit();
}catch(Exception $e){
$pdo->rollBack();
echo "Failed: " . $e->getMessage();
}
一个基于PDO的数据库操作类(新) 一个PDO事务实例的更多相关文章
- PHP 基于pdo的数据库操作类
http://www.php.cn/php-weizijiaocheng-404645.html <?php class Pdodb{ protected $pdo; protected ...
- php pdo mysql数据库操作类
<?php namespace iphp\core; use iphp\App; /** * 数据库操作基类 基于pdo * @author xuen * 支持链式操作,支持参数绑定 * 说明1 ...
- RabbitMQ学习之(五)_一个基于PHP的RabbitMQ操作类
//amqp.php类文件 <?php class Amqp { public $e_name; public $q_name; public $k_route; public $channel ...
- PDO数据库操作类
<?php include 'common_config.php'; /** * Class Mysql * PDO数据库操作类 */ class Mysql { protected stati ...
- 【知识必备】ezSQL,最好用的数据库操作类,让php操作sql更简单~
最近用php做了点小东东,用上了ezSQL,感觉真的很ez,所以拿来跟大家分享一下~ ezSQL是一个非常好用的PHP数据库操作类.著名的开源博客WordPress的数据库操作就使用了ezSQL的My ...
- PHP 数据库操作类:ezSQL
EZSQL类介绍: 下载地址:http://www.jb51.net/codes/26393.htmlezsql是一个小型的快速的数据库操作类,可以让你很容易地用PHP操作各种数据库( MySQL.o ...
- ecshop数据库操作类
ECShop v2.7.2没有使用一些开源的数据库操作类,比如adodb或者PEAR,而是封装了自己的实现. 好处:实现非常轻量,只有一个文件,27Kb,大大减小了分发包的文件大小. 当网站需要做me ...
- ECShop - 数据库操作类
ECShop v2.7.2没有使用一些开源的数据库操作类,比如adodb或者PEAR,而是封装了自己的实现.这样做的好处是实现非常轻量,只有一个文件,27Kb,大大减小了分发包的文件大小.另外,当网站 ...
- C#全能数据库操作类及调用示例
C#全能数据库操作类及调用示例 using System; using System.Data; using System.Data.Common; using System.Configuratio ...
随机推荐
- laravel where中多条件查询
1. http://www.mobanstore.com/doc/bianchengkaifa/119.html //初学laravel 发现他的查询构造器很好用 //如下 $user = DB::t ...
- D&F学数据结构系列——AVL树(平衡二叉树)
AVL树(带有平衡条件的二叉查找树) 定义:一棵AVL树是其每个节点的左子树和右子树的高度最多差1的二叉查找树. 为什么要使用AVL树(即为什么要给二叉查找树增加平衡条件),已经在我之前的博文中说到过 ...
- Leetcode: strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- intelli IDEA node开发代码提示问题
好几天没写代码了,今天新建一个项目,在引入rs这个文件系统模块时却没有关于这个模块的代码提示,着实令人恶心啊.还好最终解决了. 在没有代码提示的时候点击如下图标: 出现如下的界面,其中有个Edit u ...
- iOS 消息转发
消息转发 delegate和protocol 类别 消息转发 当向someObject发送某消息,但runtime system在当前类和父类中都找不到对应方法的实现时,runt ...
- CentOS下的账户管理
在Linux中,每个文件都分3类权限:账户本身的权限,账户所在群组的权限和其它权限.账户和群组是多对多的关系,即一个账户可以属于多个群组,一个群组可以包含多个账户.但是,对于每一个已登录的账户,只能存 ...
- Linux SHELL脚本
在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具.Shell不仅仅是命令的收集,而且是一门非常棒的编程语言.可以通过使用shell使大量的任务自动化,shell ...
- SSIS ->> Error Handling
Event Handler Each task and container raises events as it runs, such as an OnError event, among seve ...
- Oracle ->> 层级查询语句(hierarchical query)connect by
Oracle中的Connect By... Start With语句实现了递归查询或者树状查询. Connect By Prior 一方为起始(root)的ID 参考: http://www.360d ...
- 在struts-config.xml中配置validator-plugin导致404 Servlet action is not available
就是在struts-config.xml中添加了这么一段 <plug-in className="org.apache.struts.validator.ValidatorPlugIn ...