问题
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