时间:2021-05-26
复制代码 代码如下:
<?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);
}
?>
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Python基于列表list实现的CRUD操作功能。分享给大家供大家参考,具体如下:本篇文章看之前你的先了解python基础的知识点,比如控制流,
本文实例讲述了基于thinkPHP类的插入数据库操作功能。分享给大家供大家参考,具体如下:基于TP类的插入数据库操作/*********************
Java中利用泛型和反射机制抽象DAO的实例一般的DAO都有CRUD操作,在每个实体DAO接口中重复定义这些方法,不如提供一个通用的DAO接口,具体的实体DAO
本文实例讲述了php基于PDO实现功能强大的MYSQL封装类。分享给大家供大家参考,具体如下:classCPdo{protected$_dsn="mysql:h
连接方式多样,数据操作功能强大。PHP技术有着较多的连接方式,所以其数据操作功能很强大。在数据库和PHP技术进行连接时不需要任何媒介,就能方便快捷地访问数据库,