PHP implementation of constructing SQL statement class instance, constructing SQL statement Instance _php Tutorial

Source: Internet
Author: User
Tags mysql free php mysql rtrim strcmp

PHP implementation of constructing SQL statement class instance, constructing SQL statement instance


This article describes the construction SQL statement class implemented by PHP. Share to everyone for your reference, as follows:

/*** @package Database class* @author Injection (mail:injection.mail@gmail.com) * @version 1.0*/@ini_set (' Display_ Errors ', 0); class database{private $mDb _host, $mAb _user, $mAb _pwd, $mConn _no; function DataBase ($Conn _obj) {$this Connectdb ($Conn _obj); } function Connectdb ($Conn _obj) {$this->mdb_host = $Conn _obj->host; $this->mad_name = $Conn _obj->user; $thi S->mad_pwd = $Conn _obj->pwd; $this->mconn_no = mysql_connect ($this->mdb_host, $this->mad_name, $this->mad_pwd); } function Selectdb ($Conn _obj) {$this->mdb_name = $Conn _obj->dbname; mysql_select_db ($this->mdb_name);}} /*** @package Making sqls Class exetends Database class* @author Injection (mail:injection.mail@gmail.com) * @version 1.0*/ Class Makesql extends database{private $mSql; function Makesql ($type, $arr _colum_list, $arr _sql_choice) {$this->make SqlType ($arr _colum_list, $arr _sql_choice); }//switch make list function Makesqltype ($type, $arr _colum_list, $arr _sql_choice) { Switch ($type) {case ' Insert ': Return $this->makeinsert ($arr _colum_list, $arr _sql_choice); case ' SELECT ': Return $t His->makeselect ($arr _colum_list, $arr _sql_choice); Case ' Update ': Return $this->makeupdate ($arr _colum_list, $arr _sql_choice); Case ' Delete ': Return $this->makedelete ($arr _colum_list, $arr _sql_choice); }}//make Insert function Makeinsert ($arr _colum_list, $arr _sql_choice) {$colum _key = Array_keys ($arr _colum_list); $col Um_value = array_values ($arr _colum_list); $this->msql = "INSERT into". $arr _sql_choice["Tbl_name"]. " (". Join (', ', $colum _key).") VALUES (' ". Join (" ', ' ", $colum _value)." ') "; return $this->msql; }//making Select function Makeselect ($arr _colum_list = ' * ', $arr _sql_choice) {$colum _value = Array_keys ($arr _colum_li ST); foreach ($arr _sql_choice as $sql _key = $sql _value) {if (strcmp ($sql _key, ' tbl_name ') = = 0) {if (strcmp ($arr _colum_ List, ' * ')!== 0) $this->msql = "select". Join (', ', $colum _value). " From ". $sql _value; else $this->msql = "SELECT * from". $sql _value;} else if (strcmp ($sql _value, ')!== 0) if (strcmp ($sql _key, ' WHERE ') = = = 0 && strcmp ($sql _value, ' colum ') =   = = 0) {foreach ($arr _colum_list as $colum _key = $colum _value) $this->msql. = "$colum _key = ' $colum _value ' and";  $this->msql = RTrim ($this->msql, "and"); } else $this->msql. = "$sql _key". $sql _value;}  return $this->msql; }//making Update function makeupdate ($arr _colum_list, $arr _sql_choice) {$this->msql = "Update". $arr _sql_choice[' TB L_name ']. " SET "; foreach ($arr _colum_list as $colum _key = $colum _value) $this->msql. = "$colum _key = ' $colum _value ',"; $this->msql = RTrim ($this->msql, ', '); foreach ($arr _sql_choice as $sql _key = $sql _value) {if (strcmp ($sql _value, ')!== 0 && strcmp ($sql _key, ' Tbl_name ')!==0 && strcmp ($sql _key, ' ORDER by ')!== 0) $this->msql. = "$sql _key". $sql _value;} return $this->msql; }Making delete function Makedelete ($arr _colum_list, $arr _sql_choice) {$this->msql = "Delete from". $arr _sql_choice[ ' Tbl_name ']; foreach ($arr _sql_choice as $sql _key = $sql _value) {if (strcmp ($sql _key, ' tbl_name ')!== 0 && strcmp ($sql _ Value, ')!== 0) {$this->msql. = "$sql _key". $sql _value;}} return $this->msql; }}

More interested in PHP related content readers can view this site topic: "PHP+MYSQL Database Operation Primer", "PHP Prevention SQL injection Method Summary", "PHP based on PDO Operation Database skills summary" and "PHP common database Operation Skills Summary"

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • Workarounds for PHP Execution of batch MySQL statements
    • PHP connects to MySQL and executes the code of the SQL statement
    • Implementation code for PHP brokered MySQL statements
    • PHP Learning SQL Statement Quick Start
    • PHP Bulk Delete SQL statements
    • How PHP executes the SQL statement
    • PHP MSSQL Database Paging SQL statement
    • PHP based on a singleton pattern implementation of MySQL class
    • PHP package connection MySQL class and usage analysis
    • A PHP MySQL class can refer to learning familiar under
    • 12 Common Php+mysql free CMS systems

http://www.bkjia.com/PHPjc/1098689.html www.bkjia.com true http://www.bkjia.com/PHPjc/1098689.html techarticle PHP Implementation of the construction of the SQL statement class instance, constructs the SQL statement instance this article describes the PHP implementation constructs the SQL statement class. Share to everyone for your reference, specifically as follows:/*** @package Da ...

  • Related Article

    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.