SimpleDao.class
-
//require_once('FirePHPCore/FirePHP.class.php');
- //$firephp = FirePHP::getInstance(真(真()的); // firefox 中的調試器
- class SimpleDao {
- private $_table = null;
- private static $_con = null;
-
- public function SimpleDao() {
- public function SimpleDao() {
- public function SimpleDao() {
- public function SimpleDao() {
- if ($this- >_con == null) {
- $this->_con = @mysql_connect("localhost", "root", "123456");
- if ($this->_con == FALSE ) {
- echo("連接資料庫伺服器失敗。");
- $this->_con = null;
- return;
- }
- //$firephp->log("new DAO物件");
- @mysql_select_db("swan", $this->_con);
- }
- }
-
- public function table($tablename) {
- $this -> _table = $表名;
- return $this;
- }
-
- public function query($sql) {
- $result = @mysql_query($sql);
- $ ret = [];
- if ($result) {
- while ($row = mysql_fetch_array($result)) {
- $ret[] = $row;
- }
- }
- return $ret;
- }
-
- public function get($where = null) {
- $sql = "select * from ".$this->_table;
- //$ sql = $ sql.$this->_getWhereString($where);
- //echo "[get]".$sql."
";
- return $this->query($ sql);
- }
-
- public function insert($params) {
- if ($params == null || !is_array($params)) {
- return -1;
- }
- $ keys = $this->_getParamKeyString($params);
- $vals = $this->_getParamValString($ params);
- $sql = "insert into ".$this->_table."(".$ keys.") value(".$vals.")";
- //echo "[插入]".$sql."
";
- $result = @mysql_query($sql);
- if (! $result) {
- return -1;
- }
- return @mysql_insert_id();
- }
-
- public function update($params, $where = n. ) {
- if ($params == null || !is_array($params)) {
- return -1;
- }
- $upvals = $this->_getUpdateString($params);
- $wheres = $this->_getWhereString($where);
- $sql = "update ".$this->_table." set ".$upvals." ".$wheres;
- //echo "[update]".$sql."
";
- $ result = @mysql_query($sql);
- if (! $result) {
- return -1;
- }
- return @mysql_affected_rows();
- }
-
- public function delete($where) {
- $wheres = $this->_getWhereString($where);
- $wheres = $this->_getWhereString($where);
- $. ".$this->_table.$wheres;
- // 刪除echo "[delete]".$sql."
";
- $result = @mysql_query($sql);
- if (! $result) {
- return -1;
- }
- return @mysql_affected_rows();
- }
-
- protected function _getParamKeyString($params) {
- protected array_keys( $params);
- return implode(",", $keys);
- }
-
- protected function _getParamValString($params) {
- $vals = arrayparaues($$ms);
- return "'".implode("','", $vals)."'";
- }
-
- private function _getUpdateString($params) {
- //echo "_getUpdateString ";
- $sql = "";
- if (is_array($params)) {
- $sql = $this->_getKeyValString($params, ",");
- }
- return $sql;
- }
-
- private function _getWhereString($params) {
- //echo "_getWhereString";
- $sql = "";
- if (is_array($params_array($params)_array($params)_array($params) ) ) {
- $sql = " where ";
- $where = $this->_getKeyValString($params, " and "); $sql = $sql.$where; } 回傳$sql; }
-
- private function _getKeyValString($params, $split) {
- $str = "";
- if (is_array($params)) {
- $paramArr = array();
- foreach($params as $key=>$val) {
- $valstr = $val;
- if (is_string($val)) {
- $valstr = "'".$val." ' ";
- }
- $paramArr[] = $key."=".$valstr;
- }
- $str = $str.implode($split, $paramArr);
- }
- return $str;
- }
-
- public function release() {
- @mysql_close();
- }
- }
-
- function($ table ) {
- return (new SimpleDao())->table($table);
- }
- ?>
複製程式碼
使用SimpleDao的程式碼段
-
include "test/simpledao.php";
- $dao = T("sw_post");
- $ result = $dao->get();//取得所有貼文
- $dao->release();
- echo json_encode($result);
- ?>
-
-
- include "test/simpledao.php";
- $dao = T("sw_post");
- // 更新標題,其中id=1
- $cnt = $dao-> ;update(array("title "=>"Hello REST2"), array("id"=>1));
- $dao->release();
- echo json_encode(array(" count"=>$cnt));
- ?>
-
-
include "test/simpledao.php";
- $dao = T("sw_tag") ;
- // 插入新記錄
- $ cnt = $dao->insert(array("postid"=>4, "name"=>"測試TAG"));
- $dao ->release();
- echo json_encode(array("count "=>$cnt));
- ?>
-
-
include "test/simpledao .php";
- $dao = T("sw_tag");
- // 從表中刪除name='測試TAG'
- $cnt = $dao->delete(array("name") =>"測試TAG"));
- $dao->release();
- echo json_encode(array("count"=>$cnt));
- ?>
-
-
複製代碼
|
複製代碼
複製代碼
複製代碼
複製代碼複製代碼
php、mysql、dao
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31