PHP implementation of the parameters generated by the MySQL statement class complete instance, MySQL statement _php tutorial

Source: Internet
Author: User
Tags mysql free php database

PHP implementation of the parameters generated by the MySQL statement class complete instance, MySQL statement


This article describes the PHP implementation of the parameters generated by the MySQL statement class. Share to everyone for your reference, as follows:

This class can create SELECT, INSERT, UPDATE, and DELETE statements by specifying the table and field parameters.

This class can create a where condition for an SQL statement, like a query statement, using a LEFT join and an order statement

<?php/* *******************************************************************example fileThis Example shows how to  Use the Mylibsqlgen classthe example are based on the following MySQL table:create table customer (ID int.) unsigned not Null auto_increment, name varchar NOT NULL default ", address varchar" NOT NULL default ", City varchar" not " NULL default ', PRIMARY KEY (cust_id)) type=myisam;**************************************************************** */require_once ("class_mylib_sqlgen-1.0.php"); $fields = Array ("name", "Address", "City"); $values = Array ("Fadjar", "Resultmang Raya Street", "Jakarta"); $tables = Array ("Customer"); echo "Result Generate Insert
" ; $object = new Mylibsqlgen (); $object-Clear_all_assign (); To refresh all property but it no need when first time execute $object-setfields ($fields); $object-Setvalues ($values); $object-Settables ($tables); if (! $object-Getinsertsql ()) {echo $object-Error; exit;} else {$sql = $object and Result; Echo $sql. "
" ;} echo "Result Generate Update
" ; $fields = Array ("name", "Address", "City"); $values = Array ("Fadjar", "Resultmang Raya Street", "Jakarta"); $tables = Array ("Customer"); $id = 1; $conditions [0] [condition] = "id= ' $id '"; $conditions [0] ["connection"] = ""; $object-Clear_all_assign (); $object-Setfields ($fields); $object-Setvalues ($values); $object-Settables ($tables); $object-Setconditions ($conditions); if (! $object-Getupdatesql ()) {echo $object-Error; exit;} else {$sql = $object and Result; Echo $sql. "
" ;} echo "Result Generate Delete
" ; $tables = Array ("Customer"); $conditions [0] [condition] = "id= ' 1"; $conditions [0] [Connection] = "OR"; $conditions [1] [Condition] = "id= ' 2"; $conditions [1] [Connection] = "OR"; $conditions [2] [condition] = "id= ' 4"; $conditions [2] ["connection"] = ""; $object-Clear_all_assign (); $object-Settables ($tables); $object-Setconditions ($conditions); if (! $object-Getdeletesql ()) {echo $object-Error; exit;} else {$sql = $object and Result; Echo $sql. "
" ;} echo "Result Generate List
" ; $fields = Array ("id", "name", "Address", "City"); $tables = Array ("Customer"); $id = 1; $conditions [0] [condition] = "id= ' $id '"; $conditions [0] ["connection"] = ""; $object-Clear_all_assign (); $object-Setfields ($fields); $object-Settables ($tables); $object-Setconditions ($conditions); if (! $object-Getquerysql ()) {echo $object-Error; exit;} else {$sql = $object and Result; Echo $sql. "
" ;} echo "Result Generate List with Search on all fields
" ; $fields = Array ("id", "name", "Address", "City"); $tables = Array ("Customer"); $id = 1; $search = "Fadjar Nurswanto"; $object-Clear_all_assign (); $object-Setfields ($fields); $object-Settables ($tables); $object-Setsearch ($search); if (! $object-Getquerysql ()) {echo $object-Error; exit;} else {$sql = $object and Result; Echo $sql. "
" ;} echo "Result Generate List with search on some fields
" ; $fields = Array ("id", "name", "Address", "City"); $tables = Array ("Customer"); $id = 1; $search = Array ("name" = "Fadjar Nurswanto", "address" = "Tomang Raya"); $object-Clear_all_assign (); $object-Setfields ($fields); $object-Settables ($tables); $object-Setsearch ($search); if (! $object-Getquerysql ()) {echo $object-Error; exit;} else {$sql = $object and Result; Echo $sql. "
" ;}? >

Class Code:

<?php/* Created By:fadjar Nurswanto
 
  
Date:2006-08-02productname:class Mylibsqlgenproductversion:1.0.0description:class Yang Berfungsi untu   K menggenerate sqldenpencies: */class mylibsqlgen{var $Result;   var $Tables = Array ();   var $Values = Array ();   var $Fields = Array ();   var $Conditions = Array ();   var $Condition;   var $LeftJoin = Array ();   var $Search;   var $Sort = "ASC";   var $Order;   var $Error;     function Mylibsqlgen () {} function Buildcondition () {$funct = "buildcondition";     $className = Get_class ($this);     $conditions = $this-Getconditions ();     if (! $conditions) {$this, Dbgdone ($FUNCT); return true;}       if (! Is_array ($conditions)) {$this, Error = "$className:: $funct Variable conditions not array";    return; } for ($i = 0; $i < count ($conditions); $i + +) {$this-Condition. = $conditions [$i] ["Cond  Ition "]." " . $conditions [$i] ["Connection"].    "  " ;  } return true;     } function Buildleftjoin () {$funct = "buildleftjoin";     $className = Get_class ($this); if (! $this-Getleftjoin ()) {$this, Error = "$className:: $funct property Leftjoin is empty";     return;}     $LeftJoinVars = $this-Getleftjoin ();     $hasil = false; foreach ($LeftJoinVars as $LeftJoinVar) {@ $hasil. = "Left JOIN".       $LeftJoinVar ["Table"]; foreach ($LeftJoinVar ["on"] as $var) {@ $condvar. = $var ["condition"]. "".      $var ["Connection"]. ""; } $hasil. = "On (". $condvar.       " ) " ;    Unset ($condvar);     } $this resultleftjoin = $hasil;  return true;     } function Buildorder () {$funct = "buildorder";     $className = Get_class ($this); if (! $this-GetOrder ()) {$this, Error = "$className:: $funct property Order is empty";     return;} if (! $this, GetFields ()) {$this-Error = "$className:: $funct property," was empty ";     return;}     $Fields = $this-GetFields ();     $Orders = $this-GetOrder ();     if (Ereg (",", $Orders)) {$Orders = Explode (",", $Order);}     if (! Is_array ($Orders)) {$Orders = array ($Orders);} foreach ($Orders as $Order) {if (! Is_numeric ($Order)) {$this-Error = "$className:: $funct propert Y Order not Numeric ";       return;} if ($Order > Count $this) {$this, Error = "$className:: $funct Max Value of property Sort is" . Count ($this, fields);      return;} @ $xorder. = $Fields [$Order].    " , " ; } $this-Resultorder = "ORDER by".     substr ($xorder, 0,-1);  return true;     } function Buildsearch () {$funct = "buildsearch";     $className = Get_class ($this); if (! $this-Getsearch ()) {$this, Error = "$className:: $funct property Search was empty";     return;} if (! $this-GetfielDS ()) {$this-Error = "$className:: $funct property is empty";     return;}     $Fields = $this-GetFields ();     $xvalue = $this-Getsearch ();           if (Is_array ($xvalue)) {foreach ($Fields as $field) {if (@ $xvalue [$field]) {           $Values = Explode ("", $xvalue [$field]); foreach ($Values as $Value) {@ $hasil. = $field. "Like '%". $Value.          "% ' or"; if ($hasil) {@ $hasil _final. = "(". substr ($hasil, 0,-4).             ") and";          Unset ($hasil);    }}} $hasil = $hasil _final;         } else {foreach ($Fields as $field) {$Values = Explode ("", $xvalue); foreach ($Values as $Value) {@ $hasil. = $field. "Like '%". $Value.        "% ' or";     }}} $this, Resultsearch = substr ($hasil, 0,-4); return True ;     } function Clear_all_assign () {$this, Result = null;     $this-resultsearch = null;     $this-resultleftjoin = null;     $this, Result = null;     $this-Tables = Array ();     $this, Values = Array ();     $this, fields = Array ();     $this-Conditions = Array ();     $this-Condition = null;     $this-leftjoin = Array ();     $this, Sort = "ASC";     $this, Order = null;     $this, Search = null;     $this-fieldsql = null;     $this-valuesql = null;     $this-partsql = null;     $this, Error = null;  return true;     } function Combinefieldvalue ($manual = False) {$funct = "Combinefieldspostvar";     $className = Get_class ($this);     $fields = $this-GetFields ();     $values = $this-GetValues ();       if (! Is_array ($fields)) {$this, Error = "$className:: $funct Variable fields not array";    return; } if (!Is_array ($values)) {$this, Error = "$className:: $funct Variable values not array";    return;  if (count ($fields) = count ($values)) {$this, Error = "$className:: $funct Count of Fields and       Values not match ";    return; } for ($i = 0; $i < count ($fields), $i + +) {@ $this-fieldsql. = $fields [$i].       " , " ;  if ($fields [$i] = = "PWD" | |  $fields [$i] = = "Password" | | $fields [$i] = = "pwd") {@ $this-valuesql. = "Password". $values [$i].        " '), " ; @ $this-Partsql. = $fields [$i]. "=password ('"). $values [$i].      " '), " ;  } else {if (Is_numeric ($values [$i])) {@ $this-valuesql. = $values [$i] .          " , " ; @ $this-Partsql. = $fields [$i]. " = " . $values [$i].        " , " ; } else {@ $this-valuesql. = "'". $values [$i].          " ', " ; @ $this-Partsql. = $fields [$i]. " =' " . $values [$i].        " ', " ;     }}} $this, Fieldsql = substr ($this, Fieldsql, 0,-1);     $this-Valuesql = substr ($this, Valuesql, 0,-1);     $this-Partsql = substr ($this, Partsql, 0,-1);  return true;     } function Getdeletesql () {$funct = "getdeletesql";     $className = Get_class ($this);     $Tables = $this-Gettables ();  if (! $Tables | | !       Count ($Tables)) {$this, dbgfailed ($FUNCT);       $this, Error = "$className:: $funct Table was empty";    return; } for ($i = 0; $i < count ($Tables), $i + +) {@ $Table. = $Tables [$i].    " , " ;     } $Table = substr ($Table, 0,-1); $sql = "DELETE from".     $Table;  if ($this, Getconditions ()) {if (! $this, Buildcondition ()) {$this, dbgfailed ($funct); return       ;} $sql. = "WHERE".$this-Getcondition ();     } $this Result = $sql;  return true;     } function Getinsertsql () {$funct = "getinsertsql";     $className = Get_class ($this); if (! $this-GetValues ()) {$this, Error = "$className:: $funct property Values is empty";     return;} if (! $this-GetFields ()) {$this, Error = "$className:: $funct property is empty";     return;} if (! $this-Gettables ()) {$this, Error = "$className:: $funct property Tables is empty";     return;}     if (! $this, Combinefieldvalue ()) {$this, dbgfailed ($funct); return;}     $Tables = $this-Gettables (); $sql = "INSERT into". $Tables [0]. " ( " . Fieldsql, $this. ") VALUES (". Valuesql, $this.     " ) " ;     $this, Result = $sql;  return true;     } function Getupdatesql () {$funct = "getupdatesql";     $className = Get_class ($this); if (! $this, GetValues ()) {$this-Error = "$className:: $funct property Values were empty";     return;} if (! $this-GetFields ()) {$this, Error = "$className:: $funct property is empty";     return;} if (! $this-Gettables ()) {$this, Error = "$className:: $funct property Tables is empty";     return;}     if (! $this, Combinefieldvalue ()) {$this, dbgfailed ($funct); return;}     if (! $this, Buildcondition ()) {$this, dbgfailed ($funct); return;}     $Tables = $this-Gettables (); $sql = "UPDATE". $Tables [0]. "SET". Partsql, $this. "WHERE".     $this-Getcondition ();     $this, Result = $sql;  return true;     } function Getquerysql () {$funct = "getquerysql";     $className = Get_class ($this); if (! $this-GetFields ()) {$this, Error = "$className:: $funct property is empty";     return;} if (! $this-Gettables ()) {$this, Error = "$className:: $funct property Tables is empty"; REturn;}     $Fields = $this-GetFields ();     $Tables = $this-Gettables (); foreach ($Fields as $Field) {@ $sql _raw. = $Field.     " , " ;} foreach ($Tables as $Table) {@ $sql _table. = $Table.     " , " ;} $this, Result = "select". substr ($sql _raw, 0,-1). "From".     substr ($sql _table, 0,-1); if ($this, Getleftjoin ()) {if (! $this, Buildleftjoins ()) {$this, dbgfailed ($funct); return; } $this, Result. = "".    $this, Resultleftjoin; if ($this, Getconditions ()) {if (! $this, Buildcondition ()) {$this, dbgfailed ($FUNCT);       return;} Result. = "WHERE (") $this. Condition, $this.    " ) " ; if ($this, Getsearch ()) {if (! $this, Buildsearch ()) {$this, dbgfailed ($funct); return       ;}  if ($this-Resultsearch) {if (Eregi ("WHERE", $this, result)) {$this, result. = "and" . $thiS--resultsearch;} else {$this, Result. = "WHERE".      $this-Resultsearch;} }} if ($this-GetOrder ()) {if (! $this, Buildorder ()) {$this, dbgfailed ($funct); Retu       RN;} $this, Result. = "".    $this, Resultorder; if ($this, Getsort ()) {if (@ $this-Resultorder) {$this, Result. = "".      $this-Getsort ();  }} return true;   } function Getcondition () {return @ $this-Condition;}  function Getconditions () {if (count (@ $this-Conditions) && Is_array (@ $this-Conditions)) {return @   $this-Conditions;}}  function GetFields () {if (count (@ $this, fields) && Is_array (@ $this)) {return @ $this   Fields;}}  function Getleftjoin () {if (count (@ $this-Leftjoin) && Is_array (@ $this-Leftjoin)) {return @ $this   Leftjoin;}} function GetOrder () {return @ $this-> Order;}   function Getsearch () {return @ $this, Search;}   function Getsort () {return @ $this, Sort;}  function Gettables () {if (count (@ $this-Tables) && Is_array (@ $this-Tables)) {return @ $this   Tables;}}  function GetValues () {if (count @ $this, values) && Is_array (@ $this, values) {return @ $this   Values;}}   function Setcondition ($input) {$this, Condition = $input;}     function Setconditions ($input) {if (Is_array ($input)) {$this, Conditions = $input;} else {$this-Error = Get_class ($this). ":: setconditions Parameter input not array";  return;}     } function Setfields ($input) {if (Is_array ($input)) {$this, fields = $input;} else {$this-Error = Get_class ($this). ":: Setfields Parameter input not array";  return;}     } function Setleftjoin ($input) {if (Is_array ($input)) {$this, leftjoin = $input;} else {$this-&Gt Error = Get_class ($this). ":: Setfields Parameter input not array";  return;}   } function Setorder ($input) {$this, Order = $input;}   function Setsearch ($input) {$this, Search = $input;}   function Setsort ($input) {$this, Sort = $input;}     function Settables ($input) {if (Is_array ($input)) {$this, Tables = $input;} else {$this-Error = Get_class ($this). ":: Settables Parameter input not array";  return;}     } function Setvalues ($input) {if (Is_array ($input)) {$this, Values = $input;} else {$this-Error = Get_class ($this). ":: setvalues Parameter input not array";  return;}  }}?>
 

More interested in PHP related content readers can view the topic: "PHP based on PDO Operation Database Skills summary", "PHP operation and operator Usage Summary", "PHP Network Programming Tips", "PHP Basic Grammar Primer Tutorial", "PHP operation Office Document Tips Summary ( including word,excel,access,ppt), PHP date and Time usage summary, PHP Primer for object-oriented programming, PHP string usage Summary, PHP+MYSQL database Operations Starter Tutorial, and A summary of common PHP database operation techniques

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

Articles you may be interested in:

    • 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
    • PHP implements a complete example of model base class based on mysqli
    • How PHP uses the Mysqli class library to achieve perfect page-splitting effects
    • PHP format MySQL return float type method
    • MySQL simple operation class for PHP
    • PHP Simple operation MySQL Database class

http://www.bkjia.com/PHPjc/1119968.html www.bkjia.com true http://www.bkjia.com/PHPjc/1119968.html techarticle PHP Implementation of the parameters generated by the MySQL statement class complete instance, the MySQL statement in this article describes the PHP implementation of the parameters generated by the MySQL statement class. Share to everyone for your reference, as follows ...

  • 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.