簡體   English   中英

在CodeIgniter中使用WHERE CONCAT和Active Record

[英]Using WHERE CONCAT with Active Record in CodeIgniter

我試圖插入的原始查詢是:

SELECT * FROM x WHERE CONCAT(y, ' ', x) LIKE '%value%';

我已經通過AR文檔檢查過,找不到任何可以讓我這樣做的東西。 我不太熟悉它是如何構建這些查詢的,並且希望有人可以指出我正確的方向。 謝謝一堆。

如果要使用AR類,則需要將FALSE作為第三個參數傳遞,以避免查詢自動轉義。 你現在可以自己逃避爭論了:

$value = $this->db->escape_like_str($unescaped);

$this->db->from('x');
$this->db->where("CONCAT(y, ' ', x) LIKE '%".$value."%'", NULL, FALSE);
$result = $this->db->get();

請參閱本手冊的Active Record會話中的第4點)。 引用:

   Custom string:

   You can write your own clauses manually:
   $where = "name='Joe' AND status='boss' OR status='active'";
   $this->db->where($where);
   $this->db->where() accepts an optional third parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks.
   $this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);

一種更簡單的方法,即imho,可以運行“常規”查詢並利用綁定:

$result = $this->db->query("CONCAT(y, ' ', x) LIKE '%?%'", array($value));

這樣的事情應該有效:

$this->db->where("CONCAT(y, ' ', x) LIKE '%value%'");

$this->db->get(x);

或者在不使用第三個參數的情況下使用關聯數組方法:

$a = array(
    'CONCAT(`y`, " ", `x`)' => $value,
    'title' => $title,
    ...
);
...
$this->db->like($a);

將生成WHERE部分查詢:
... WHERE CONCAT(`y`, " ", `x`) LIKE '%test value%' AND `title` LIKE '%test title%' AND ...
使用多個搜索參數時顯然很有用。

這已經過時了......

你可以試試這個:

$this->db->like('CONCAT(field_name," ",field_name_b)',$this->db->escape_like_str('value'));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM