Implemented crud operations based on simple dao classes in php and mysql

Source: Internet
Author: User
A simple dao that implements basic CRUD functions can inherit the dao class extended to the actual business, and can also be directly used.

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

,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.