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不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
包子漫画网页版入口与全集阅读指南_正版免费漫画快速访问方法
包子漫画网页版入口与全集阅读指南_正版免费漫画快速访问方法

本专题汇总了包子漫画官网和网页版入口,提供最新章节抢先看方法、正版免费阅读指南,以及稳定访问方式,帮助用户快速直达包子漫画页面,无广告畅享全集漫画内容。

13

2026.02.10

MC.JS网页版快速畅玩指南_MC.JS官网在线入口及免安装体验方法
MC.JS网页版快速畅玩指南_MC.JS官网在线入口及免安装体验方法

本专题汇总了MC.JS官网入口和网页版快速畅玩方法,提供免安装访问、不同版本(1.8.8、1.12.8)在线体验指南,以及正版网页端操作说明,帮助玩家轻松进入MC.JS世界,实现即时畅玩与高效体验。

10

2026.02.10

谷歌邮箱网页版登录与注册全指南_Gmail账号快速访问与安全操作教程
谷歌邮箱网页版登录与注册全指南_Gmail账号快速访问与安全操作教程

本专题汇总了谷歌邮箱网页版的最新登录入口和注册方法,详细提供官方账号快速访问方式、网页版操作教程及安全登录技巧,帮助用户轻松管理Gmail邮箱账户,实现高效、安全的邮箱使用体验。

3

2026.02.10

铁路12306订票与退改全攻略_高效购票与座位选取技巧
铁路12306订票与退改全攻略_高效购票与座位选取技巧

本专题全面汇总铁路12306订票、退票、改签及候补订单操作技巧,提供车厢座位分布参考、抢票攻略和高铁安检注意事项,帮助新手用户快速掌握高效购票与退改流程,提高出行效率和体验。

6

2026.02.10

TensorFlow2深度学习模型实战与优化
TensorFlow2深度学习模型实战与优化

本专题面向 AI 与数据科学开发者,系统讲解 TensorFlow 2 框架下深度学习模型的构建、训练、调优与部署。内容包括神经网络基础、卷积神经网络、循环神经网络、优化算法及模型性能提升技巧。通过实战项目演示,帮助开发者掌握从模型设计到上线的完整流程。

0

2026.02.10

Vue3组合式API与组件开发实战
Vue3组合式API与组件开发实战

本专题讲解 Vue 3 组合式 API 的核心概念与应用技巧,深入分析响应式系统、生命周期管理、组件设计与复用策略。通过完整项目案例,指导前端开发者实现高性能、结构清晰的 Vue 应用,提升开发效率与代码可维护性。

4

2026.02.10

Go语言微服务架构与gRPC实战
Go语言微服务架构与gRPC实战

本专题面向有 Go 基础的开发者,系统讲解微服务架构设计与 gRPC 的高效应用。内容涵盖服务拆分、RPC 通信、负载均衡、错误处理、服务注册与发现等关键技术。通过实战案例,帮助开发者搭建高性能、可扩展的 Go 微服务系统。

1

2026.02.10

React 18状态管理与Hooks高级实践
React 18状态管理与Hooks高级实践

本专题专注于 React 18 的高级开发技术,详细讲解 useState、useEffect、useReducer、useContext 等 Hooks 的使用技巧,以及 Redux、Zustand 等状态管理工具的集成与优化方法。通过真实案例,帮助前端开发者构建可维护、性能优良的现代 React 应用。

4

2026.02.10

Node.js后端开发与Express框架实践
Node.js后端开发与Express框架实践

本专题针对初中级 Node.js 开发者,系统讲解如何使用 Express 框架搭建高性能后端服务。内容包括路由设计、中间件开发、数据库集成、API 安全与异常处理,以及 RESTful API 的设计与优化。通过实际项目演示,帮助开发者快速掌握 Node.js 后端开发流程。

1

2026.02.10

热门下载

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

精品课程

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

共22课时 | 3.6万人学习

AngularJS教程
AngularJS教程

共24课时 | 3.5万人学习

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

共15课时 | 3.3万人学习

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

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