The operation of CI on database sentences is sometimes very convenient and sometimes embarrassing, such as the problem of parentheses. The problem with parentheses comes from handling the precedence relationship of and and OR.
Sometimes it takes or above and. $this->db->where () and $this->db->orwhere () how to do it.
The first type: Write your own SQL statement. But sometimes the program that has been written is difficult to change.
The second type: write the WHERE statement yourself, and you may encounter such a problem.
where () and orwhere () are actually identical functions.
There is a paragraph in this manual:
$this->db->where ();
This function allows you to set the WHERE clause using one of four methods:
Note: All values passed to this function will be automatically escaped to generate a secure query.
Simple Key/value Method: $this->db->where (' name ', $name);
Build: WHERE name = ' Joe '
Note that the equals sign has been added for you.
If you call this function multiple times, these conditions are connected by and:
$this->db->where (' name ', $name);
$this->db->where (' title ', $title);
$this->db->where (' status ', $status);
WHERE name = ' Joe ' and title = ' Boss ' and ' status = ' active ' Custom Key/value method:
You can include an operator in the first parameter in order to control the comparison:
$this->db->where (' Name! = ', $name);
$this->db->where (' ID < ', $id);
Generated: WHERE name! = ' Joe ' and ID < 45 associative array method: $array = Array (' name ' = ' = ' $name, ' title ' = + $title, ' status ' = $status);
$this->db->where ($array);
Generated: WHERE name = ' Joe ' and title = ' Boss ' and status = ' active '
You can also include operators when using this method:
$array = Array (' name! = ' = $name, ' ID < ' + $id, ' date > ' = $date);
$this->db->where ($array); Custom string:
You can write the clauses manually:
$where = "Name= ' Joe ' and status= ' boss ' OR status= ' active ';
$this->db->where ($where);
$this->db->where () accepts the optional third parameter. If you set it to FALSE, CodeIgniter will not provide protection for your field names or table names that contain anti-tick numbers.
$this->db->where (' MATCH (field) against ("value") ', NULL, FALSE);
The red place is important, the 4th says:
$where = "Name= ' Joe ' and status= ' boss ' OR status= ' active ';
$this->db->where ($where);
Actually, it won't work. where () adds "' to the statement, and then says:
"' Name= ' Joe ' and status= ' boss ' OR status= ' active '" SQL statement is wrong.
The third type:
Change Source code:
CI Forum has such a paragraph:
... ...
Add parentheses to the like array.
Modify system\database\db_active_rec.php
About Line 1559th
Php
if (count ($this, Ar_like) > 0)
{
if (count ($this, Ar_where) > 0)
{
$sql. = "NAND";
}
$sql. = Implode ("n", $this-ar_like);
}
Switch
Php
if (count ($this, Ar_like) > 0)
{
if (count ($this, Ar_where) > 0)
{
$sql. = "NAND (";
}
$sql. = Implode ("n", $this-ar_like);
if (count ($this, Ar_where) > 0)
{
$sql. = ")";
}
}
I hope the CI developer can fix the bug that or_like can't form parentheses inside as soon as possible
This forum is very brain-disabled, show the return character \ n will drop a slash, understand PHP's own plus.
From: http://codeigniter.org.cn/forums/thread-1270-1-1.html
Similarly, you can modify where, but this is only the start and end of the where statement plus.
For the actual needs, several combined with the basic situation can be solved. I don't know if there's any better. Oh, that should be the direct repair of CI source code inside the parentheses formed the bug it