zoukankan      html  css  js  c++  java
  • 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"; ?>

      

  • 相关阅读:
    WebFrom 复杂控件
    WebFrom 简单控件
    WinForm开发控件集合
    ListView 控件操作
    窗体类型
    WEBFORM--第一讲
    display:inline/block/inline-block
    WINFORM--第五讲(窗体类型)
    WINFORM--第四讲(布局)
    WINFORM--第三讲(下拉列表)
  • 原文地址:https://www.cnblogs.com/chenshuo/p/3619509.html
Copyright © 2011-2022 走看看