Unnecessary quotes adding on sql while executing - codeigniter

天大地大妈咪最大 提交于 2019-12-07 17:25:09

问题


while trying to do a select query I came into a situation where unnecessary quotes are injecting to it on execution. I'm working in Codeigniter. Trying to selects record which having first 4 characters same. Code is:

$calendar = $this->db->select("c.first_name as cfn, u.first_name as ufn", false)
        ->from("{$this->tables['contacts']} c")
        ->join("{$this->tables['users']} u", " SUBSTR( u.first_name , 1 , 4) = SUBSTR( c.first_name , 1 , 4) ", '')
        ->where(array('c.status' => 1, 'c.first_name !=' => ''))
        ->get()->result_array();

I'm getting an error as:

FUNCTION dbname.SUBSTR does not exist. Check the 'Function Name Parsing 
and Resolution' section in the Reference Manual

SELECT c.first_name as cfn, u.first_name as ufn FROM (`contacts` c) 
JOIN `users` u ON `SUBSTR`( `u`.`first_name` , 1 , 4) = SUBSTR( c.first_name , 1 , 4) 
 WHERE `c`.`status` = 1 AND `c`.`first_name` != ''

`SUBSTR` on query is unexciting(single quote for SUBSTR).


回答1:


I had the same problem once and I solved it by using

str_replace('"','',$string);



回答2:


In my case, I has to fix this issue by doing:

$calendar = $this->db->query("SELECT c.first_name as cfn, u.first_name as ufn 
FROM (`contacts` c) JOIN `users` u ON 
((SUBSTR(`u`.`first_name`, 1, 4)) = (SUBSTR(`c`.`first_name`, 1, 4))) 
WHERE `c`.`status` = 1 AND `c`.`first_name` != ''")->result_array();
print_r($calendar);


来源:https://stackoverflow.com/questions/43995039/unnecessary-quotes-adding-on-sql-while-executing-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!