PHP beginners use Insert and Update statements to construct classes. Copy the code as follows: $ mysqlnewsqlstr (table1); $ mysql-set (name, value); $ mysql-set (name, 1, true ); echo $ mysql-insertSql (); copy the code as follows: c usage
The code is as follows:
$ Mysql = new sqlstr ("table1 ");
$ Mysql-> set ("name", "value ");
$ Mysql-> set ("name", "1", true );
Echo $ mysql-> insertSql ();
The code is as follows:
Class sqlstr
{
Private $ param = array ();
Private $ tablename;
Function sqlstr ($ tablename)
{
$ This-> tablename = $ tablename;
}
Public function set ($ name, $ value, $ isnum = false ){
$ Value = str_replace ("'", "'' ", $ value );
$ This-> param [$ name] = array ($ value, $ isnum );
}
Public function insertSql (){
$ Keys = "";
$ Values = "";
Foreach ($ this-> param as $ key => $ value ){
$ Keys = $ keys. $ key .",";
If ($ value [1]) {
$ Values = $ values. $ value [0]. ",";
} Else {
$ Values = $ values. "'". $ value [0]. "',";
}
}
If ($ keys! = "") {$ Keys = substr ($ keys, 0, strlen ($ keys)-1 );}
If ($ values! = "") {$ Values = substr ($ values, 0, strlen ($ values)-1 );}
Return "insert into". $ this-> tablename. "($ keys) values ($ values )";
}
Public function updateSql ($ cond ){
$ Group = "";
Foreach ($ this-> param as $ key => $ value ){
$ Group. = $ key. "= ";
If ($ value [1]) {
$ Group. = $ value [0]. ",";
} Else {
$ Group. = "'". $ value [0]. "',";
}
}
If ($ group! = "") {$ Group = substr ($ group, 0, strlen ($ group)-1 );}
Return "update". $ this-> tablename. "set $ group where". $ cond;
}
}
The alias code is as follows: $ mysql = new sqlstr ("table1"); $ mysql-set ("name", "value"); $ mysql-set ("name ", "1", true); echo $ mysql-insertSql (); the code is as follows: c...