php封裝的pdo數(shù)據(jù)庫操作工具類與用法示例_第1頁
php封裝的pdo數(shù)據(jù)庫操作工具類與用法示例_第2頁
php封裝的pdo數(shù)據(jù)庫操作工具類與用法示例_第3頁
php封裝的pdo數(shù)據(jù)庫操作工具類與用法示例_第4頁
php封裝的pdo數(shù)據(jù)庫操作工具類與用法示例_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

第php封裝的pdo數(shù)據(jù)庫操作工具類與用法示例本文實例講述了php封裝的pdo數(shù)據(jù)庫操作工具類與用法。分享給大家供大家參考,具體如下:

header("Content-Type:text/html;charset=utf-8");

classPdoMysql{

publicstatic$config=array();//設(shè)置連接參數(shù),配置信息

publicstatic$link=null;//保存連接標(biāo)識符

publicstatic$pconnect=false;//是否開啟長連接

publicstatic$dbVersion=null;//保存數(shù)據(jù)庫版本

publicstatic$connected=false;//判斷是否連接成功

publicstatic$PDOStatement=null;//保證PDOStatement對象

publicstatic$queryStr=null;//保存最后執(zhí)行的操作

publicstatic$error=null;//保存錯誤信息

publicstatic$lastInsertId=null;//保存上一步插入操作保存的AUTO_INCREMANT

publicstatic$numRows=null;//受影響記錄的條數(shù)

*構(gòu)造函數(shù),連接數(shù)據(jù)庫

*@paramarray|string$dbConfigThedatabaseconfiguration

*@returnboolean(description_of_the_return_value)

publicfunction__construct($dbConfig=''){

if(!class_exists("PDO")){

self::throw_exception("不支持PDO,請先開啟");

if(!is_array($dbConfig)){

$dbConfig=array(

'hostname'='localhost',

'username'='root',

'password'='1234',

'database'='test',

'hostport'='3306',

'dbms'='mysql',

'dsn'='mysql:host=localhost;dbname=test'

if(empty($dbConfig['hostname'])){

self::throw_exception("沒有定義數(shù)據(jù)庫配置,請先定義");

self::$config=$dbConfig;

if(empty(self::$config['params'])){

self::$config['params']=array();

if(!isset(self::$link)){

$configs=self::$config;

if(self::$pconnect){

//開啟長連接,添加到配置數(shù)組中

$configs['params'][constant("PDO::ATTR_PERSISTENT")]=true;

try{

self::$link=newPDO($configs['dsn'],$configs['username'],$configs['password'],$configs['params']);

}catch(PDOException$e){

self::throw_exception($e-getMessage());

if(!self::$link){

self::throw_exception("PDO連接錯誤");

returnfalse;

self::$link-exec("setnamesutf8");

self::$dbVersion=self::$link-getAttribute(constant("PDO::ATTR_SERVER_VERSION"));

unset($configs);

*得到所有記錄

*@paramtype$sqlThesql

*@returntypeAll.

publicstaticfunctiongetAll($sql=null){

if($sql!=null){

self::query($sql);

$result=self::$PDOStatement-fetchAll(constant("PDO::FETCH_ASSOC"));

return$result;

*得到一條記錄

*@paramtype$sqlThesql

*@returntypeTherow.

publicstaticfunctiongetRow($sql=null){

if($sql!=null){

self::query($sql);

$result=self::$PDOStatement-fetch(constant("PDO::FETCH_ASSOC"));

return$result;

*執(zhí)行增刪改操作,返回受影響記錄的條數(shù)

*@paramtype$sqlThesql

*@returnboolean(description_of_the_return_value)

publicstaticfunctionexecute($sql=null){

$link=self::$link;

if(!$link)returnfalse;

if($sql!=null){

self::$queryStr=$sql;

if(!empty(self::$PDOStatement))self::free();

$result=$link-exec(self::$queryStr);

self::haveErrorThrowException();

if($result){

self::$lastInsertId=$link-lastInsertId();

self::$numRows=$result;

return$result;

}else{

returnfalse;

*根據(jù)主鍵查找記錄

*@paramtype$tabNameThetabname

*@paramtype$priIdThepriidentifier

*@paramstring$fieldsThefields

*@returntype(description_of_the_return_value)

publicstaticfunctionfindById($tabName,$priId,$fields='*'){

$sql='SELECT%sFROM%sWHEREid=%d';

returnself::getRow(sprintf($sql,self::parseFields($fields),$tabName,$priId));

*執(zhí)行普通查詢

*@paramtype$tablesThetables

*@paramtype$whereThewhere

*@paramstring$fieldsThefields

*@paramtype$groupThegroup

*@paramtype$havingThehaving

*@paramtype$orderTheorder

*@paramtype$limitThelimit

*@returntype(description_of_the_return_value)

publicstaticfunctionfind($tables,$where=null,$fields='*',$group=null,$having=null,$order=null,$limit

=null){

$sql='SELECT'.self::parseFields($fields).'FROM'.$tables

.self::parseWhere($where)

.self::parseGroup($group)

.self::parseHaving($having)

.self::parseOrder($order)

.self::parseLimit($limit);

$data=self::getAll($sql);

return$data;

*添加記錄

*@paramtype$dataThedata

*@paramtype$tableThetable

*@returntype(description_of_the_return_value)

publicstaticfunctionadd($data,$table){

$keys=array_keys($data);

array_walk($keys,array('PdoMySQL','addSpecialChar'));

$fieldsStr=join(',',$keys);

$values="'".join("','",array_values($data))."'";

$sql="INSERT{$table}({$fieldsStr})VALUES({$values})";

returnself::execute($sql);

*更新數(shù)據(jù)

*@paramtype$dataThedata

*@paramtype$tableThetable

*@paramtype$whereThewhere

*@paramtype$orderTheorder

*@paramtype$limitThelimit

publicstaticfunctionupdate($data,$table,$where=null,$order=null,$limit=null){

$sets='';

foreach($dataas$key=$value){

$sets.=$key."='".$value."',";

$sets=rtrim($sets,',');

$sql="UPDATE{$table}SET{$sets}".self::parseWhere($where).self::parseOrder($order).self::parseLimit($limit);

echo$sql;

*刪除數(shù)據(jù)

*@paramtype$dataThedata

*@paramtype$tableThetable

*@paramtype$whereThewhere

*@paramtype$orderTheorder

*@paramtype$limitThelimit

*@returntype(description_of_the_return_value)

publicstaticfunctiondelete($table,$where=null,$order=null,$limit=null){

$sql="DELETEFROM{$table}".self::parseWhere($where).self::parseOrder($order).self::parseLimit($limit);

returnself::execute($sql);

*執(zhí)行查詢

*@paramstring$sqlThesql

*@returnboolean(description_of_the_return_value)

publicstaticfunctionquery($sql=''){

$link=self::$link;

if(!$link)returnfalse;

//判斷之前是否有結(jié)果集,如果有的話,釋放結(jié)果集

if(!empty(self::$PDOStatement))self::free();

self::$queryStr=$sql;

self::$PDOStatement=$link-prepare(self::$queryStr);

$res=self::$PDOStatement-execute();

self::haveErrorThrowException();

return$res;

*獲取最后執(zhí)行的sql

*@returnbooleanThelastsql.

publicstaticfunctiongetLastSql(){

$link=self::$link;

if(!$link){

returnfalse;

returnself::$queryStr;

*獲取最后插入的ID

*@returnbooleanThelastinsertidentifier.

publicstaticfunctiongetLastInsertId(){

$link=self::$link;

if(!$link){

returnfalse;

returnself::$lastInsertId;

*獲得數(shù)據(jù)庫的版本

*@returnbooleanThedatabaseversion.

publicstaticfunctiongetDbVersion(){

$link=self::$link;

if(!$link){

returnfalse;

returnself::$dbVersion;

*得到數(shù)據(jù)庫中表

*@returnarray(description_of_the_return_value)

publicstaticfunctionshowTables(){

$tables=array();

if(self::query("showtables")){

$result=self::getAll();

foreach($resultas$key=$value){

$tables[$key]=current($value);

return$tables;

*解析where條件

*@paramtype$whereThewhere

*@returntype(description_of_the_return_value)

publicstaticfunctionparseWhere($where){

$whereStr='';

if(is_string($where)!empty($where)){

$whereStr=$where;

returnempty($whereStr)'':'WHERE'.$whereStr;

*解析group

*@paramtype$groupThegroup

*@returntype(description_of_the_return_value)

publicstaticfunctionparseGroup($group){

$groupStr='';

if(is_array($group)){

$groupStr=implode(',',$group);

}elseif(is_string($group)!empty($group)){

$groupStr=$group;

returnempty($groupStr)'':'GROUPBY'.$groupStr;

*解析having

*@paramtype$havingThehaving

*@returntype(description_of_the_return_value)

publicstaticfunctionparseHaving($having){

$havingStr='';

if(is_string($having)!empty($having)){

$havingStr=$having;

returnempty($havingStr)'':'HAVING'.$havingStr;

*解析order

*@paramtype$orderTheorder

*@returntype(description_of_the_return_value)

publicstaticfunctionparseOrder($order){

$orderStr='';

if(is_string($order)!empty($order)){

$orderStr=$order;

returnempty($orderStr)'':'ORDERBY'.$orderStr;

*解析limit

*@paramtype$limitThelimit

*@returntype(description_of_the_return_value)

publicstaticfunctionparseLimit($limit){

$limitStr='';

if(is_array($limit)){

$limitStr=implode(',',$limit);

}elseif(is_string($limit)!empty($limit)){

$limitStr=$limit;

returnempty($limitStr)'':'LIMIT'.$limitStr;

*解析字段

*@paramtype$fieldsThefields

*@returnstring(description_of_the_return_value)

publicstaticfunctionparseFields($fields){

if(is_array($fields)){

array_walk($fields,array('PdoMySQL','addSpecialChar'));

$fieldsStr=implode(',',$fields);

}elseif(is_string($fields)!(empty($fields))){

if(strpos($fields,'`')===false){

$fields=explode(',',$fields);

array_walk($fields,array('PdoMySQL','addSpecialChar'));

$fieldsStr=implode(',',$fields);

}else{

$fieldsStr=$fields;

}else{

$fieldsStr="*";

return$fieldsStr;

*通過反引號引用字字段

*@paramstring$valueThevalue

*@returnstring(description_of_the_return_value)

publicstaticfunctionaddSpecialChar($value){

if($value==="*"||strpos($value,'.')!==false||strpos($value,'`')!==false){

//不用做處理

}elseif(strpos($value,'`')===false){

$value='`'.trim($value).'`';

return$value;

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論