PHP PDO encapsulation static class code sharing

Source: Internet
Author: User
  1. /**
  2. * Class DB
  3. * Database Operation class
  4. */
  5. Class DB {
  6. /**
  7. * @var
  8. * @return CDB
  9. */
  10. private static $db;
  11. /** Get CDB class
  12. * @param $table _name table name
  13. * @param string $db _setting Call 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 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. * Perform Delete record operation
  37. * @param $table Table name
  38. * @param $condition Delete data conditions, not allowed to empty. You can set 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. * Perform add record operation
  48. * @param $table 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 add data using 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. * Gets the primary key number of the last record added
  61. * @return int
  62. */
  63. public static function Insertid () {
  64. $db =self::cdb ();
  65. return $db->insert_id ();
  66. }
  67. /**
  68. * Perform update record operation
  69. * @param $table Table name
  70. * @param $data The data content to be updated, the parameter is an array
  71. * Array key is a field value, array value is data value
  72. * array When [Example: Array (' name ' = ' lanmps ', ' password ' = ' 123456 ')]
  73. * An array of another use array (' name ' = ' = ' +=1 ', ' base ' = '-=1 '); the program automatically resolves to ' name ' = ' name ' + 1, ' base ' = ' base '-1
  74. * String, please follow the format:
  75. * String [Example 2:array (' Catid=:catid and Time>=:time ', Array (': CatID ' =>10, ': Time ' = ' 2012-02-10 '))]
  76. * @param $where conditions when updating data,
  77. * String, please follow the format:
  78. * String [Example 1: "Id=1 and time> $time"]
  79. * String [Example 2:array (' Catid=:catid and Time>=:time ', Array (': CatID ' =>10, ': Time ' = ' 2012-02-10 '))]
  80. * array When [Example: 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. * Get a single record query
  90. * @param array $sql query condition statement
  91. * @return array/null data Query result set, if not present, returns an empty
  92. */
  93. public static function Fetchfirst ($sql) {
  94. $db =self::cdb ();
  95. Return $db->fetch ($sql);
  96. }
  97. /**
  98. * Execute SQL query
  99. * @param $sql Query conditions
  100. * @return Array Query result set
  101. */
  102. public static function Fetchall ($sql) {
  103. $db =self::cdb ();
  104. Return $db->fetchall ($sql);
  105. }
  106. /**
  107. * Execute SQL query directly
  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 SQL query
  117. * @param $table Table name
  118. * @param $where Query conditions
  119. * String, please follow the format:
  120. * String [Example 1: "Id=1 and time> $time"]
  121. * String [Example 2:array (' Catid=:catid and Time>=:time ', Array (': CatID ' =>10, ': Time ' = ' 2012-02-10 '))]
  122. * array When [Example: Array (' name ' = ' = ' lanmps ', ' password ' = ' 123456 ')]
  123. * @param $fields field values to query [example ' name ', ' gender ', ' birthday ']
  124. * @param $limit return result range [Example: 10 or 10, 10 default is empty]
  125. * @param $order Sort By default by database
  126. * @param $group grouping method [default is empty]
  127. * @return Array Query result set
  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. * Get a single record query
  136. * @param $table Table name
  137. * @param array $where query condition statement
  138. * String, please follow the format:
  139. * String [Example 2:array (' Catid=:catid and Time>=:time ', Array (': CatID ' =>10, ': Time ' = ' 2012-02-10 '))]
  140. * array When [Example: Array (' name ' = ' = ' lanmps ', ' password ' = ' 123456 ')]
  141. * @param string $fields The field value to query [example ' name ', ' gender ', ' birthday ']
  142. * @param string $order Sort by default by database
  143. * @param string $group grouping method [default is empty]
  144. * @return array/null data Query result set, if not present, returns an empty
  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. * Querying multiple data and paging
  153. * @param $table Table name
  154. * @param $where Query conditions
  155. * String, please follow the format:
  156. * String [Example 1: "Id=1 and time> $time"]
  157. * String [Example 2:array (' Catid=:catid and Time>=:time ', Array (': CatID ' =>10, ': Time ' = ' 2012-02-10 '))]
  158. * array When [Example: Array (' name ' = ' = ' lanmps ', ' password ' = ' 123456 ')]
  159. * @param $fields Field *,id
  160. * @param $order Sort ID desc, orderlist ASC
  161. * @param $page Page 1
  162. * @param $pagesize number of pages per page
  163. * @return Array (' data ' = = The total number of records, ' 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. /** 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. }
Copy Code

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.