当前位置: 网学 > 编程文档 > PHP > 正文

一个基于PDO的数据库操作类(新) 一个PDO事务实例

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 13/06/22

鉴于大家对PHP十分关注,我们编辑小组在此为大家搜集整理了“一个基于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 wher

网学推荐

免费论文

原创论文

浏览:
设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
湘ICP备09003080号