欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP数据库操作类

程序员文章站 2022-05-29 20:33:57
...
  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=rtrim($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. //==========================================
  37. // 函数: mod($postList)
  38. // 功能: 修改表数据
  39. // 参数: $postList 提交的变量列表
  40. //==========================================
  41. function mod($postList) {
  42. $id=$postList["id"];
  43. unset($postList["id"]);
  44. $value='';
  45. foreach ($postList as $k=>$v) {
  46. if(in_array($k, $this->fieldList)){
  47. if (!get_magic_quotes_gpc())
  48. $value .= $k." = '".addslashes($v)."',";
  49. else
  50. $value .= $k." = '".$v."',";
  51. }
  52. }
  53. $value=rtrim($value, ",");
  54. $sql = "UPDATE {$this->tabName} SET {$value} WHERE id={$id}";
  55. return $this->mysqli->query($sql);
  56. }
  57. //==========================================
  58. // 函数: del($id)
  59. // 功能: 删除
  60. // 参数: $id 编号或ID列表数组
  61. // 返回: 0 失败 成功为删除的记录数
  62. //==========================================
  63. function del($id) {
  64. if(is_array($id))
  65. $tmp = "IN (" . join(",", $id) . ")";
  66. else
  67. $tmp = "= $id";
  68. $sql = "DELETE FROM {$this->tabName} WHERE id " . $tmp ;
  69. return $this->mysqli->query($sql);
  70. }
  71. function get($id) {
  72. $sql = "SELECT * FROM {$this->tabName} WHERE id ={$id}";
  73. $result=$this->mysqli->query($sql);
  74. if($result && $result->num_rows ==1){
  75. return $result->fetch_assoc();
  76. }else{
  77. return false;
  78. }
  79. }
  80. function getMessList(){
  81. $message="";
  82. if(!empty($this->messList)){
  83. foreach($this->messList as $value){
  84. $message.=$value."
    ";
  85. }
  86. }
  87. return $message;
  88. }
  89. }
  90. ?>
复制代码

相关标签: PHP数据库操作类