PHP newbie's Insert and Update statement construction class _ PHP Tutorial

Source: Internet
Author: User
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...

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.