PHP方法字符串拼接转化mysql进行数据查询

来源:互联网 发布:微信开发 未备案域名 编辑:程序博客网 时间:2024/05/29 13:21

定义方法部分:

//定义数组  private $_getjoinstr = array();    /**     字段中包含 */  public function dbinstr($fiekd, $str, $spl1=',', $spl2=',')  {      return "instr(concat('$spl1', $fiekd, '$spl2'), '".$spl1.$str.$spl2."')>0";  }    //字符串拼接转化mysql方法  public function getjoinstr($fids, $us, $lx=0, $slx=0)  {      $s      = '';      //判断是否是数字      if(is_numeric($us)){          $key= 'a'.$fids.''.$us.'_'.$lx.'';          if(isset($this->_getjoinstr[$key]))return $this->_getjoinstr[$key];          //admin表中查询id、name、deptid、deptath          $us = $this->getone($us,'id,`name`,`deptid`,`deptpath`');      }      //如果没有数据,返回空      if(!$us)return '';      //有数据执行查询      $uid    = $us['id'];      $key    = 'a'.$fids.''.$uid.'_'.$lx.'';      if(isset($this->_getjoinstr[$key]))return $this->_getjoinstr[$key];      //判断是否为空 为空也执行查询       if($slx==0)$tj[]    = "ifnull($fids,'')=''";      //判断是否包含 all      $tj[]   = $this->dbinstr($fids, 'all');      //判断包含的u开头的人员信息      $tj[]   = $this->dbinstr($fids, 'u'.$uid);      //根据当前用户信息去查询当前用户所在组      if($us){          $dep = explode(',', $us['deptpath']);          foreach($dep as $deps){              $_deps  = str_replace(array('[',']'), array('',''), $deps);              $tj[]   = $this->dbinstr($fids, 'd'.$_deps);          }      }      //用or链接      $s  = join(' or ', $tj);      $this->_getjoinstr[$key] = $s;      return $s;  }  

调用部分:

$uid='1';//当前传入用户的ID  $where_userid = m('admin')->getjoinstr('userid', $uid);  if($where_userid) $s.= $where_userid;  //获取所有的步骤  $rows = $this->db->getall("SELECT * FROM [Q]flow_course WHERE " .$s. " ORDER BY sort,id ASC"); 



数据库表中字段存储:


原创粉丝点击