Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unnecessary quotes adding on sql while executing - codeigniter

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).

like image 333
Sinto Avatar asked Feb 02 '26 00:02

Sinto


2 Answers

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

str_replace('"','',$string);
like image 156
Shyamali Avatar answered Feb 03 '26 14:02

Shyamali


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);
like image 43
Sinto Avatar answered Feb 03 '26 15:02

Sinto