复制代码 代码如下:
<?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);
}
?>
mysql,dao,crud操作
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 谭咏麟《20世纪中华歌坛名人百集珍藏版》[WAV+CUE][1G]
- 炉石传说40轮盘术最新卡组代码在哪找 标准40轮盘术卡组代码分享
- 炉石传说亲王贼怎么玩 2024亲王贼最新卡组代码分享
- 炉石传说30.6.2补丁后有什么卡组 30.6.2最强卡组最新推荐
- 模拟之声慢刻CD《蔡琴名曲回顾遇听》[原抓WAV+CUE]
- BruceLiu-WAVES(MusicbySatie)(2024)2CD[24Bit-96kHz]FLAC
- KonstantinKrimmel-MythosSchubertLoewe(2024)[24Bit-96kHz]FLAC
- 2024雷蛇高校挑战赛 嘤式分解助力收官之战
- 海信发布110吋世俱杯官方定制AI电视 引领智能观赛
- 海信发布27英寸显示器大圣G5 Pro:采用自研超解析芯片、友达原厂模组
- 蔡琴《机遇》1:1母盘直刻日本头版[WAV分轨][1.1G]
- 陈百强《与你几分钟的约会》XRCD+SHMCD限量编号版[低速原抓WAV+CUE][994M]
- 陈洁丽《监听王NO.1 》示范级发烧天碟[WAV+分轨][1.1G]
- 单色凌.2014-小岁月太着急【海蝶】【WAV+CUE】
- 陈淑桦.1988-抱紧我HOLD.ME.NOW【EMI百代】【WAV+CUE】