基于php和mysql的簡(jiǎn)單的dao類(lèi)實(shí)現(xiàn)crud操作功能
<?php
//require_once('FirePHPCore/FirePHP.class.php');
//$firephp = FirePHP::getInstance(true); // debugger in firefox
class SimpleDao {
private $_table = null;
private static $_con = null;
public function SimpleDao() {
if ($this->_con == null) {
$this->_con = @mysql_connect("localhost", "root", "123456");
if ($this->_con == FALSE) {
echo("connect to db server failed.");
$this->_con = null;
return;
}
//$firephp->log("new DAO object");
@mysql_select_db("swan", $this->_con);
}
}
public function table($tablename) {
$this->_table = $tablename;
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."<br>";
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.") values(".$vals.")";
//echo "[insert]".$sql."<br>";
$result = @mysql_query($sql);
if (! $result) {
return -1;
}
return @mysql_insert_id();
}
public function update($params, $where = null) {
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."<br>";
$result = @mysql_query($sql);
if (! $result) {
return -1;
}
return @mysql_affected_rows();
}
public function delete($where) {
$wheres = $this->_getWhereString($where);
$sql = "delete from ".$this->_table.$wheres;
//echo "[delete]".$sql."<br>";
$result = @mysql_query($sql);
if (! $result) {
return -1;
}
return @mysql_affected_rows();
}
protected function _getParamKeyString($params) {
$keys = array_keys($params);
return implode(",", $keys);
}
protected function _getParamValString($params) {
$vals = array_values($params);
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)) {
$sql = " where ";
$where = $this->_getKeyValString($params, " and ");
$sql = $sql.$where;
}
return $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 T($table) {
return (new SimpleDao())->table($table);
}
?>
- php設(shè)計(jì)模式 DAO(數(shù)據(jù)訪問(wèn)對(duì)象模式)
- php daodb插入、更新與刪除數(shù)據(jù)
- PHP設(shè)計(jì)模式之建造者模式(Builder)原理與用法案例詳解
- PHP設(shè)計(jì)模式之適配器模式(Adapter)原理與用法詳解
- PHP設(shè)計(jì)模式之策略模式(Strategy)入門(mén)與應(yīng)用案例詳解
- PHP設(shè)計(jì)模式之單例模式入門(mén)與應(yīng)用詳解
- PHP設(shè)計(jì)模式之觀察者模式入門(mén)與應(yīng)用案例詳解
- PHP設(shè)計(jì)模式之中介者模式(Mediator Pattern)入門(mén)與應(yīng)用案例詳解
- PHP設(shè)計(jì)模式之觀察者模式(Observer)詳細(xì)介紹和代碼實(shí)例
- PHP經(jīng)典面試題之設(shè)計(jì)模式(經(jīng)常遇到)
- php設(shè)計(jì)模式小結(jié)
- PHP設(shè)計(jì)模式之?dāng)?shù)據(jù)訪問(wèn)對(duì)象模式(DAO)原理與用法實(shí)例分析
相關(guān)文章
YII2框架中操作數(shù)據(jù)庫(kù)的方式實(shí)例分析
這篇文章主要介紹了YII2框架中操作數(shù)據(jù)庫(kù)的方式,結(jié)合實(shí)例形式總結(jié)分析了YII2使用createCommand方式及AR(Active Record)方式操作數(shù)據(jù)庫(kù)相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2020-03-03ThinkPHP5.0多個(gè)文件上傳后找不到臨時(shí)文件的修改方法
這篇文章主要介紹了ThinkPHP5.0多個(gè)文件上傳后找不到臨時(shí)文件的修改方法,需要的朋友可以參考下2018-07-07PHP+百度AI OCR文字識(shí)別實(shí)現(xiàn)了圖片的文字識(shí)別功能
這篇文章主要介紹了PHP+百度AI OCR文字識(shí)別實(shí)現(xiàn)了圖片的文字識(shí)別功能,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05php版微信公眾平臺(tái)實(shí)現(xiàn)預(yù)約提交后發(fā)送email的方法
這篇文章主要介紹了php版微信公眾平臺(tái)實(shí)現(xiàn)預(yù)約提交后發(fā)送email的方法,結(jié)合實(shí)例形式分析了php微信公眾平臺(tái)郵件發(fā)送的相關(guān)操作技巧,需要的朋友可以參考下2016-09-09destoon數(shù)據(jù)庫(kù)表說(shuō)明匯總
這篇文章主要介紹了destoon數(shù)據(jù)庫(kù)表說(shuō)明匯總,可以方便針對(duì)destoon進(jìn)行二次的時(shí)候查詢使用,需要的朋友可以參考下2014-07-07PHP迭代器實(shí)現(xiàn)斐波納契數(shù)列的函數(shù)
斐波納契數(shù)列通常做法是用遞歸實(shí)現(xiàn),當(dāng)然還有其它的方法。這里現(xiàn)學(xué)現(xiàn)賣(mài),用PHP的迭代器來(lái)實(shí)現(xiàn)一個(gè)斐波納契數(shù)列,幾乎沒(méi)有什么難度,只是把類(lèi)里的next()方法重寫(xiě)了一次。注釋已經(jīng)寫(xiě)到代碼中,也是相當(dāng)好理解的2013-11-11php獲取網(wǎng)頁(yè)中圖片、DIV內(nèi)容的簡(jiǎn)單方法
這篇文章主要介紹了php獲取網(wǎng)頁(yè)中圖片、DIV內(nèi)容的簡(jiǎn)單方法,都是通過(guò)正則表達(dá)式實(shí)現(xiàn)的,強(qiáng)大的正則啊,需要的朋友可以參考下2014-06-06php的memcache類(lèi)分享(memcache隊(duì)列)
這篇文章主要介紹了php的memcache類(lèi)的使用方法(memcache隊(duì)列),需要的朋友可以參考下2014-03-03Swoole-1.7.22 版本已發(fā)布,修復(fù)PHP7相關(guān)問(wèn)題
swoole-1.7.22 版本已發(fā)布,此版本是一個(gè)BUG修復(fù)版本,專(zhuān)門(mén)針對(duì)PHP7做了大量修改,可完美運(yùn)行于PHP7環(huán)境2015-12-12