多个Like子句CodeIgniter

时间:2014-04-23 20:14:03

标签: php mysql codeigniter

我正在尝试使用多个“赞”来在多个列中查找匹配项。

PHP& MySQL的:

$area = $this->session->userdata('area');
if($area == 'inbox'){

   $this->db->where('receiver',$user); 
   $this->db->where('receiver_deleted !=','yes');

   }elseif($area == 'sent'){
   $this->db->where('sender',$user); 
   $this->db->where('sender_deleted !=','yes');
}

$this->db->like('sender',$k);
$this->db->or_like('msg',$k); 


$this->db->from('messages');
$this->db->join('users','users.username = messages.sender','inner');

$this->db->order_by('read','no');
$sql = $this->db->get();

if($sql->num_rows > 0){

 return $sql->result();
}else{

 echo "failed!";
     }

我也试过了:

$this->db->like('sender',$k); 
$this->db->like('msg',$k); 

第一个没有任何反应。第二个只执行一个,在这种情况下第一个像。我无法让两者同时工作。

任何帮助将不胜感激, 麦克

5 个答案:

答案 0 :(得分:3)

or_like会在两个喜欢之间产生一个OR。您可以将数组传递给like,以便将多个数组与它们放在一起。

你有没有尝试过:

$this->db->like(array('sender' => $k, 'msg' => $k));

答案 1 :(得分:2)

这将在

之间使用OR搜索多个列
$this->db->or_like(array('sender' => $k, 'msg' => $k));/* LIKE OR LIKE */

答案 2 :(得分:0)

使用此:

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    // vector is just as convenient as an array to initialize
    vector<int> array = { 20, 34, 45, 2, 10 };

    cout << "The original elements of the array" << endl;
    // you can use range-based for loops with containers
    for (auto val : array)
        cout << val << ' ';
    cout << endl;

    int index;
    int number;

    cout << "Enter the index and number for insertion" << endl;
    cin >> index;
    cin >> number;

    // vector support insert, and many other operations
    array.insert( array.begin()+index, number );

    cout << "Elements after insertion" << endl;
    for (auto val : array)
        cout << val << ' ';
    cout << endl;
}

你会得到你想要的确切。

答案 3 :(得分:0)

为此,您应该使用Grouping where子句。

$area = $this->session->userdata('area');
if($area == 'inbox'){

   $this->db->where('receiver',$user); 
   $this->db->where('receiver_deleted !=','yes');

   }elseif($area == 'sent'){
   $this->db->where('sender',$user); 
   $this->db->where('sender_deleted !=','yes');
}
$this->db->group_start();  //group start
$this->db->like('sender',$k);
$this->db->or_like('msg',$k); 
$this->db->group_end();  //group ed

答案 4 :(得分:-1)

你可以这样做:

$this->db->get_where('tablename',$whereclause);

我遇到同样的问题并且有效。