MySQL增刪改查工具PHP類

來源:互聯網
上載者:User
  1. 以前開發項目沒用架構,直接物件導向開發很實用的一個mysql工具類。
  2. header("content-type:text/html;charset=utf-8");
  3. class DBUtils{
  4. /**
  5. *通用更新方法 insert update delete 操作
  6. *@param sql
  7. *@return bool true false
  8. */
  9. public function update($sql){
  10. $link = $this->getConn();
  11. mysql_query($sql);
  12. //如果出錯顯示
  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. *通用查詢方法 select 操作
  23. *@param sql
  24. *@return array
  25. */
  26. public function queryRows($sql){
  27. //建立串連,編碼,資料庫
  28. $link = $this->getConn();
  29. //發送sql
  30. $rs = mysql_query($sql);
  31. //如果出錯顯示
  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. *通用查詢方法 select 操作 查詢結果一行資料
  46. *@param sql
  47. *@return array 如果失敗返回 false;
  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. *通用查詢方法 select 操作 查詢結果一個資料
  58. *@param sql
  59. *@return array 如果失敗返回 false;
  60. * 例: 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. }
複製代碼
MySQL, PHP
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.