基於php和mysql的簡單的dao類

WBOY
發布: 2016-07-23 08:55:00
原創
912 人瀏覽過
複製代碼
SimpleDao.class
  1. //require_once('FirePHPCore/FirePHP.class.php');
  2. //$firephp = FirePHP::getInstance(真(真()的); // firefox 中的調試器
  3. class SimpleDao {
  4. private $_table = null;
  5. private static $_con = null;
  6. public function SimpleDao() {
  7. public function SimpleDao() {
  8. public function SimpleDao() {
  9. public function SimpleDao() {
  10. if ($this- >_con == null) {
  11. $this->_con = @mysql_connect("localhost", "root", "123456");
  12. if ($this->_con == FALSE ) {
  13. echo("連接資料庫伺服器失敗。");
  14. $this->_con = null;
  15. return;
  16. }
  17. //$firephp->log("new DAO物件");
  18. @mysql_select_db("swan", $this->_con);
  19. }
  20. }
  21. public function table($tablename) {
  22. $this -> _table = $表名;
  23. return $this;
  24. }
  25. public function query($sql) {
  26. $result = @mysql_query($sql);
  27. $ ret = [];
  28. if ($result) {
  29. while ($row = mysql_fetch_array($result)) {
  30. $ret[] = $row;
  31. }
  32. }
  33. return $ret;
  34. }
  35. public function get($where = null) {
  36. $sql = "select * from ".$this->_table;
  37. //$ sql = $ sql.$this->_getWhereString($where);
  38. //echo "[get]".$sql."
    ";
  39. return $this->query($ sql);
  40. }
  41. public function insert($params) {
  42. if ($params == null || !is_array($params)) {
  43. return -1;
  44. }
  45. $ keys = $this->_getParamKeyString($params);
  46. $vals = $this->_getParamValString($ params);
  47. $sql = "insert into ".$this->_table."(".$ keys.") value(".$vals.")";
  48. //echo "[插入]".$sql."
    ";
  49. $result = @mysql_query($sql);
  50. if (! $result) {
  51. return -1;
  52. }
  53. return @mysql_insert_id();
  54. }
  55. public function update($params, $where = n. ) {
  56. if ($params == null || !is_array($params)) {
  57. return -1;
  58. }
  59. $upvals = $this->_getUpdateString($params);
  60. $wheres = $this->_getWhereString($where);
  61. $sql = "update ".$this->_table." set ".$upvals." ".$wheres;
  62. //echo "[update]".$sql."
    ";
  63. $ result = @mysql_query($sql);
  64. if (! $result) {
  65. return -1;
  66. }
  67. return @mysql_affected_rows();
  68. }
  69. public function delete($where) {
  70. $wheres = $this->_getWhereString($where);
  71. $wheres = $this->_getWhereString($where);
  72. $. ".$this->_table.$wheres;
  73. // 刪除echo "[delete]".$sql."
    ";
  74. $result = @mysql_query($sql);
  75. if (! $result) {
  76. return -1;
  77. }
  78. return @mysql_affected_rows();
  79. }
  80. protected function _getParamKeyString($params) {
  81. protected array_keys( $params);
  82. return implode(",", $keys);
  83. }
  84. protected function _getParamValString($params) {
  85. $vals = arrayparaues($$ms);
  86. return "'".implode("','", $vals)."'";
  87. }
  88. private function _getUpdateString($params) {
  89. //echo "_getUpdateString ";
  90. $sql = "";
  91. if (is_array($params)) {
  92. $sql = $this->_getKeyValString($params, ",");
  93. }
  94. return $sql;
  95. }
  96. private function _getWhereString($params) {
  97. //echo "_getWhereString";
  98. $sql = "";
  99. if (is_array($params_array($params)_array($params)_array($params) ) ) {
  100. $sql = " where ";
  101. $where = $this->_getKeyValString($params, " and "); $sql = $sql.$where; } 回傳$sql; }
  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($ table ) {
  122. return (new SimpleDao())->table($table);
  123. }
  124. ?>
複製程式碼
使用SimpleDao的程式碼段
  1. include "test/simpledao.php";
  2. $dao = T("sw_post");
  3. $ result = $dao->get();//取得所有貼文
  4. $dao->release();
  5. echo json_encode($result);
  6. ?>
  7. include "test/simpledao.php";
  8. $dao = T("sw_post");
  9. // 更新標題,其中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. // 插入新記錄
  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. // 從表中刪除name='測試TAG'
  24. $cnt = $dao->delete(array("name") =>"測試TAG"));
  25. $dao->release();
  26. echo json_encode(array("count"=>$cnt));
  27. ?>
複製代碼
複製代碼
複製代碼

複製代碼複製代碼 php、mysql、dao
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板