PHP PDO封裝靜態類代碼分享

來源:互聯網
上載者:User
  1. /**
  2. * Class DB
  3. * 資料庫操作類
  4. */
  5. class DB {
  6. /**
  7. * @var
  8. * @return CDB
  9. */
  10. private static $db;
  11. /**擷取CDb類
  12. * @param $table_name 表名稱
  13. * @param string $db_setting 調用資料庫配置項
  14. * @param array $db_config 資料庫配置
  15. * @return CDb
  16. */
  17. public static function cdb($table_name='',$db_setting='default',$db_config=array()){
  18. if(!isset(self::$db)){
  19. $db = new CDb($table_name,$db_setting,$db_config);
  20. self::$db=$db;
  21. }else{
  22. $db=self::$db;
  23. }
  24. return $db;
  25. }
  26. /** 配置
  27. * @param $table_name 表名稱
  28. * @param string $db_setting 調用資料庫配置項
  29. * @param array $db_config 資料庫配置
  30. * @return CDb
  31. */
  32. public static function init($table_name='',$db_setting='default',$db_config=array()) {
  33. return self::cdb($table_name,$db_setting,$db_config);
  34. }
  35. /**
  36. * 執行刪除記錄操作
  37. * @param $table 表名稱
  38. * @param $condition 刪除資料條件,不充許為空白。可以為數組
  39. * @return boolean
  40. */
  41. public static function delete($table, $condition) {
  42. $db=self::cdb();
  43. $db->setTableName($table);
  44. return $db->delete($condition);
  45. }
  46. /**
  47. * 執行添加記錄操作
  48. * @param $table 表名稱
  49. * @param array $data要增加的資料,參數為數組。數組key為欄位值,數組值為資料取值
  50. * @param bool $return_insert_id 是否返回建立ID號
  51. * @param bool $replace 是否採用 replace into的方式添加資料
  52. * @return boolean
  53. */
  54. public static function insert($table, $data, $return_insert_id = false, $replace = false) {
  55. $db=self::cdb();
  56. $db->setTableName($table);
  57. return $db->insert($data, $return_insert_id, $replace);
  58. }
  59. /**
  60. * 擷取最後一次添加記錄的主鍵號
  61. * @return int
  62. */
  63. public static function insertID() {
  64. $db=self::cdb();
  65. return $db->insert_id();
  66. }
  67. /**
  68. * 執行更新記錄操作
  69. * @param $table 表名稱
  70. * @param $data 要更新的資料內容,參數為數組
  71. * 為數組時數組key為欄位值,數組值為資料取值
  72. * 為數組時[例: array('name'=>'lanmps','password'=>'123456')]
  73. * 數組的另一種使用array('name'=>'+=1', 'base'=>'-=1');程式會自動解析為`name` = `name` + 1, `base` = `base` - 1
  74. * 字串,請按照格式 :
  75. * 字串 [例2:array('catid=:catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
  76. * @param $where 更新資料時的條件,
  77. * 字串,請按照格式 :
  78. * 字串 [例1:" id=1 and time>$time " ]
  79. * 字串 [例2:array('catid=:catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
  80. * 數組時 [例: array('name'=>'lanmps','password'=>'123456')]
  81. * @return boolean
  82. */ bbs.it-home.org
  83. public static function update($table, $data, $where) {
  84. $db=self::cdb();
  85. $db->setTableName($table);
  86. return $db->update($data,$where);
  87. }
  88. /**
  89. * 擷取單條記錄查詢
  90. * @param array $SQL 查詢條件陳述式
  91. * @return array/null資料查詢結果集,如果不存在,則返回空
  92. */
  93. public static function fetchFirst($sql) {
  94. $db=self::cdb();
  95. return $db->fetch($sql);
  96. }
  97. /**
  98. * 執行sql查詢
  99. * @param $sql查詢條件
  100. * @return array 查詢結果集數組
  101. */
  102. public static function fetchAll($sql) {
  103. $db=self::cdb();
  104. return $db->fetchAll($sql);
  105. }
  106. /**
  107. * 直接執行sql查詢
  108. * @param $sql 查詢sql語句
  109. * @return
  110. */
  111. public static function query($sql) {
  112. $db=self::cdb();
  113. return $db->exec($sql);
  114. }
  115. /**
  116. * 執行sql查詢
  117. * @param $table 表名稱
  118. * @param $where 查詢條件
  119. * 字串,請按照格式 :
  120. * 字串 [例1:" id=1 and time>$time " ]
  121. * 字串 [例2:array('catid=:catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
  122. * 數組時 [例: array('name'=>'lanmps','password'=>'123456')]
  123. * @param $fields需要查詢的欄位值[例`name`,`gender`,`birthday`]
  124. * @param $limit 返回結果範圍[例:10或10,10 預設為空白]
  125. * @param $order 排序方式 [預設按資料庫預設排序]
  126. * @param $group 分組方式 [預設為空白]
  127. * @return array 查詢結果集數組
  128. */
  129. public static function select($table,$where = '', $fields = '*', $limit = '', $order = '', $group = '') {
  130. $db=self::cdb();
  131. $db->setTableName($table);
  132. return $db->select($where , $fields , $limit, $order , $group);
  133. }
  134. /**
  135. * 擷取單條記錄查詢
  136. * @param $table 表名稱
  137. * @param array $where查詢條件陳述式
  138. * 字串,請按照格式 :
  139. * 字串 [例2:array('catid=:catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
  140. * 數組時 [例: array('name'=>'lanmps','password'=>'123456')]
  141. * @param string $fields 需要查詢的欄位值[例`name`,`gender`,`birthday`]
  142. * @param string $order 排序方式 [預設按資料庫預設排序]
  143. * @param string $group 分組方式 [預設為空白]
  144. * @return array/null資料查詢結果集,如果不存在,則返回空
  145. */
  146. public static function getOne($table,$where,$fields = '*', $order = '', $group = '') {
  147. $db=self::cdb();
  148. $db->setTableName($table);
  149. return $db->get_one($where , $fields,$order, $group);
  150. }
  151. /**
  152. * 查詢多條資料並分頁
  153. * @param $table 表名稱
  154. * @param $where 查詢條件
  155. * 字串,請按照格式 :
  156. * 字串 [例1:" id=1 and time>$time " ]
  157. * 字串 [例2:array('catid=:catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
  158. * 數組時 [例: array('name'=>'lanmps','password'=>'123456')]
  159. * @param $fields欄位 *,id
  160. * @param $order 排序 id desc ,orderlist asc
  161. * @param $page 頁碼 1
  162. * @param $pagesize 每頁條數
  163. * @return array('data'=>資料,'count'=>記錄總數)
  164. */
  165. public static function listInfo($table,$where = '',$fields='*', $order = '', $page = 1, $pagesize = 20) {
  166. $db=self::cdb();
  167. $db->setTableName($table);
  168. $d=$db->listinfo($where,$fields, $order, $page, $pagesize);
  169. return array('data'=>$d,'count'=>self::$db->number);
  170. }
  171. /**第一個參數值
  172. * @param $sql
  173. * @return mixed
  174. */
  175. public static function resultFirst($sql){
  176. $db=self::cdb();
  177. return $db->resultFirst($sql);
  178. }
  179. }
複製代碼

調用方法:

DB::insert('test',array('name'=>'test'));
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.