国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PDO的數據庫操作類

2024-09-04 11:44:15
字體:
來源:轉載
供稿:網友
  1. /* 
  2.   參數說明 
  3.   int   $debug   是否開啟調試,開啟則輸出sql語句 
  4.   int   $mode   0 返回數組 
  5.          1 返回單條記錄 
  6.          2 返回行數 
  7.   string  $table   數據庫表 
  8.   string  $fields   需要查詢的數據庫字段,允許為空,默認為查找全部 
  9.   string  $sqlwhere  查詢條件,允許為空 
  10.   string  $orderby  排序,允許為空,默認為id倒序 
  11.   */ 
  12.  function hrSelect($debug$mode$table$fields="*"$sqlwhere=""$orderby="id desc"){ 
  13.   global $pdo
  14.   if($debug){ 
  15.    if($mode == 2){ 
  16.     echo "select count(*) from $table where 1=1 $sqlwhere order by $orderby"
  17.    }elseif($mode == 1){ 
  18.     echo "select $fields from $table where 1=1 $sqlwhere"
  19.    }else
  20.     echo "select $fields from $table where 1=1 $sqlwhere order by $orderby"
  21.    } 
  22.    exit
  23.   }else
  24.    if($mode == 2){ 
  25.     $rs = $pdo->query("select count(*) from $table where 1=1 $sqlwhere order by $orderby"); 
  26.     $return = $rs->fetchColumn(); 
  27.    }elseif($mode == 1){ 
  28.     $rs = $pdo->query("select $fields from $table where 1=1 $sqlwhere"); 
  29.     $return = $rs->fetch(); 
  30.    }else
  31.     $rs = $pdo->query("select $fields from $table where 1=1 $sqlwhere order by $orderby"); 
  32.     $return = $rs->fetchAll(); 
  33.    } 
  34.    return $return
  35.   } 
  36.  } 
  37.   
  38.  /* 
  39.   參數說明 
  40.   int   $debug   是否開啟調試,開啟則輸出sql語句 
  41.   int   $mode   0 默認insert,無返回信息 
  42.          1 返回執行條目數 
  43.          2 返回最后一次插入記錄的id 
  44.   string  $table   數據庫表 
  45.   string  $fields   需要插入數據庫的字段 
  46.   string  $values   需要插入數據庫的信息,必須與$fields一一對應 
  47.  */ 
  48.  function hrInsert($debug$mode$table$fields$values){ 
  49.   global $pdo
  50.   if($debug){ 
  51.    echo "insert into $table ($fields) values ($values)"
  52.    exit
  53.   }else
  54.    if($mode == 2){ 
  55.     $return = $pdo->lastInsertId("insert into $table ($fields) values ($values)"); 
  56.    }elseif($mode == 1){ 
  57.     $return = $pdo->exec("insert into $table ($fields) values ($values)"); 
  58.    }else
  59.     $pdo->query("insert into $table ($fields) values ($values)"); 
  60.     exit
  61.    } 
  62.    return $return
  63.   } 
  64.  } 
  65.   
  66.  /* 
  67.   參數說明 
  68.   int   $debug   是否開啟調試,開啟則輸出sql語句 
  69.   int   $mode   0 默認update,無返回信息 
  70.          1 返回執行條目數 
  71.   string  $table   數據庫表 
  72.   string  $set   需要更新的字段及內容,格式:a='abc',b=2,c='2010-10-10 10:10:10' 
  73.   string  $sqlwhere  修改條件,允許為空 
  74.  */ 
  75.  function hrUpdate($debug$mode$table$set$sqlwhere=""){ 
  76.   global $pdo
  77.   if($debug){ 
  78.    echo "update $table set $set where 1=1 $sqlwhere"
  79.    exit
  80.   }else
  81.    if($mode==1){ 
  82.     $return = $pdo->exec("update $table set $set where 1=1 $sqlwhere"); 
  83.    }else
  84.     $pdo->query("update $table set $set where 1=1 $sqlwhere"); 
  85.     exit
  86.    } 
  87.    return $return
  88.   } 
  89.  } 
  90.   
  91.  /* 
  92.   參數說明 
  93.   int   $debug   是否開啟調試,開啟則輸出sql語句 
  94.   int   $mode   0 默認delete,無返回信息 
  95.          1 返回執行條目數 
  96.   string  $table   數據庫表 
  97.   string  $sqlwhere  刪除條件,允許為空 
  98.  */ 
  99.  function hrDelete($debug$mode$table$sqlwhere=""){ 
  100.   global $pdo
  101.   if($debug){ 
  102.    echo "delete from $table where 1=1 $sqlwhere"
  103.    exit
  104.   }else
  105.    if($mode == 1){ 
  106.     $return = $pdo->exec("delete from $table where 1=1 $sqlwhere"); 
  107.    }else
  108.     $pdo->query("delete from $table where 1=1 $sqlwhere"); 
  109.     exit
  110.    } 
  111.    return $return
  112.   } 
  113.  } 

 

另外一段代碼是基于我這個數據庫操作類的事務實例,注意,數據庫操作表類型必須為InnoDB,其他類型不支持事務.

PDO事務機制:

$pdo->beginTransaction(); --開啟事務

$pdo->commit();    --結束事務

$pdo->rollBack();   --回滾操作

示例,用try/catch包住db操作,當事務內的db操作出現中斷,則執行回滾并拋出異常信息,代碼如下:

  1. try{ 
  2.   $pdo->beginTransaction(); 
  3.   hrInsert(0,1,"class","name,parentid","'god',0"); //可以正常執行 
  4.   hrInsert(0,0,0,"tb_searchlog","userid,code","4"); //出錯 
  5.   $pdo->commit(); 
  6. //開源代碼Vevb.com 
  7.  }catch(Exception $e){ 
  8.   $pdo->rollBack(); 
  9.   echo "Failed: " . $e->getMessage(); 
  10.  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 尚义县| 彰化市| 贵州省| 武乡县| 长乐市| 镇雄县| 景宁| 图木舒克市| 宁都县| 利川市| 宝清县| 剑川县| 武宁县| 永寿县| 丰城市| 泸溪县| 英超| 鄂伦春自治旗| 门源| 兴和县| 遂昌县| 裕民县| 江永县| 绵阳市| 上高县| 刚察县| 应用必备| 佛学| 镇巴县| 樟树市| 滦南县| 瓮安县| 临安市| 荥经县| 山阴县| 余姚市| 阜平县| 乡城县| 沙坪坝区| 四川省| 孙吴县|