一個基于PDO的數(shù)據(jù)庫操作類
更新時間:2011年03月24日 00:37:59 作者:
工作一年以來,所做的項目使用的都是ADODB,但其的代碼臃腫和執(zhí)行效率低導致現(xiàn)在需要更換。
百度之后決定使用PDO,至于為什么選擇PDO,這里就不再多說,大家自己去百度下就能明白。
既然要換,那最基本就需要有個常用的數(shù)據(jù)庫操作類,也就是所謂的增刪改查等,昨晚搗騰了一晚,大致弄出了個雛形,以下就是代碼,希望大家能給出點意見。
<?php
/*
作者:胡睿
日期:2011/03/19
電郵:hooray0905@foxmail.com
20110319
常用數(shù)據(jù)庫操作,如:增刪改查,獲取單條記錄、多條記錄,返回最新一條插入記錄id,返回操作記錄行數(shù)等
*/
/*
參數(shù)說明
int $debug 是否開啟調試,開啟則輸出sql語句
int $getcount 是否記數(shù),返回值為行數(shù)
int $getrow 是否返回值單條記錄
string $table 數(shù)據(jù)庫表
string $fields 需要查詢的數(shù)據(jù)庫字段,允許為空,默認為查找全部
string $sqlwhere 查詢條件,允許為空
string $orderby 排序,允許為空,默認為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 是否開啟調試,開啟則輸出sql語句
int $execrow 是否開啟返回執(zhí)行條目數(shù)
int $lastinsertid 是否開啟返回最后一條插入記錄id
string $table 數(shù)據(jù)庫表
string $fields 需要插入數(shù)據(jù)庫的字段
string $values 需要插入數(shù)據(jù)庫的信息,必須與$fields一一對應
*/
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 是否開啟調試,開啟則輸出sql語句
int $execrow 是否開啟執(zhí)行并返回條目數(shù)
string $table 數(shù)據(jù)庫表
string $set 需要更新的字段及內容,格式: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 是否開啟調試,開啟則輸出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ù)的注釋都寫的很清楚,如果有人需要,不清楚使用方法可以直接問我。
既然要換,那最基本就需要有個常用的數(shù)據(jù)庫操作類,也就是所謂的增刪改查等,昨晚搗騰了一晚,大致弄出了個雛形,以下就是代碼,希望大家能給出點意見。
復制代碼 代碼如下:
<?php
/*
作者:胡睿
日期:2011/03/19
電郵:hooray0905@foxmail.com
20110319
常用數(shù)據(jù)庫操作,如:增刪改查,獲取單條記錄、多條記錄,返回最新一條插入記錄id,返回操作記錄行數(shù)等
*/
/*
參數(shù)說明
int $debug 是否開啟調試,開啟則輸出sql語句
int $getcount 是否記數(shù),返回值為行數(shù)
int $getrow 是否返回值單條記錄
string $table 數(shù)據(jù)庫表
string $fields 需要查詢的數(shù)據(jù)庫字段,允許為空,默認為查找全部
string $sqlwhere 查詢條件,允許為空
string $orderby 排序,允許為空,默認為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 是否開啟調試,開啟則輸出sql語句
int $execrow 是否開啟返回執(zhí)行條目數(shù)
int $lastinsertid 是否開啟返回最后一條插入記錄id
string $table 數(shù)據(jù)庫表
string $fields 需要插入數(shù)據(jù)庫的字段
string $values 需要插入數(shù)據(jù)庫的信息,必須與$fields一一對應
*/
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 是否開啟調試,開啟則輸出sql語句
int $execrow 是否開啟執(zhí)行并返回條目數(shù)
string $table 數(shù)據(jù)庫表
string $set 需要更新的字段及內容,格式: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 是否開啟調試,開啟則輸出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使用Header函數(shù),PHP_AUTH_PW和PHP_AUTH_USER做用戶驗證
這篇文章主要介紹了php使用Header函數(shù),PHP_AUTH_PW和PHP_AUTH_USER做用戶驗證的方法,結合實例形式分析了PHP使用Header函數(shù)調用登錄驗證及PHP_AUTH_PW和PHP_AUTH_USER進行驗證處理的相關技巧,需要的朋友可以參考下2016-05-05session在php5.3中的變化 session_is_registered() is deprecated in
在php 5.3中session_is_registered()已經(jīng)是放棄使用了,大家在使用過程中需要注意一下了2013-11-11PHP用mysql_insert_id()函數(shù)獲得剛插入數(shù)據(jù)或當前發(fā)布文章的ID
向mysql 插入數(shù)據(jù)時,很多時候我們想知道剛剛插入數(shù)據(jù)的id,這對我們很有用。下面這篇文章就詳細給大家介紹了利用mysql_insert_id()函數(shù)獲得剛插入數(shù)據(jù)或當前發(fā)布文章的ID,有需要的朋友們可以參考借鑒,感興趣的朋友們下面來一起看看吧。2016-11-11從一個不錯的留言本弄的mysql數(shù)據(jù)庫操作類
本文通過實例代碼給大家介紹了mysql數(shù)據(jù)庫操作類的相關知識,感興趣的朋友跟隨腳本之家小編一起看看吧2007-09-09