色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

一個基于PDO的數(shù)據(jù)庫操作類

百度之后決定使用PDO,至于為什么選擇PDO,這里就不再多說,大家自己去百度下就能明白。
既然要換,那最基本就需要有個常用的數(shù)據(jù)庫操作類,也就是所謂的增刪改查等,昨晚搗騰了一晚,大致弄出了個雛形,以下就是代碼,希望大家能給出點意見。
復(fù)制代碼 代碼如下:
<?php
/*
作者:胡睿
日期:2011/03/19
電郵:hooray0905@foxmail.com
20110319
常用數(shù)據(jù)庫操作,如:增刪改查,獲取單條記錄、多條記錄,返回最新一條插入記錄id,返回操作記錄行數(shù)等
*/
/*
參數(shù)說明
int $debug 是否開啟調(diào)試,開啟則輸出sql語句
int $getcount 是否記數(shù),返回值為行數(shù)
int $getrow 是否返回值單條記錄
string $table 數(shù)據(jù)庫表
string $fields 需要查詢的數(shù)據(jù)庫字段,允許為空,默認(rèn)為查找全部
string $sqlwhere 查詢條件,允許為空
string $orderby 排序,允許為空,默認(rèn)為id倒序
*/
function hrSelect($debug, $getcount, $getrow, $table, $fields="*", $sqlwhere="", $orderby="id desc"){
global $pdo;
if($debug){
if($getcount){
echo "select count(*) from $table where 1=1 $sqlwhere order by $orderby";
}else{
echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";
}
exit;
}else{
if($getcount){
$rs = $pdo->query("select count(*) from $table where 1=1 $sqlwhere order by $orderby");
return $rs->fetchColumn();
}elseif($getrow){
$rs = $pdo->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
return $rs->fetch();
}else{
$rs = $pdo->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
return $rs->fetchAll();
}
}
}
/*
參數(shù)說明
int $debug 是否開啟調(diào)試,開啟則輸出sql語句
int $execrow 是否開啟返回執(zhí)行條目數(shù)
int $lastinsertid 是否開啟返回最后一條插入記錄id
string $table 數(shù)據(jù)庫表
string $fields 需要插入數(shù)據(jù)庫的字段
string $values 需要插入數(shù)據(jù)庫的信息,必須與$fields一一對應(yīng)
*/
function hrInsert($debug, $execrow, $lastinsertid, $table, $fields, $values){
global $pdo;
if($debug){
echo "insert into $table ($fields) values ($values)";
exit;
}elseif($execrow){
return $pdo->exec("insert into $table ($fields) values ($values)");
}elseif($lastinsertid){
return $pdo->lastInsertId("insert into $table ($fields) values ($values)");
}else{
$pdo->query("insert into $table ($fields) values ($values)");
}
}
/*
參數(shù)說明
int $debug 是否開啟調(diào)試,開啟則輸出sql語句
int $execrow 是否開啟執(zhí)行并返回條目數(shù)
string $table 數(shù)據(jù)庫表
string $set 需要更新的字段及內(nèi)容,格式:a='abc',b=2,c='2010-10-10 10:10:10'
string $sqlwhere 修改條件,允許為空
*/
function hrUpdate($debug, $execrow, $table, $set, $sqlwhere=""){
global $pdo;
if($debug){
echo "update $table set $set where 1=1 $sqlwhere";
exit;
}elseif($execrow){
return $pdo->exec("update $table set $set where 1=1 $sqlwhere");
}else{
$pdo->query("update $table set $set where 1=1 $sqlwhere");
}
}
/*
參數(shù)說明
int $debug 是否開啟調(diào)試,開啟則輸出sql語句
int $execrow 是否開啟返回執(zhí)行條目數(shù)
string $table 數(shù)據(jù)庫表
string $sqlwhere 刪除條件,允許為空
*/
function hrDelete($debug, $execrow, $table, $sqlwhere=""){
global $pdo;
if($debug){
echo "delete from $table where 1=1 $sqlwhere";
exit;
}elseif($execrow){
return $pdo->exec("delete from $table where 1=1 $sqlwhere");
}else{
$pdo->query("delete from $table where 1=1 $sqlwhere");
}
}
?>

參數(shù)的注釋都寫的很清楚,如果有人需要,不清楚使用方法可以直接問我。

php技術(shù)一個基于PDO的數(shù)據(jù)庫操作類,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 衡阳县| 张家口市| 库尔勒市| 札达县| 中牟县| 康保县| 东丽区| 九江市| 乌兰浩特市| 东乡| 富顺县| 临沭县| 吉木萨尔县| 喀喇| 徐汇区| 什邡市| 开远市| 南通市| 韶关市| 旺苍县| 黄石市| 德州市| 海伦市| 勃利县| 万全县| 荣昌县| 淳安县| 芦山县| 沙湾县| 广水市| 九江市| 青海省| 凉城县| 霍州市| 南宁市| 南投市| 延长县| 永昌县| 乌审旗| 土默特右旗| 卢湾区|