0

0

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

php中文网

php中文网

发布时间:2016-06-21 08:51:08

|

1055人浏览过

|

来源于php中文网

原创

 

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


php 
/* ******************************************************************* 
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 . ”
” ;} 
?> 

类代码: 
php 
/* 
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 ;} 


?>



PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法

本专题系统整理pixiv网页版官网入口及登录访问方式,涵盖官网登录页面直达路径、在线阅读入口及快速进入方法说明,帮助用户高效找到pixiv官方网站,实现便捷、安全的网页端浏览与账号登录体验。

1044

2026.02.13

微博网页版主页入口与登录指南_官方网页端快速访问方法
微博网页版主页入口与登录指南_官方网页端快速访问方法

本专题系统整理微博网页版官方入口及网页端登录方式,涵盖首页直达地址、账号登录流程与常见访问问题说明,帮助用户快速找到微博官网主页,实现便捷、安全的网页端登录与内容浏览体验。

334

2026.02.13

Flutter跨平台开发与状态管理实战
Flutter跨平台开发与状态管理实战

本专题围绕Flutter框架展开,系统讲解跨平台UI构建原理与状态管理方案。内容涵盖Widget生命周期、路由管理、Provider与Bloc状态管理模式、网络请求封装及性能优化技巧。通过实战项目演示,帮助开发者构建流畅、可维护的跨平台移动应用。

213

2026.02.13

TypeScript工程化开发与Vite构建优化实践
TypeScript工程化开发与Vite构建优化实践

本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。

35

2026.02.13

Redis高可用架构与分布式缓存实战
Redis高可用架构与分布式缓存实战

本专题围绕 Redis 在高并发系统中的应用展开,系统讲解主从复制、哨兵机制、Cluster 集群模式及数据分片原理。内容涵盖缓存穿透与雪崩解决方案、分布式锁实现、热点数据优化及持久化策略。通过真实业务场景演示,帮助开发者构建高可用、可扩展的分布式缓存系统。

111

2026.02.13

c语言 数据类型
c语言 数据类型

本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

77

2026.02.12

雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法
雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法

本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。

17

2026.02.12

豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法
豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法

本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。

813

2026.02.12

PostgreSQL性能优化与索引调优实战
PostgreSQL性能优化与索引调优实战

本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。

97

2026.02.12

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
JavaScript高级框架设计视频教程
JavaScript高级框架设计视频教程

共22课时 | 3.6万人学习

AngularJS教程
AngularJS教程

共24课时 | 3.8万人学习

CSS3实现按钮特效视频教程
CSS3实现按钮特效视频教程

共15课时 | 3.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号