PHP constructs using Insert and Update statements
- $ Mysql = new sqlstr ("table1 ");
- $ Mysql-> set ("name", "value ");
- $ Mysql-> set ("name", "1", true );
- Echo $ mysql-> insertSql ();
2. php constructor implemented by insert and update
- /**
- * Construction class of Insert and Update statements
- * Edit: bbs.it-home.org
- */
- 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;
- }
- }
|