Home > Backend Development > PHP Tutorial > MySQL add, delete, modify, query tool PHP class

MySQL add, delete, modify, query tool PHP class

WBOY
Release: 2016-07-25 08:43:37
Original
1150 people have browsed it
  1. In the past, the development project did not use a framework, and it was a very practical mysql tool class that was directly developed as object-oriented.
  2. header("content-type:text/html;charset=utf-8");
  3. class DBUtils{
  4. /**
  5. *General update method insert update delete operation
  6. *@param sql
  7. *@return bool true false
  8. */
  9. public function update($sql){
  10. $link = $this->getConn();
  11. mysql_query($sql);
  12. //If an error occurs
  13. if(DEBUG){
  14. echo mysql_error();
  15. }
  16. $rs = mysql_affected_rows($link);
  17. $rs = $rs > 0;
  18. mysql_close($link);
  19. return $rs;
  20. }
  21. /**
  22. *General query method select operation
  23. *@param sql
  24. *@return array
  25. */
  26. public function queryRows($sql){
  27. //Create connection, encoding , database
  28. $link = $this->getConn();
  29. //Send sql
  30. $rs = mysql_query($sql);
  31. //If an error occurs
  32. if(DEBUG){
  33. echo mysql_error();
  34. }
  35. $rows = array();
  36. while($row = mysql_fetch_array($rs)){
  37. $rows[] = $row;//pdemo7.php
  38. }
  39. //
  40. mysql_free_result($rs);
  41. mysql_close($link);
  42. return $rows;
  43. }
  44. /**
  45. *General query method select operation query result one row of data
  46. *@param sql
  47. *@return array Return false if failed;
  48. */
  49. public function queryRow($sql){
  50. $rs = $this->queryRows($sql);
  51. if (!empty($rs[0])){
  52. return $rs[0];
  53. }
  54. return false;
  55. }
  56. /**
  57. *General query method select operation Query result is one data
  58. *@param sql
  59. *@return array Return false if failed;
  60. * Example: select count(*) from user;
  61. */
  62. public function queryObj($sql){
  63. $rs = $this->queryRows($sql);
  64. //var_dump($rs);
  65. if(!empty($rs[0][0])){
  66. return $rs[0][0];
  67. }
  68. return false;
  69. }
  70. private function getConn(){
  71. $link = mysql_connect('127.0.0.1','root','');
  72. mysql_query("set names utf8");
  73. mysql_select_db(" news");
  74. return $link;
  75. }
  76. }
Copy code

MySQL, PHP


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template