欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

自寫的利用PDO對mysql數(shù)據(jù)庫增刪改查操作類

 更新時間:2018年02月19日 09:46:56   作者:劉志遠  
這篇文章主要給大家介紹了關(guān)于自寫的利用PDO對mysql數(shù)據(jù)庫的增刪改查操作類的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

前言

PDO一是PHP數(shù)據(jù)對象(PHP Data Object)的縮寫。

并不能使用PDO擴展本身執(zhí)行任何數(shù)據(jù)庫操作,必須使用一個database-specific PDO driver(針對特定數(shù)據(jù)庫的PDO驅(qū)動)訪問數(shù)據(jù)庫服務(wù)器。

PDO并不提供數(shù)據(jù)庫抽象,它并不會重寫SQL或提供數(shù)據(jù)庫本身缺失的功能,如果你需要這種功能,你需要使用一個更加成熟的抽象層。

最近在做項目時用到了PDO操作mysql數(shù)據(jù)庫,于是自己寫了一個類文件,命名為mysql_class.php文件代碼如下:

示例代碼

<?php
class mysql{
 //常量聲明
 const DSN = "mysql:host=[數(shù)據(jù)庫地址];dbname=[數(shù)據(jù)庫名];charset=utf8";//數(shù)據(jù)庫地址與數(shù)據(jù)庫名及編碼
 const USERNAME = "[數(shù)據(jù)庫用戶名]";//用戶名
 const PASSWD = "[數(shù)據(jù)庫密碼]";//密碼
 
 //私有變量聲明
 private $sql = NULL;//sql語句緩存
 private $link = NULL;//數(shù)據(jù)庫連接
 private $result = NULL;//結(jié)果
 
 /*******************************************************************************
  * @ 名稱:建立連接
  * @ 屬性:私有
 *******************************************************************************/
 private function connect(){
  try {
   $this->link = new \PDO(self::DSN, self::USERNAME, self::PASSWD, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));//創(chuàng)建連接
  }
  catch (PDOException $e) {
   die ("Error!:".$e->getMessage()."<hr/>");
  }
 }
 
 /*******************************************************************************
  * @ 名稱:執(zhí)行sql語句
  * @ 屬性:私有
  * @ 入口參數(shù):無
  * @ 出口參數(shù):執(zhí)行成功返回真,否則返回假,查詢語句存儲結(jié)果集數(shù)組
 *******************************************************************************/
 private function sql(){
  try {
   $this->connect();
   $this->link->beginTransaction();//開啟一個事務(wù)
   $prepare = $this->link->prepare($this->sql);//準備查詢語句
   $prepare->execute();//執(zhí)行查詢語句并返回結(jié)果集
   $cmd = strtolower(substr(trim($this->sql),0,6));//截取命令字符
   if($cmd == "select"){
    $array = $prepare->fetch(PDO::FETCH_ASSOC);//獲取結(jié)果集中的所有數(shù)據(jù)
    if(count($array)){
     $this->result = NULL;
     $this->result = $array;//存儲結(jié)果集
     return true;//查詢到結(jié)果返回真
    }else{
     return false;//否則返回假
    }
   }else if($cmd == "insert" || $cmd == "delete" || $cmd == "update"){
    if($prepare){
     return true;//執(zhí)行成功返回真
    }else{
     return false;//否則返回假
    }
   }
   $this->link->commit(); //如果正確執(zhí)行完成那么確認commit
  } catch (PDOException $e) {
   $this->link->rollBack();//如果執(zhí)行中有錯誤的情況下回滾
   die ("Error!:".$e->getMessage()."<hr/>");
  }
 }
 
 /*******************************************************************************
  * @ 名稱:sql語句處理
  * @ 屬性:公有
  * @ 入口參數(shù):cmd增刪改查字符命令;dsname數(shù)據(jù)表名;first第一個參數(shù);second第二個參數(shù);
  * @ 出口參數(shù):執(zhí)行成功返回真,否則返回假,查詢操作返回結(jié)果集數(shù)組
  * @ 使用示例:
  $mysql->handle("insert","abc","openid,nickname","'123','abc'");//增加
  $mysql->handle("delete","abc","openid='123'");//刪除
  $mysql->handle("update","abc","nickname='def'","openid='123'");//更新
  $res = $mysql->handle("select","abc","*","openid='123'");//查詢
  if(is_array($res) == true){
   foreach($res as $key=>$val){
    echo $key."=".$val."<hr>";
   }
  }//遍歷查詢結(jié)果數(shù)組
 *******************************************************************************/
 public function handle($cmd,$dsname,$first,$second=NULL){
  switch($cmd){
   case 'insert'://插入
    $this->sql = "insert into $dsname ($first) values ($second)";
    break;
   case 'delete'://刪除
    $this->sql = "delete from $dsname where $first";
    break;
   case 'update'://更新
    $this->sql = "update $dsname set $first where $second";
    break;
   case 'select'://查詢
    $this->sql = "select $first from $dsname where $second";
    break;
   default:
    die ("Syntax Error!");//提示語法錯誤
    break;
  }
  
  $res = $this->sql();//執(zhí)行sql語句
  if($res){
   if($cmd == 'select'){
    return $this->result;//返回查詢結(jié)果
   }else{
    return true;//執(zhí)行成功返回真
   }
  }else{
   return false;//否則返回假
  }
  $this->link=NULL;;//關(guān)閉數(shù)據(jù)庫
 }
}
$mysql = new mysql;//數(shù)據(jù)庫類的實例化
?>

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論