一個(gè)基于PDO的數(shù)據(jù)庫(kù)操作類
既然要換,那最基本就需要有個(gè)常用的數(shù)據(jù)庫(kù)操作類,也就是所謂的增刪改查等,昨晚?yè)v騰了一晚,大致弄出了個(gè)雛形,以下就是代碼,希望大家能給出點(diǎn)意見(jiàn)。
<?php
/*
作者:胡睿
日期:2011/03/19
電郵:hooray0905@foxmail.com
20110319
常用數(shù)據(jù)庫(kù)操作,如:增刪改查,獲取單條記錄、多條記錄,返回最新一條插入記錄id,返回操作記錄行數(shù)等
*/
/*
參數(shù)說(shuō)明
int $debug 是否開(kāi)啟調(diào)試,開(kāi)啟則輸出sql語(yǔ)句
int $getcount 是否記數(shù),返回值為行數(shù)
int $getrow 是否返回值單條記錄
string $table 數(shù)據(jù)庫(kù)表
string $fields 需要查詢的數(shù)據(jù)庫(kù)字段,允許為空,默認(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ù)說(shuō)明
int $debug 是否開(kāi)啟調(diào)試,開(kāi)啟則輸出sql語(yǔ)句
int $execrow 是否開(kāi)啟返回執(zhí)行條目數(shù)
int $lastinsertid 是否開(kāi)啟返回最后一條插入記錄id
string $table 數(shù)據(jù)庫(kù)表
string $fields 需要插入數(shù)據(jù)庫(kù)的字段
string $values 需要插入數(shù)據(jù)庫(kù)的信息,必須與$fields一一對(duì)應(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ù)說(shuō)明
int $debug 是否開(kāi)啟調(diào)試,開(kāi)啟則輸出sql語(yǔ)句
int $execrow 是否開(kāi)啟執(zhí)行并返回條目數(shù)
string $table 數(shù)據(jù)庫(kù)表
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ù)說(shuō)明
int $debug 是否開(kāi)啟調(diào)試,開(kāi)啟則輸出sql語(yǔ)句
int $execrow 是否開(kāi)啟返回執(zhí)行條目數(shù)
string $table 數(shù)據(jù)庫(kù)表
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ù)的注釋都寫(xiě)的很清楚,如果有人需要,不清楚使用方法可以直接問(wèn)我。
相關(guān)文章
php使用Header函數(shù),PHP_AUTH_PW和PHP_AUTH_USER做用戶驗(yàn)證
這篇文章主要介紹了php使用Header函數(shù),PHP_AUTH_PW和PHP_AUTH_USER做用戶驗(yàn)證的方法,結(jié)合實(shí)例形式分析了PHP使用Header函數(shù)調(diào)用登錄驗(yàn)證及PHP_AUTH_PW和PHP_AUTH_USER進(jìn)行驗(yàn)證處理的相關(guān)技巧,需要的朋友可以參考下2016-05-05session在php5.3中的變化 session_is_registered() is deprecated in
在php 5.3中session_is_registered()已經(jīng)是放棄使用了,大家在使用過(guò)程中需要注意一下了2013-11-11PHP 之 寫(xiě)時(shí)復(fù)制介紹(Copy On Write)
很多時(shí)候,我們會(huì)因?yàn)橐恍┬g(shù)語(yǔ)而對(duì)其概念產(chǎn)生莫測(cè)高深的恐懼,而其實(shí),他們的基本原理往往非常簡(jiǎn)單。本小節(jié)將介紹PHP中寫(xiě)時(shí)復(fù)制這種策略的實(shí)現(xiàn)2014-05-05PHP用mysql_insert_id()函數(shù)獲得剛插入數(shù)據(jù)或當(dāng)前發(fā)布文章的ID
向mysql 插入數(shù)據(jù)時(shí),很多時(shí)候我們想知道剛剛插入數(shù)據(jù)的id,這對(duì)我們很有用。下面這篇文章就詳細(xì)給大家介紹了利用mysql_insert_id()函數(shù)獲得剛插入數(shù)據(jù)或當(dāng)前發(fā)布文章的ID,有需要的朋友們可以參考借鑒,感興趣的朋友們下面來(lái)一起看看吧。2016-11-11從一個(gè)不錯(cuò)的留言本弄的mysql數(shù)據(jù)庫(kù)操作類
本文通過(guò)實(shí)例代碼給大家介紹了mysql數(shù)據(jù)庫(kù)操作類的相關(guān)知識(shí),感興趣的朋友跟隨腳本之家小編一起看看吧2007-09-09php基礎(chǔ)知識(shí):類與對(duì)象(2) 自動(dòng)加載對(duì)象
php基礎(chǔ)知識(shí):類與對(duì)象(2) 自動(dòng)加載對(duì)象...2006-12-126種解決PHP Trait屬性沖突問(wèn)題的方法小結(jié)
在PHP中,Trait是一種用于在類之間共享方法的方法,然而,Trait中的成員屬性可能會(huì)導(dǎo)致沖突,所以本文為大家整理了6種解決Trait屬性沖突的方法,希望對(duì)大家有所幫助2024-02-02