您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > PHP通过参数来生成MYSQL语句类
PHP通过参数来生成MYSQL语句类这个类可以通过具有参数的数组来构建MySQL查询语句。这个类可以通过指定的表和字段参数创建SELECT,INSERT,UPDATE和DELETE语句。这个类可以创建SQL语句的WHERE条件,像LIKE的查询语句,使用LEFTJOIN和ORDER语句。例子:?php/********************************************************************ExamplefileThisexampleshowshowtousetheMyLibSQLGenclassTheexampleisbasedonthefollowingMySQLtable:CREATETABLEcustomer(idint(10)unsignedNOTNULLauto_increment,namevarchar(60)NOTNULLdefault”,addressvarchar(60)NOTNULLdefault”,cityvarchar(60)NOTNULLdefault”,PRIMARYKEY(cust_id))TYPE=MyISAM;********************************************************************/require_once(”class_mylib_SQLGen-1.0.php”);fields=Array(”name”,”address”,”city”);values=Array(”Fadjar”,”ResultmangRayaStreet”,”Jakarta”);tables=Array(”customer”);echo”bResultGenerateInsert/bbr”;object=newMyLibSQLGen();object-clear_all_assign();//torefreshallpropertybutitnoneedwhenfirsttimeexecuteobject-setFields(fields);object-setValues(values);object-setTables(tables);if(!object-getInsertSQL()){echoobject-Error;exit;}else{sql=object-Result;echosql.”br”;}echo”bResultGenerateUpdate/bbr”;fields=Array(”name”,”address”,”city”);values=Array(”Fadjar”,”ResultmangRayaStreet”,”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()){echoobject-Error;exit;}else{sql=object-Result;echosql.”br”;}echo”bResultGenerateDelete/bbr”;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()){echoobject-Error;exit;}else{sql=object-Result;echosql.”br”;}echo”bResultGenerateList/bbr”;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()){echoobject-Error;exit;}else{sql=object-Result;echosql.”br”;}echo”bResultGenerateListwithsearchonallfields/bbr”;fields=Array(”id”,”name”,”address”,”city”);tables=Array(”customer”);id=1;search=”FadjarNurswanto”;object-clear_all_assign();object-setFields(fields);object-setTables(tables);object-setSearch(search);if(!object-getQuerySQL()){echoobject-Error;exit;}else{sql=object-Result;echosql.”br”;}echo”bResultGenerateListwithsearchonsomefields/bbr”;fields=Array(”id”,”name”,”address”,”city”);tables=Array(”customer”);id=1;search=Array(”name”=”FadjarNurswanto”,”address”=”TomangRaya”);object-clear_all_assign();object-setFields(fields);object-setTables(tables);object-setSearch(search);if(!object-getQuerySQL()){echoobject-Error;exit;}else{sql=object-Result;echosql.”br”;}?类代码:?php/*CreatedBy:FadjarNurswantofajr_n@rindudendam.netDATE:2006-08-02PRODUCTNAME:classMyLibSQLGenPRODUCTVERSION:1.0.0DESCRIPTION:classyangberfungsiuntukmenggenerateSQLDENPENCIES:*/classMyLibSQLGen{varResult;varTables=Array();varValues=Array();varFields=Array();varConditions=Array();varCondition;varLeftJoin=Array();varSearch;varSort=”ASC”;varOrder;varError;functionMyLibSQLGen(){}functionBuildCondition(){funct=”BuildCondition”;className=get_class(this);conditions=this-getConditions();if(!conditions){this-dbgDone(funct);returntrue;}if(!is_array(conditions)){this-Error=”className::funct\nVariableconditionsnotArray”;return;}for(i=0;icount(conditions);i++){this-Condition.=conditions[i][condition].””.conditions[i][connection].””;}returntrue;}functionBuildLeftJoin(){funct=”BuildLeftJoin”;className=get_class(this);if(!this-getLeftJoin()){this-Error=”className::funct\nPropertyLeftJoinwasempty”;return;}LeftJoinVars=this-getLeftJoin();hasil=false;foreach(LeftJoinVarsasLeftJoinVar){@hasil.=”LEFTJOIN”.LeftJoinVar[table];foreach(LeftJoinVar[on]asvar){@condvar.=var[condition].””.var[connection].””;}hasil.=”ON(”.condvar.”)”;unset(condvar);}this-ResultLeftJoin=hasil;returntrue;}functionBuildOrder(){funct=”BuildOrder”;className=get_class(this);if(!this-getOrder()){this-Error=”className::funct\nPropertyOrderwasempty”;return;}if(!this-getFields()){this-Error=”className::funct\nPropertyFieldswasempty”;return;}Fields=this-getFields();Orders=this-getOrder();if(ereg(”,”,Orders)){Orders=explode(”,”,Order);}if(!is_array(Orders)){Orders=Array(Orders);}foreach(OrdersasOrder){if(!is_numeric(Order)){this-Error=”className::funct\nPropertyOrdernotNumeric”;return;}if(Ordercount(this-Fields)){this-Error=”className::funct\nMaxvalueofpropertySortis”.count(this-Fields);return;}@xorder.=Fields[Order].”,”;}this-ResultOrder=”ORDERBY”.substr(xorder,0,-1);returntrue;}functionBuildSearch(){funct=”BuildSearch”;className=get_
本文标题:PHP通过参数来生成MYSQL语句类
链接地址:https://www.777doc.com/doc-3650524 .html