PHP处理多表查询时的SQL语句拆分与重新组装

在自己写框架时候会发现,多表查询组装SQL语句
<?php $pre = "pre_"; $aid = "44"; $data = array("user.username,comment.uid,article.aid","user,comment,article","user.uid=comment.uid AND article.aid=comment.aid AND comment.aid=$aid"); //字段判断 if($data["0"] === "*"){ $filed = "*"; }else{ $arr = explode(",",$data["0"]); $length = count($arr); for($i=0; $i<$length; $i++){ $filed .= $pre.$arr[$i].","; } $filed = substr($filed, 0,-1); } //表名判断 if(strpos($data["1"],",") > 1){ $arr = explode(",",$data["1"]); $length = count($arr); for($i=0; $i<$length; $i++){ $table .= $pre.$arr[$i].","; } $table = substr($table, 0,-1); }else{ $table = $pre.$data["1"]; } //条件判断 $one = explode(" AND ", $data["2"]); $len1 = count($one); for($i=0; $i<$len1; $i++){ $two[] = explode("=", $one[$i]); } $len2 = count($two); for($i=0; $i<$len2; $i++){ for($j=0; $j<$len2-1; $j++){ if(strpos($two[$i][$j],".") > 1){ $where .= $pre.$two[$i][$j]."="; }else{ $where .= $two[$i][$j]; } } } $where = explode("=",$where); $len3 = count($where); for($i=0; $i<$len3; $i++){ if($i%2 == 0){ $c .=$where[$i]."="; }else{ $c .=$where[$i]." AND "; } } $where = substr($c, 0,-4); echo $query = "SELECT $filed FROM $table WHERE $where"; ?>

  

转载于:https://www.cnblogs.com/chenshuo/p/3619509.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值