PHPPDO encapsulation static class code sharing

Source: Internet
Author: User
PHPPDO encapsulation static class code sharing

  1. /**
  2. * Class DB
  3. * Database operations
  4. */
  5. Class DB {
  6. /**
  7. * @ Var
  8. * @ Return CDB
  9. */
  10. Private static $ db;
  11. /** Obtain the CDb class
  12. * @ Param $ table_name table name
  13. * @ Param string $ db_setting call the database configuration item
  14. * @ Param array $ db_config database configuration
  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. /** Configuration
  27. * @ Param $ table_name table name
  28. * @ Param string $ db_setting call the database configuration item
  29. * @ Param array $ db_config database configuration
  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. * Delete a record
  37. * @ Param $ table name
  38. * @ Param $ condition: Delete a data condition. it cannot be blank. Can be an array
  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. * Add a record
  48. * @ Param $ table name
  49. * @ Param array $ data the data to be added. The parameter is an array. The array key is the field value, and the array value is the data value.
  50. * @ Param bool $ return_insert_id whether to return the new ID number
  51. * @ Param bool $ replace whether to use the replace into method to add data
  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. * Obtain the primary key number of the last added record
  61. * @ Return int
  62. */
  63. Public static function insertID (){
  64. $ Db = self: cdb ();
  65. Return $ db-> insert_id ();
  66. }
  67. /**
  68. * Perform the update record operation.
  69. * @ Param $ table name
  70. * @ Param $ data the data content to be updated. the parameter is an array.
  71. * If it is an array, the group key is the field value, and the array value is the data value.
  72. * [Example: array ('name' => 'lanmps ', 'password' => '123')]
  73. * Array ('name' => '+ = 1', 'base' =>'-= 1 '); the program will automatically resolve it to 'name' = 'name' + 1, 'base' = 'base'-1
  74. * String, in the following format:
  75. * String [Example 2: array ('catid =: catid AND time >=: time', array (': catid' => 10 ,': time '=> '2017-02-10')]
  76. * @ Param $ where: conditions for updating data,
  77. * String, in the following format:
  78. * String [Example 1: "id = 1 and time> $ time"]
  79. * String [Example 2: array ('catid =: catid AND time >=: time', array (': catid' => 10 ,': time '=> '2017-02-10')]
  80. * [Example: array ('name' => 'lanmps ', 'password' => '123')]
  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. * Query a single record
  90. * @ Param array $ SQL query condition statement
  91. * @ Return array/null: query result set. If no result exists, null is returned.
  92. */
  93. Public static function fetchFirst ($ SQL ){
  94. $ Db = self: cdb ();
  95. Return $ db-> fetch ($ SQL );
  96. }
  97. /**
  98. * Execute an SQL query
  99. * @ Param $ SQL query condition
  100. * @ Return array number Group of query result sets
  101. */
  102. Public static function fetchAll ($ SQL ){
  103. $ Db = self: cdb ();
  104. Return $ db-> fetchAll ($ SQL );
  105. }
  106. /**
  107. * Directly execute SQL queries
  108. * @ Param $ SQL query SQL statements
  109. * @ Return
  110. */
  111. Public static function query ($ SQL ){
  112. $ Db = self: cdb ();
  113. Return $ db-> exec ($ SQL );
  114. }
  115. /**
  116. * Execute an SQL query
  117. * @ Param $ table name
  118. * @ Param $ where query condition
  119. * String, in the following format:
  120. * String [Example 1: "id = 1 and time> $ time"]
  121. * String [Example 2: array ('catid =: catid AND time >=: time', array (': catid' => 10 ,': time '=> '2017-02-10')]
  122. * [Example: array ('name' => 'lanmps ', 'password' => '123')]
  123. * @ Param $ field value to be queried by fields [example: 'name', 'gender', 'birthday']
  124. * @ Param $ limit return result range [example: 10 or 10, 10 is blank by default]
  125. * @ Param $ order: Sort by database by default.
  126. * @ Param $ group: [null by default]
  127. * @ Return array number Group of query result sets
  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. * Query a single record
  136. * @ Param $ table name
  137. * @ Param array $ where query condition statement
  138. * String, in the following format:
  139. * String [Example 2: array ('catid =: catid AND time >=: time', array (': catid' => 10 ,': time '=> '2017-02-10')]
  140. * [Example: array ('name' => 'lanmps ', 'password' => '123')]
  141. * @ Param string $ field value to be queried by fields [example: 'name', 'gender', 'birthday']
  142. * @ Param string $ order: [sort by database by default]
  143. * @ Param string $ group: [null by default]
  144. * @ Return array/null: query result set. If no result exists, null is returned.
  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. * Query multiple data entries and pagination
  153. * @ Param $ table name
  154. * @ Param $ where query condition
  155. * String, in the following format:
  156. * String [Example 1: "id = 1 and time> $ time"]
  157. * String [Example 2: array ('catid =: catid AND time >=: time', array (': catid' => 10 ,': time '=> '2017-02-10')]
  158. * [Example: array ('name' => 'lanmps ', 'password' => '123')]
  159. * @ Param $ fields field *, id
  160. * @ Param $ order sort id desc, orderlist asc
  161. * @ Param $ page number 1
  162. * @ Param $ pagesize the number of entries per page
  163. * @ Return array ('data' => data, 'count' => Total Records)
  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. /** The first parameter value
  172. * @ Param $ SQL
  173. * @ Return mixed
  174. */
  175. Public static function resultFirst ($ SQL ){
  176. $ Db = self: cdb ();
  177. Return $ db-> resultFirst ($ SQL );
  178. }
  179. }

Call method:

DB: insert ('test', array ('name' => 'test '));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.