0

In mysql there is select * from table where condition and (condition or condition). Does this also has a code for codeigniter? example pseudo code

$this->db->select("*");
$this->db->from("table");
$this->db->where("condition", $var):
.... now the second where should be --> and (condition or condition)
Vincent
  • 852
  • 6
  • 29
  • 67
  • what about the or_where? or you can just use the query function to write your whatever. – dansasu11 Jun 17 '14 at 15:23
  • I guess my answer got upvoted but I just realized I don't think it actually answers your question - check these out: http://stackoverflow.com/questions/12981351/codeigniter-and-or-and also http://stackoverflow.com/questions/11056303/combining-mysql-and-or-queries-in-codeigniter – Dan Jun 17 '14 at 16:06

2 Answers2

2

To query by multiple conditions pass them in an array like this:

$this->db->select('*');
$this->db->from('table');
$this->db->where(array('condition'=>$var_1, 'condition_2'=>$var_2, 'condition_3'=> $var_3)):
$result = $this->db->get()->result_array(); //returns result as array

However it depends on your specific query, as certain types of queries/conditions don't work in this type of format.

EDIT: this is what you are looking for though:

$this->db->select('*');
$this->db->from('table');
$this->db->where('condition_1'=>$var_1);
$this->db->where("(condition_2=1 OR condition_2=2)", NULL, FALSE);
$query = $this->db->get()->result_array();
Dan
  • 9,391
  • 5
  • 41
  • 73
1

Hope this works

$this->db->select("*");
$this->db->where('condition',$var);
$this->db->or_where(array('condition'=>$var,'condition'=>$var));
$qry = $this->db->get("table");
print_r($qry->result());

All the best.

Siri
  • 1,344
  • 2
  • 10
  • 10
  • you have to get a `result` or `row`, if you just do `->get` it won't work – Dan Jun 17 '14 at 15:39
  • i expect him to know that..wat is wanted is the precedence of "and" and "or"...thanks for the suggestion anyways..i updated for better – Siri Jun 17 '14 at 15:43
  • it should be $this->db->where() where in mysql it is where condition AND (condition or condition) – Vincent Jun 17 '14 at 16:05
  • it works exactly lyk dat...where condition and (condition or condition)...it is tested and works perfectly.. – Siri Jun 17 '14 at 17:37