Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Sql与或在codeigniter中组合_Php_Mysql_Sql Server_Codeigniter_Join - Fatal编程技术网

Php Sql与或在codeigniter中组合

Php Sql与或在codeigniter中组合,php,mysql,sql-server,codeigniter,join,Php,Mysql,Sql Server,Codeigniter,Join,我想在codeigniter SQL查询中结合AND条件和OR条件 我正在根据用户输入创建一个where条件 当用户选择rent、iam passing或condition in WHERE(和)时,如下$WHERE['p.contract\u id=3或p.contract\u id']=2 这是我的密码 $where = array('p.status' => 1, 'pi.order' => 1); if(!$fil['city'] == 0){ $whe

我想在codeigniter SQL查询中结合AND条件和OR条件

我正在根据用户输入创建一个where条件

当用户选择rent、iam passing或condition in WHERE(和)时,如下
$WHERE['p.contract\u id=3或p.contract\u id']=2

这是我的密码

$where = array('p.status' => 1, 'pi.order' => 1);

      if(!$fil['city'] == 0){
    $where['p.city_id']=$fil['city'];   
  }

  if(!$fil['area'] == 0){
    $where['p.area_id']=$fil['area']; 
  }

  if(!$fil['type'] == 0){
    $where['p.type_id']=$fil['type']; 
  }


  if(!$fil['bed'] == 0 && !$fil['bed']== 10){
    $where['pd.bed']=$fil['bed']; 
  }
  if($fil['bed']== 10){
    $where['pd.bed >']=$fil['bed']; 
  }


  if(!$fil['bath'] == 0 && !$fil['bath'] == 10){
    $where['pd.bath']=$fil['bath']; 
  }
  if($fil['bath'] == 10){
    $where['pd.bath >']=$fil['bath']; 
  }


  if(!is_null($fil['rent'])){
    $where['p.contract_id = 3 OR p.contract_id']=2; 
  }
  if(!is_null($fil['sale'])){
    $where['p.contract_id']=1; 
  }

  if(!$fil['price_from'] ==""){
    $where['pd.price >']=$fil['price_from']; 
  }


        //real query
      $this->db->select('p.*, pd.title, pd.price, pd.bed, pd.bath, pd.size, pd.size_type, pd.full_description, 
  pi.name as image, pi.order, a.name as area, t.name as type_name, ct.name as contract_type, pv.views as views, 
  c.name as city');
  $this->db->from('property p'); 
  $this->db->join('property_detail pd', 'pd.property_id=p.property_id', 'left');
  $this->db->join('property_image pi', 'pi.property_id=p.property_id', 'left');
  $this->db->join('type t', 't.type_id=p.type_id', 'left');
  $this->db->join('contract_type ct', 'ct.con_typ_id=p.contract_id', 'left');
  $this->db->join('property_views pv', 'pv.property_id=p.property_id', 'left');
  $this->db->join('area a', 'a.area_id=p.area_id', 'left');
  $this->db->join('city c', 'c.city_id=p.city_id', 'left');
  $this->db->where($where);
  $this->db->order_by('p.property_id','desc'); 
  $this->db->limit($config['per_page'], $this->uri->segment(3));        
  $query = $this->db->get();
  $result = $query->result();
  return $result;
错误

Error Number: 1064
您的SQL语法有错误;查看与MySQL服务器版本对应的手册,了解第12行“2”附近使用的正确语法

SELECT    'p'.*, 
          'pd'.'title', 
          'pd'.'price', 
          'pd'.'bed', 
          'pd'.'bath', 
          'pd'.'size', 
          'pd'.'size_type', 
          'pd'.'full_description', 
          'pi'.'NAME' AS 'image', 
          'pi'.'ORDER', 
          'a'.'NAME'   AS 'area', 
          't'.'NAME'   AS 'type_name', 
          'ct'.'NAME'  AS 'contract_type', 
          'pv'.'views' AS 'views', 
          'c'.'NAME'   AS 'city' 
FROM      'property' 'p' 
LEFT JOIN 'property_detail' 'pd' 
ON        'pd'.'property_id'='p'.'property_id' 
LEFT JOIN 'property_image' 'pi' 
ON        'pi'.'property_id'='p'.'property_id' 
LEFT JOIN 'type' 't' 
ON        't'.'type_id'='p'.'type_id' 
LEFT JOIN 'contract_type' 'ct' 
ON        'ct'.'con_typ_id'='p'.'contract_id' 
LEFT JOIN 'property_views' 'pv' 
ON        'pv'.'property_id'='p'.'property_id' 
LEFT JOIN 'area' 'a' 
ON        'a'.'area_id'='p'.'area_id' 
LEFT JOIN 'city' 'c' 
ON        'c'.'city_id'='p'.'city_id' 
WHERE     'p'.'status' = 1 
AND       'pi'.'ORDER' = 1 
AND       'p'.'contract_id' = 3 
OR        p.contract_id 2

我很清楚这个错误是因为如果这个
$where['p.contract\u id=3或p.contract\u id']=2。有人能帮我通过codeigniter中的where条件或语句吗。Tnx.

这可能适合您。在这里,我将
或_where()
where()
放在where条件之后,用于检查您的
if
条件

SELECT `p`.*, `pd`.`title`, `pd`.`price`, `pd`.`bed`, `pd`.`bath`, `pd`.`size`, `pd`.`size_type`, `pd`.`full_description`, `pi`.`name` as `image`, `pi`.`order`, `a`.`name` as `area`, `t`.`name` as `type_name`, `ct`.`name` as `contract_type`, `pv`.`views` as `views`, `c`.`name` as `city` FROM `property` `p` LEFT JOIN `property_detail` `pd` ON `pd`.`property_id`=`p`.`property_id` LEFT JOIN `property_image` `pi` ON `pi`.`property_id`=`p`.`property_id` LEFT JOIN `type` `t` ON `t`.`type_id`=`p`.`type_id` LEFT JOIN `contract_type` `ct` ON `ct`.`con_typ_id`=`p`.`contract_id` LEFT JOIN `property_views` `pv` ON `pv`.`property_id`=`p`.`property_id` LEFT JOIN `area` `a` ON `a`.`area_id`=`p`.`area_id` LEFT JOIN `city` `c` ON `c`.`city_id`=`p`.`city_id` 
 WHERE `p`.`contract_id` IN ('3', '2') AND  `p`.`status` = 1 AND `pi`.`order` = 1 ;
      $this->db->distinct();
      $this->db->select('p.*, pd.title, pd.price, pd.bed, pd.bath, pd.size, pd.size_type, pd.full_description, 
      pi.name as image, pi.order, a.name as area, t.name as type_name, ct.name as contract_type, pv.views as views, 
      c.name as city',false);
      $this->db->from('property p'); 
      $this->db->join('property_detail pd', 'pd.property_id=p.property_id', 'left');
      $this->db->join('property_image pi', 'pi.property_id=p.property_id', 'left');
      $this->db->join('type t', 't.type_id=p.type_id', 'left');
      $this->db->join('contract_type ct', 'ct.con_typ_id=p.contract_id', 'left');
      $this->db->join('property_views pv', 'pv.property_id=p.property_id', 'left');
      $this->db->join('area a', 'a.area_id=p.area_id', 'left');
      $this->db->join('city c', 'c.city_id=p.city_id', 'left');
      $this->db->where($where);
      if(!is_null($fil['rent'])){
      $this->db->where('p.contract_id','3'); 
      $this->db->or_where('p.contract_id','2'); 
      }
      $this->db->order_by('p.property_id','desc'); 
      $this->db->limit($config['per_page'], $this->uri->segment(3));        
      $query = $this->db->get();
      $result = $query->result();
      return $result;
输出

SELECT `p`.*, `pd`.`title`, `pd`.`price`, `pd`.`bed`, `pd`.`bath`, `pd`.`size`, `pd`.`size_type`, `pd`.`full_description`, `pi`.`name` as image, `pi`.`order`, `a`.`name` as area, `t`.`name` as type_name, `ct`.`name` as contract_type, `pv`.`views` as views, `c`.`name` as city FROM (`property` p) LEFT JOIN `property_detail` pd ON `pd`.`property_id`=`p`.`property_id` LEFT JOIN `property_image` pi ON `pi`.`property_id`=`p`.`property_id` LEFT JOIN `type` t ON `t`.`type_id`=`p`.`type_id` LEFT JOIN `contract_type` ct ON `ct`.`con_typ_id`=`p`.`contract_id` LEFT JOIN `property_views` pv ON `pv`.`property_id`=`p`.`property_id` LEFT JOIN `area` a ON `a`.`area_id`=`p`.`area_id` LEFT JOIN `city` c ON `c`.`city_id`=`p`.`city_id` WHERE `p`.`status` = 1 AND `pi`.`order` = 1 AND `p`.`contract_id` = '3' OR `p`.`contract_id` = '2' ORDER BY `p`.`property_id` desc LIMIT 0

您可以尝试类似的方法:

$this->db->where('(p.contract_id = 3 OR p.contract_id =2) AND (...) ', NULL, FALSE);

第三个参数告诉ci,它不应该试图保护字段名。您必须小心注射问题。

尝试以这种方式发送
$where['(p.contract\u id=3或p.contract\u id=2)]='。不工作。取决于用户选择,如果用户选择
(销售)
its=1,否则(租金/租赁)=2或3,然后在(2,3)
中使用
p.contract\u id>$where['p.contract\u id']=2,3
,codeigniter给出语法错误。用户where_在()中而不是在此中的where()case@Sisodiatnx。但是它只选择p.contract_id 3.sorry第一次听到单词
vise versa
,如果我没有错的话。。。。。是的,我同时检查了
p.contract\u id',“2,3”
它只显示2和
p.contract\u id',“3,2”
它只显示3。检查我的更新答案。它会像您预期的那样工作,但它会为数据库中的每个条目创建7个重复项。tnx