ホームページ  >  記事  >  バックエンド開発  >  php と mysql に基づくシンプルな dao クラス

php と mysql に基づくシンプルな dao クラス

WBOY
WBOYオリジナル
2016-07-23 08:55:00875ブラウズ
SimpleDao.class
  1. //require_once('FirePHPCore/FirePHP.class.php');
  2. //$firephp = FirePHP::getInstance(true); // 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("データベースサーバーへの接続に失敗しました。");
  11. $this ->_con = null;
  12. return;
  13. }
  14. //$firephp->log("新しい DAO オブジェクト");
  15. @mysql_select_db("swan", $this->_con);
  16. }
  17. }
  18. パブリック関数テーブル($tablename) {
  19. $this->_table = $tablename;
  20. return $this;
  21. }
  22. パブリック関数クエリ($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.") value(".$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."< br>";
  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 = "「.$this->_table.$wheres;から削除」
  69. //echo "[delete]".$sql." $result = @mysql_query($sql);
  70. if (! $result) {
  71. return -1;
  72. }
  73. return @mysql_affected_rows();
  74. }
  75. protected function _getParamKeyString($params) {
  76. $keys = array_keys($params);
  77. return implode(",", $keys );
  78. }
  79. protected function _getParamValString($params) {
  80. $vals = array_values($params);
  81. return "'".implode("','", $vals)."'";
  82. }
  83. private function _getUpdateString($params) {
  84. //echo "_getUpdateString";
  85. $sql = "";
  86. if (is_array($params)) {
  87. $sql = $this->_getKeyValString($params, "," );
  88. }
  89. return $sql;
  90. }
  91. プライベート関数 _getWhereString($params) {
  92. //echo "_getWhereString";
  93. $sql = "";
  94. if (is_array($params)) {
  95. $sql = " where ";
  96. $where = $this->_getKeyValString($params, " and ");
  97. $sql = $sql.$where;
  98. }
  99. return $sql;
  100. }
  101. プライベート関数 _getKeyValString($params, $split) {
  102. $str = "";
  103. if (is_array($params)) {
  104. $paramArr = array();
  105. foreach($params as $key=>$ val) {
  106. $valstr = $val;
  107. if (is_string($val)) {
  108. $valstr = "'".$val."'";
  109. }
  110. $paramArr[] = $key."=。 $valstr;
  111. }
  112. $str = $str.implode($split, $paramArr);
  113. }
  114. return $str;
  115. }
  116. public function release() {
  117. @mysql_close();
  118. }
  119. }
  120. function T($table) {
  121. return (new SimpleDao())->table($table);
  122. }
  123. ?>
复制代码
SimpleDao の代コードセグメントを使用します
  1. include "test/simpledao.php";
  2. $dao = T("sw_post");
  3. $result = $dao->get();//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


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。