首頁 > 後端開發 > php教程 > 關於CI框架常用的函數封裝

關於CI框架常用的函數封裝

不言
發布: 2023-04-01 07:28:01
原創
2089 人瀏覽過

這篇文章主要介紹了CI框架常用函數封裝,結合實例形式分析了CI框架常用的資料庫查詢、插入、刪除、更新及debug調試等操作技巧,需要的朋友可以參考下

本文實例講述了CI框架常用函數封裝。分享給大家供大家參考,如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

/**

* 封装查询函数

*/

public function get_what($table='',$where=array(),$fields = ' * '){

    if( '' == $table ){

      return false;

    }

    //查询并返回相关结果

    $query = $this->db->select($fields)->where($where)->get($table);

    $res = $query->result_array();

    return $res;

}

/**

* 封装单条查询函数

*/

public function get_row($table='',$where=array(),$fields = ' * '){

    if( '' == $table ){

      return false;

    }

    //查询并返回相关结果

    $query = $this->db->select($fields)->where($where)->get($table);

    $res = $query->row_array();

    return $res;

}

/**

* 封装更新函数

*/

public function update_what($table='', $where=array(), $data = array()){

    if('' == $table || true === empty($where) || true === empty($data)){

      return false;

    }

    //更新相应的字段

    $query = $this->db->update($table,$data,$where);

    return $query;

}

/**

* 扩展数据库函数之自增 自减

* using:

* $table = 'codeuser';

$where = array('id'=>1);

$data = array('usestate'=>'usestate+1','imgtype' => 'imgtype-1');

*/

public function update_count($table = '', $where=array(), $data=array()){

     //如果表名为空 或者数据为空则直接 返回false

     if('' == $table || empty($data)){

       return false;

     }

     foreach($data as $key => $val){

       if(false !== stripos($val,'+') || false !== stripos($val,'-')){

         $this->db->set($key, $val, FALSE);

       }else{

         $this->db->set($key, $val);

       }

     }

     $res = $this->db->where($where)->update($table);

     return $res;

}

/**

* 封装插入函数

*/

public function insert_what($table = '', $data = array()){

    if('' == $table || true === empty($data)){

      return false;

    }

    //插入 相关记录

    $query = $this->db->insert($table, $data);

    return $query;

}

/**

* 删除记录封装函数

*/

public function delete_what($table = '', $where=array()){

    if(true === empty($where) || '' == $table){

      return false;

    }

    //删除相关表记录

    $query = $this->db->delete($table,$where);

    return $query;

}

/**

* debug 相关函数

*/

 public function debug_what($org_error = ''){

    $con = $this->router->fetch_class();

    $func = $this->router->fetch_method();

    if($org_error){

      $error .= date("Y-m-d H:i:s",time())."\r\n";

      $error .= __FILE__."\r\n";

      $error .= $con." 控制器下的:\r\n";

      $error .= $func." 方法调试信息如下:\r\n";

      $error .= $org_error;file_put_contents("./error_log.txt",$error."\r\n",FILE_APPEND);

    }

}

登入後複製

以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!

相關推薦:

關於CI框架封裝常用的映像處理方法

關於CI框架操作redis的方法

關於CI框架擴充系統核心類別的方法分析

#

以上是關於CI框架常用的函數封裝的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板