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