2015-02-11 48 views
0

如何在代碼中使用like子句iginiter query()WITH BIND PARAMS?如何在代碼中使用like子句iginiter query()WITH BIND PARAMS?

如: 當我使用

$query = 'SELECT mycol FROM mytable WHERE name LIKE %?'; 
$name = 'foo'; 
$db->this->query($query,array($name)); 
//the clause generated 
//SELECT mycol FROM mytable WHERE id LIKE '%'foo'%' 
//I expected this 
//SELECT mycol FROM mytable WHERE id LIKE '%foo' 

我不把PARAM值內查詢和使用如下: $查詢=「SELECT mycol FROM mytable的WHERE名稱LIKE '%富';

此外,作爲我的查詢包括我不能用$這個 - > DB->像()函數: INSERT忽略 和 INSERT INTO表的SELECT山坳FROM表2;

請指教? 謝謝,

回答

1
$query = 'SELECT mycol FROM mytable WHERE name LIKE ?'; 
$name = '%foo'; 
$this->db->query($query,array($name)); 
+0

這是正確的答案,但需要修復'$ DB->這 - > query'到'$這個 - > db-> query' – 2015-02-11 12:09:16

+0

謝謝,我剛剛從問題的作者複製它。現在它是正確的。 – pbogut 2015-02-11 13:23:10

1

笨將'params'值替換?。 如果你寫這

$query = 'SELECT mycol FROM mytable WHERE name LIKE %?'; 
$name = 'foo'; 
//$db->this->query($query,array($name)); //you wrote this line wrong. 
//it should be like this 
    $this->db->query($query,array($name)); 

會產生

SELECT mycol FROM mytable WHERE name LIKE %'foo' //inverse comma after % ,actually before and after foo. 

所以,你正確的方式將

$query = 'SELECT mycol FROM mytable WHERE name LIKE ?'; 
$name = '%foo';  
$this->db->query($query,array($name)); 

它會產生

SELECT mycol FROM mytable WHERE name LIKE '%foo' 


你寫了這是錯誤的

$db->this->query($query,array($name)); 

正道

$this->db->query($query,array($name));