A simple dao that implements basic CRUD functions can inherit the dao class extended to the actual business, and can also be directly used.
The Code is as follows:
// 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 );
}
?>