PDO database Operation class

Source: Internet
Author: User
  1. Class hrdb{
  2. protected $pdo;
  3. protected $res;
  4. protected $config;
  5. /* Constructor function */
  6. function __construct ($config) {
  7. $this->config = $config;
  8. $this->connect ();
  9. }
  10. /* Database connection */
  11. Public Function connect () {
  12. $this->pdo = new PDO ($this->config[' DSN '), $this->config[' name '], $this->config[' password ']);
  13. $this->pdo->query (' Set names utf8; ');
  14. Serialize the results into Stdclass
  15. $this->pdo->setattribute (Pdo::attr_default_fetch_mode, pdo::fetch_obj);
  16. Write your own code capture exception
  17. $this->pdo->setattribute (Pdo::attr_errmode, pdo::errmode_exception);
  18. }
  19. /* Database off */
  20. Public function Close () {
  21. $this->pdo = null;
  22. }
  23. Public Function Query ($sql) {
  24. $res = $this->pdo->query ($sql);
  25. if ($res) {
  26. $this->res = $res;
  27. }
  28. }
  29. Public function exec ($sql) {
  30. $res = $this->pdo->exec ($sql);
  31. if ($res) {
  32. $this->res = $res;
  33. }
  34. }
  35. Public Function Fetchall () {
  36. return $this->res->fetchall ();
  37. }
  38. Public function fetch () {
  39. return $this->res->fetch ();
  40. }
  41. Public Function Fetchcolumn () {
  42. return $this->res->fetchcolumn ();
  43. }
  44. Public Function Lastinsertid () {
  45. return $this->res->lastinsertid ();
  46. }
  47. /**
  48. * Parameter Description
  49. * int $debug whether to turn on debug, output SQL statement on
  50. * 0 does not open
  51. * 1 Open
  52. * 2 Open and terminate the program
  53. * int $mode return type
  54. * 0 returns multiple records
  55. * 1 Returns a single record
  56. * 2 Number of rows returned
  57. * String/array $table database table, two kinds of value-transfer modes
  58. * Normal Mode:
  59. * ' Tb_member, Tb_money '
  60. * Array Mode:
  61. * Array (' tb_member ', ' Tb_money ')
  62. * String/array $fields database fields that need to be queried, allowed to be empty, default to find all, two modes of pass value
  63. * Normal Mode:
  64. * ' Username, password '
  65. * Array Mode:
  66. * Array (' username ', ' password ')
  67. * String/array $sqlwhere query condition, allow NULL, two modes of transmission value
  68. * Normal Mode:
  69. * ' and type = 1 and username like "%os%" '
  70. * Array Mode:
  71. * Array (' type = 1 ', ' username like '%os% "')
  72. * String $orderby Sort, default to reverse ID
  73. */
  74. Public Function Select ($debug, $mode, $table, $fields = "*", $sqlwhere = "", $orderby = "Tbid desc") {
  75. Parameter handling
  76. if (Is_array ($table)) {
  77. $table = Implode (', ', $table);
  78. }
  79. if (Is_array ($fields)) {
  80. $fields = Implode (', ', $fields);
  81. }
  82. if (Is_array ($sqlwhere)) {
  83. $sqlwhere = ' and '. Implode (' and ', $sqlwhere);
  84. }
  85. Database operations
  86. if ($debug = = = 0) {
  87. if ($mode = = = 2) {
  88. $this->query ("SELECT count (tbid) from $table where 1=1 $sqlwhere");
  89. $return = $this->fetchcolumn ();
  90. }else if ($mode = = = 1) {
  91. $this->query ("Select $fields from $table where 1=1 $sqlwhere order by $orderby");
  92. $return = $this->fetch ();
  93. }else{
  94. $this->query ("Select $fields from $table where 1=1 $sqlwhere order by $orderby");
  95. $return = $this->fetchall ();
  96. }
  97. return $return;
  98. }else{
  99. if ($mode = = = 2) {
  100. echo "SELECT COUNT (tbid) from $table where 1=1 $sqlwhere";
  101. }else if ($mode = = = 1) {
  102. echo "Select $fields from $table where 1=1 $sqlwhere order by $orderby";
  103. }
  104. else{
  105. echo "Select $fields from $table where 1=1 $sqlwhere order by $orderby";
  106. }
  107. if ($debug = = = 2) {
  108. Exit
  109. }
  110. }
  111. }
  112. /**
  113. * Parameter Description
  114. * int $debug whether to turn on debug, output SQL statement on
  115. * 0 does not open
  116. * 1 Open
  117. * 2 Open and terminate the program
  118. * int $mode return type
  119. * 0 no return information
  120. * 1 Returns the number of execution entries
  121. * 2 Returns the ID of the last inserted record
  122. * String/array $table database table, two kinds of value-transfer modes
  123. * Normal Mode:
  124. * ' Tb_member, Tb_money '
  125. * Array Mode:
  126. * Array (' tb_member ', ' Tb_money ')
  127. * String/array $set need to insert the field and content, two modes of transmission value
  128. * Normal Mode:
  129. * ' username = ' Test ', type = 1, dt = Now () '
  130. * Array Mode:
  131. * Array (' username = ' test ' ', ' type = 1 ', ' dt = Now () ')
  132. */
  133. Public Function Insert ($debug, $mode, $table, $set) {
  134. Parameter handling
  135. if (Is_array ($table)) {
  136. $table = Implode (', ', $table);
  137. }
  138. if (Is_array ($set)) {
  139. $set = Implode (', ', $set);
  140. }
  141. Database operations
  142. if ($debug = = = 0) {
  143. if ($mode = = = 2) {
  144. $this->query ("INSERT into $table set $set");
  145. $return = $this->lastinsertid ();
  146. }else if ($mode = = = 1) {
  147. $this->exec ("INSERT into $table set $set");
  148. $return = $this->res;
  149. }else{
  150. $this->query ("INSERT into $table set $set");
  151. $return = NULL;
  152. }
  153. return $return;
  154. }else{
  155. echo "INSERT into $table set $set";
  156. if ($debug = = = 2) {
  157. Exit
  158. }
  159. }
  160. }
  161. /**
  162. * Parameter Description
  163. * int $debug whether to turn on debug, output SQL statement on
  164. * 0 does not open
  165. * 1 Open
  166. * 2 Open and terminate the program
  167. * int $mode return type
  168. * 0 no return information
  169. * 1 Returns the number of execution entries
  170. * String $table database table, two modes of transfer value
  171. * Normal Mode:
  172. * ' Tb_member, Tb_money '
  173. * Array Mode:
  174. * Array (' tb_member ', ' Tb_money ')
  175. * String/array $set need to update the fields and content, two types of value-transfer mode
  176. * Normal Mode:
  177. * ' username = ' Test ', type = 1, dt = Now () '
  178. * Array Mode:
  179. * Array (' username = ' test ' ', ' type = 1 ', ' dt = Now () ')
  180. * String/array $sqlwhere Modify condition, allow null, two modes of transmission value
  181. * Normal Mode:
  182. * ' and type = 1 and username like "%os%" '
  183. * Array Mode:
  184. * Array (' type = 1 ', ' username like '%os% "')
  185. */
  186. Public Function Update ($debug, $mode, $table, $set, $sqlwhere = "") {
  187. Parameter handling
  188. if (Is_array ($table)) {
  189. $table = Implode (', ', $table);
  190. }
  191. if (Is_array ($set)) {
  192. $set = Implode (', ', $set);
  193. }
  194. if (Is_array ($sqlwhere)) {
  195. $sqlwhere = ' and '. Implode (' and ', $sqlwhere);
  196. }
  197. Database operations
  198. if ($debug = = = 0) {
  199. if ($mode = = = 1) {
  200. $this->exec ("Update $table set $set where 1=1 $sqlwhere");
  201. $return = $this->res;
  202. }else{
  203. $this->query ("Update $table set $set where 1=1 $sqlwhere");
  204. $return = NULL;
  205. }
  206. return $return;
  207. }else{
  208. echo "Update $table set $set where 1=1 $sqlwhere";
  209. if ($debug = = = 2) {
  210. Exit
  211. }
  212. }
  213. }
  214. /**
  215. * Parameter Description
  216. * int $debug whether to turn on debug, output SQL statement on
  217. * 0 does not open
  218. * 1 Open
  219. * 2 Open and terminate the program
  220. * int $mode return type
  221. * 0 no return information
  222. * 1 Returns the number of execution entries
  223. * String $table database table
  224. * String/array $sqlwhere Delete condition, allow null, two modes of transfer value
  225. * Normal Mode:
  226. * ' and type = 1 and username like "%os%" '
  227. * Array Mode:
  228. * Array (' type = 1 ', ' username like '%os% "')
  229. */
  230. Public Function Delete ($debug, $mode, $table, $sqlwhere = "") {
  231. Parameter handling
  232. if (Is_array ($sqlwhere)) {
  233. $sqlwhere = ' and '. Implode (' and ', $sqlwhere);
  234. }
  235. Database operations
  236. if ($debug = = = 0) {
  237. if ($mode = = = 1) {
  238. $this->exec ("Delete from $table where 1=1 $sqlwhere");
  239. $return = $this->res;
  240. }else{
  241. $this->query ("Delete from $table where 1=1 $sqlwhere");
  242. $return = NULL;
  243. }
  244. return $return;
  245. }else{
  246. echo "Delete from $table where 1=1 $sqlwhere";
  247. if ($debug = = = 2) {
  248. Exit
  249. }
  250. }
  251. }
  252. }
Copy Code
Pdo
  • Related Article

    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.