<?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);
}
?>