PHP Beginners use the Insert and Update statements to construct classes. There are not many functions available. For more information about php, see
PHP Beginners use the Insert and Update statements to construct classes. There are not many functions available. For more information about php, see
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;
}
}