当前位置:  开发笔记 > 后端 > 正文

PHP实例:PHP通过参数来生成MYSQL语句类

这个类可以通过具有参数的数组来构建MySQL查询语句。这个类可以通过指定的表和字段参数创建SELECT,INSERT,UPDATE和DELETE语句。这个类可以创建SQL语句的WHERE条件,像LIKE的查询语句,使用LEFTJOI...">

 

这个类可以通过具有参数的数组来构建MySQL查询语句。 
这个类可以通过指定的表和字段参数创建SELECT ,INSERT , UPDATE 和 DELETE 语句。 
这个类可以创建SQL语句的WHERE条件,像LIKE的查询语句,使用LEFT JOIN和ORDER 语句。 
例子:


/* ******************************************************************* 
Example file 
This example shows how to use the MyLibSQLGen class 

The example is based on the following MySQL table: 

CREATE TABLE customer ( 
id int(10) unsigned NOT NULL auto_increment, 
name varchar(60) NOT NULL default ”, 
address varchar(60) NOT NULL default ”, 
city varchar(60) 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 -> 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 -> 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 -> 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 -> 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 -> 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 -> Result; echo sql . ”
” ;} 
?> 

类代码: 
/* 
Created By : Fadjar Nurswanto  
DATE : 2006-08-02 
PRODUCTNAME : class MyLibSQLGen 
PRODUCTVERSION : 1.0.0 
DESCRIPTION : class yang berfungsi untuk menggenerate SQL 
DENPENCIES : 
*/ 
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 \nVariable conditions not Array ” ; 
return ; 

for ( i = 0 ; i
this -> Condition .= conditions [ i ][ " condition " ] . ” ” . conditions [ i ][ " connection " ] . ” ” ; 

return true ; 

function BuildLeftJoin() 

funct = ” BuildLeftJoin ” ; 
className = get_class ( this ); 
if ( ! this -> getLeftJoin()){ this -> Error = ” className::funct \nProperty LeftJoin was 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 \nProperty Order was empty ” ; return ;} 
if ( ! this -> getFields()){ this -> Error = ” className::funct \nProperty Fields 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 \nProperty Order not Numeric ” ; return ;} 
if ( Order > count ( this -> Fields)){ this -> Error = ” className::funct \nMax 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 \nProperty Search was empty ” ; return ;} 
if ( ! this -> getFields()){ this -> Error = ” className::funct \nProperty Fields was 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 \nVariable fields not Array ” ; 
return ; 

if ( ! is_array ( values )) 

this -> Error = ” className::funct \nVariable values not Array ” ; 
return ; 

if ( count ( fields ) != count ( values )) 

this -> Error = ” className::funct \nCount of fields and values not match ” ; 
return ; 

for ( i = 0 ; 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 \nTable was empty ” ; 
return ; 

for ( i = 0 ; 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 \nProperty Values was empty ” ; return ;} 
if ( ! this -> getFields()){ this -> Error = ” className::funct \nProperty Fields was empty ” ; return ;} 
if ( ! this -> getTables()){ this -> Error = ” className::funct \nProperty Tables was empty ” ; return ;} 

if ( ! this -> CombineFieldValue()){ this -> dbgFailed( funct ); return ;} 
Tables = this -> getTables(); 

sql = ” INSERT INTO ” . Tables [ 0 ] . ” ( ” . this -> fieldSQL . ” ) VALUES ( ” . this -> valueSQL . ” ) ” ; 

this -> Result = sql ; 

return true ; 

function getUpdateSQL() 

funct = ” getUpdateSQL ” ; 
className = get_class ( this ); 

if ( ! this -> getValues()){ this -> Error = ” className::funct \nProperty Values was empty ” ; return ;} 
if ( ! this -> getFields()){ this -> Error = ” className::funct \nProperty Fields was empty ” ; return ;} 
if ( ! this -> getTables()){ this -> Error = ” className::funct \nProperty Tables was empty ” ; return ;} 

if ( ! this -> CombineFieldValue()){ this -> dbgFailed( funct ); return ;} 
if ( ! this -> BuildCondition()){ this -> dbgFailed( funct ); return ;} 
Tables = this -> getTables(); 

sql = ” UPDATE ” . Tables [ 0 ] . ” SET ” . this -> partSQL . ” WHERE ” . this -> getCondition(); 

this -> Result = sql ; 

return true ; 

function getQuerySQL() 

funct = ” getQuerySQL ” ; 
className = get_class ( this ); 

if ( ! this -> getFields()){ this -> Error = ” className::funct \nProperty Fields was empty ” ; return ;} 
if ( ! this -> getTables()){ this -> Error = ” className::funct \nProperty Tables was 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 ;} 
this -> Result .= ” WHERE ( ” . this -> Condition . ” ) ” ; 

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 ); return ;} 
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 -> Fields)){ 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 \nParameter input not array ” ; return ;} 

function setFields( input ) 

if ( is_array ( input )){ this -> Fields = input ;} 
else { this -> Error = get_class ( this ) . ” ::setFields \nParameter input not array ” ; return ;} 

function setLeftJoin( input ) 

if ( is_array ( input )){ this -> LeftJoin = input ;} 
else { this -> Error = get_class ( this ) . ” ::setFields \nParameter 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 \nParameter input not array ” ; return ;} 

function setValues( input ) 

if ( is_array ( input )){ this -> Values = input ;} 
else { this -> Error = get_class ( this ) . ” ::setValues \nParameter input not array ” ; return ;} 


?>


推荐阅读
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • PHP 编程疑难解析与知识点汇总
    本文详细解答了 PHP 编程中的常见问题,并提供了丰富的代码示例和解决方案,帮助开发者更好地理解和应用 PHP 知识。 ... [详细]
  • PHP 5.2.5 安装与配置指南
    本文详细介绍了 PHP 5.2.5 的安装和配置步骤,帮助开发者解决常见的环境配置问题,特别是上传图片时遇到的错误。通过本教程,您可以顺利搭建并优化 PHP 运行环境。 ... [详细]
  • 深入理解 SQL 视图、存储过程与事务
    本文详细介绍了SQL中的视图、存储过程和事务的概念及应用。视图为用户提供了一种灵活的数据查询方式,存储过程则封装了复杂的SQL逻辑,而事务确保了数据库操作的完整性和一致性。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 本文探讨了适用于Spring Boot应用程序的Web版SQL管理工具,这些工具不仅支持H2数据库,还能够处理MySQL和Oracle等主流数据库的表结构修改。 ... [详细]
  • 本文介绍了如何使用 PostgreSQL 的 `UPDATE ... FROM` 语法,通过映射表实现对多行记录进行高效的批量更新。这种方法不仅适用于单列更新,还支持多列的同时更新。 ... [详细]
  • 本文详细介绍了如何使用libpq库与PostgreSQL后端建立连接。通过探讨PQconnectdb()函数的工作原理及其在实际应用中的使用方法,帮助读者理解并掌握建立高效、稳定的数据库连接的关键步骤。 ... [详细]
  • Windows服务与数据库交互问题解析
    本文探讨了在Windows 10(64位)环境下开发的Windows服务,旨在定期向本地MS SQL Server (v.11)插入记录。尽管服务已成功安装并运行,但记录并未正确插入。我们将详细分析可能的原因及解决方案。 ... [详细]
  • SQL中UPDATE SET FROM语句的使用方法及应用场景
    本文详细介绍了SQL中UPDATE SET FROM语句的使用方法,通过具体示例展示了如何利用该语句高效地更新多表关联数据。适合数据库管理员和开发人员参考。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 构建基于BERT的中文NL2SQL模型:一个简明的基准
    本文探讨了将自然语言转换为SQL语句(NL2SQL)的任务,这是人工智能领域中一项非常实用的研究方向。文章介绍了笔者在公司举办的首届中文NL2SQL挑战赛中的实践,该比赛提供了金融和通用领域的表格数据,并标注了对应的自然语言与SQL语句对,旨在训练准确的NL2SQL模型。 ... [详细]
  • 本文详细介绍了HTML中标签的使用方法和作用。通过具体示例,解释了如何利用标签为网页中的缩写和简称提供完整解释,并探讨了其在提高可读性和搜索引擎优化方面的优势。 ... [详细]
  • 数据库内核开发入门 | 搭建研发环境的初步指南
    本课程将带你从零开始,逐步掌握数据库内核开发的基础知识和实践技能,重点介绍如何搭建OceanBase的开发环境。 ... [详细]
  • 使用C#开发SQL Server存储过程的指南
    本文介绍如何利用C#在SQL Server中创建存储过程,涵盖背景、步骤和应用场景,旨在帮助开发者更好地理解和应用这一技术。 ... [详细]
author-avatar
呼噜垃圾桶
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有