基于php和mysql的简单的dao类实现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."
";
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."
";
$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."
";
$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."
";
$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);
}
?>

相关文章

Hessian开源的远程通讯,采用二进制 RPC的协议,基于 HTTP 传输。可以实现PHP调用Java,Python,C#等多语...
初识Mongodb的一些总结,在Mac Os X下真实搭建mongodb环境,以及分享个Mongodb管理工具,学习期间一些总结...
边看边操作,这样才能记得牢,实践是检验真理的唯一标准.光看不练假把式,光练不看傻把式,边看边练真把式....
在php中,结果输出一共有两种方式:echo和print,下面将对两种方式做一个比较。 echo与print的区别: (...
在安装好wampServer后,一直没有使用phpMyAdmin,今天用了一下,phpMyAdmin显示错误:The mbstring exte...
变量是用于存储数据的容器,与代数相似,可以给变量赋予某个确定的值(例如:$x=3)或者是赋予其它的变...