首頁  >  文章  >  後端開發  >  PHP資料庫操作類

PHP資料庫操作類

WBOY
WBOY原創
2016-07-25 09:12:27826瀏覽
  1. /*============================== ====================================*/
  2. /* 檔名:BaseLogic.class. php */
  3. /* 摘要: 資料處理公共類別. */
  4. class BaseLogic extends MyDB {
  5. protected $tabName; //表格的名稱
  6. protected $fieldList; //字段集合
  7. protected $messList;
  8. //================================== ========
  9. // 函數: add($postList)
  10. // 功能: 新增
  11. // 參數: $postList 提交的變數清單
  12. // 回傳: 剛插入的自增ID
  13. //========================================= =
  14. function add($postList) {
  15. $fieldList='';
  16. $value='';
  17. foreach ($postList as $k=>$v) {
  18. if( in_array($k, $this->fieldList)){
  19. $fieldList.=$k.",";
  20. if (!get_magic_quotes_gpc())
  21. $value .= "'".addslashes( $v)."',";
  22. else
  23. $value .= "'".$v."',";
  24. }
  25. }
  26. $fieldList=rtrimrim ($fieldList, ",");
  27. $value=rtrim($value, ",");
  28. $sql = "INSERT INTO {$this->tabName} (".$fieldList. ") VALUES(".$value.")";
  29. echo $sql;
  30. $result=$this->mysqli->query($sql);
  31. if($result && $this- >mysqli->affected_rows >0 )
  32. return $this->mysqli->insert_id;
  33. else
  34. return false; }
  35. //==== ======================================
  36. // 函式: mod($postList)
  37. // 功能: 修改表格資料
  38. // 參數: $postList 提交的變數清單
  39. //====================== ====================
  40. function mod($postList) {
  41. $id=$postList["id"];
  42. unset($ postList["id"]);
  43. $value='';
  44. foreach ($postList as $k=>$v) {
  45. if(in_array($k, $this->fieldList)) {
  46. if (!get_magic_quotes_gpc())
  47. $value .= $k." = '".addslashes($v)."',";
  48. else
  49. $value .= $k ." = '".$v."',";
  50. }
  51. }
  52. $value=rtrim($value, ",");
  53. $sql = "UPDATE {$this- >tabName} SET {$value} WHERE id={$id}";
  54. return $this->mysqli->query($sql);
  55. }
  56. //==== ======================================
  57. // 函式: del($id)
  58. // 功能: 刪除
  59. // 參數: $id 編號或ID清單陣列
  60. // 傳回: 0 失敗成功為刪除的記錄數
  61. //======== ==================================
  62. function del($id) {
  63. if(is_array ($id))
  64. $tmp = "IN (" . join(",", $id) . ")";
  65. else
  66. $tmp = "= $id";
  67. $sql = "DELETE FROM {$this->tabName} WHERE id " . $tmp ;
  68. return $this->mysqli->query($sql);
  69. }
  70. function get($id) {
  71. $sql = "SELECT * FROM {$this->tabName} WHERE id ={$id}";
  72. $result=$this- >mysqli->query($sql);
  73. if($result && $result->num_rows ==1){
  74. return $result->fetch_assoc();
  75. }else{
  76. return false;
  77. }
  78. }
  79. function getMessList(){
  80. $message="";
  81. if(!empty($this->messList)){
  82. foreach($this->messList as $value){
  83. $message.=$value."
    ";
  84. }
  85. }
  86. return $message;
  87. }
  88. }
  89. ?>
複製程式碼



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn