Home  >  Article  >  Backend Development  >  基于php和mysql的简单的dao类

基于php和mysql的简单的dao类

WBOY
WBOYOriginal
2016-07-23 08:55:00810browse
SimpleDao.class
  1. //require_once('FirePHPCore/FirePHP.class.php');
  2. //$firephp = FirePHP::getInstance(true); // debugger in firefox
  3. class SimpleDao {
  4. private $_table = null;
  5. private static $_con = null;
  6. public function SimpleDao() {
  7. if ($this->_con == null) {
  8. $this->_con = @mysql_connect("localhost", "root", "123456");
  9. if ($this->_con == FALSE) {
  10. echo("connect to db server failed.");
  11. $this->_con = null;
  12. return;
  13. }
  14. //$firephp->log("new DAO object");
  15. @mysql_select_db("swan", $this->_con);
  16. }
  17. }
  18. public function table($tablename) {
  19. $this->_table = $tablename;
  20. return $this;
  21. }
  22. public function query($sql) {
  23. $result = @mysql_query($sql);
  24. $ret = [];
  25. if ($result) {
  26. while ($row = mysql_fetch_array($result)) {
  27. $ret[] = $row;
  28. }
  29. }
  30. return $ret;
  31. }
  32. public function get($where = null) {
  33. $sql = "select * from ".$this->_table;
  34. //$sql = $sql.$this->_getWhereString($where);
  35. //echo "[get]".$sql."
    ";
  36. return $this->query($sql);
  37. }
  38. public function insert($params) {
  39. if ($params == null || !is_array($params)) {
  40. return -1;
  41. }
  42. $keys = $this->_getParamKeyString($params);
  43. $vals = $this->_getParamValString($params);
  44. $sql = "insert into ".$this->_table."(".$keys.") values(".$vals.")";
  45. //echo "[insert]".$sql."
    ";
  46. $result = @mysql_query($sql);
  47. if (! $result) {
  48. return -1;
  49. }
  50. return @mysql_insert_id();
  51. }
  52. public function update($params, $where = null) {
  53. if ($params == null || !is_array($params)) {
  54. return -1;
  55. }
  56. $upvals = $this->_getUpdateString($params);
  57. $wheres = $this->_getWhereString($where);
  58. $sql = "update ".$this->_table." set ".$upvals." ".$wheres;
  59. //echo "[update]".$sql."
    ";
  60. $result = @mysql_query($sql);
  61. if (! $result) {
  62. return -1;
  63. }
  64. return @mysql_affected_rows();
  65. }
  66. public function delete($where) {
  67. $wheres = $this->_getWhereString($where);
  68. $sql = "delete from ".$this->_table.$wheres;
  69. //echo "[delete]".$sql."
    ";
  70. $result = @mysql_query($sql);
  71. if (! $result) {
  72. return -1;
  73. }
  74. return @mysql_affected_rows();
  75. }
  76. protected function _getParamKeyString($params) {
  77. $keys = array_keys($params);
  78. return implode(",", $keys);
  79. }
  80. protected function _getParamValString($params) {
  81. $vals = array_values($params);
  82. return "'".implode("','", $vals)."'";
  83. }
  84. private function _getUpdateString($params) {
  85. //echo "_getUpdateString";
  86. $sql = "";
  87. if (is_array($params)) {
  88. $sql = $this->_getKeyValString($params, ",");
  89. }
  90. return $sql;
  91. }
  92. private function _getWhereString($params) {
  93. //echo "_getWhereString";
  94. $sql = "";
  95. if (is_array($params)) {
  96. $sql = " where ";
  97. $where = $this->_getKeyValString($params, " and ");
  98. $sql = $sql.$where;
  99. }
  100. return $sql;
  101. }
  102. private function _getKeyValString($params, $split) {
  103. $str = "";
  104. if (is_array($params)) {
  105. $paramArr = array();
  106. foreach($params as $key=>$val) {
  107. $valstr = $val;
  108. if (is_string($val)) {
  109. $valstr = "'".$val."'";
  110. }
  111. $paramArr[] = $key."=".$valstr;
  112. }
  113. $str = $str.implode($split, $paramArr);
  114. }
  115. return $str;
  116. }
  117. public function release() {
  118. @mysql_close();
  119. }
  120. }
  121. function T($table) {
  122. return (new SimpleDao())->table($table);
  123. }
  124. ?>
复制代码
使用SimpleDao的代码段
  1. include "test/simpledao.php";
  2. $dao = T("sw_post");
  3. $result = $dao->get();//get all posts
  4. $dao->release();
  5. echo json_encode($result);
  6. ?>
  7. include "test/simpledao.php";
  8. $dao = T("sw_post");
  9. // update title where id=1
  10. $cnt = $dao->update(array("title"=>"Hello REST2"), array("id"=>1));
  11. $dao->release();
  12. echo json_encode(array("count"=>$cnt));
  13. ?>
  14. include "test/simpledao.php";
  15. $dao = T("sw_tag");
  16. // insert new record
  17. $cnt = $dao->insert(array("postid"=>4, "name"=>"测试TAG"));
  18. $dao->release();
  19. echo json_encode(array("count"=>$cnt));
  20. ?>
  21. include "test/simpledao.php";
  22. $dao = T("sw_tag");
  23. // delete from table where name='测试TAG'
  24. $cnt = $dao->delete(array("name"=>"测试TAG"));
  25. $dao->release();
  26. echo json_encode(array("count"=>$cnt));
  27. ?>
复制代码
php, mysql, dao


Statement:
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