![]() |
escaping sql - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: escaping sql (/showthread.php?tid=14189) |
escaping sql - El Forum - 12-22-2008 [eluser]mradlmaier[/eluser] Hi All, I have the following sql: Code: $where = "(strcmp(soundex('" . $username . "'), soundex(klang)) = 0) OR username LIKE '%" . $username . "%'"; Code: $where = "(strcmp(soundex('" . $this->db->escape($username) . "'), soundex(klang)) = 0) OR username LIKE '%" . $this->db->escape($username) . "%'"; and that will produce this error Quote:A Database Error Occurred So how do i correctly escape the above...? Michael escaping sql - El Forum - 12-22-2008 [eluser]anggie[/eluser] according to the user_guide, you should try this.. Code: $where = "(strcmp(soundex(" . $this->db->escape($username) . "), soundex(klang)) = 0) OR username LIKE '%" . $this->db->escape($username) . "%'"; escaping sql - El Forum - 12-22-2008 [eluser]Henry Weismann[/eluser] Notice the single quotes: Code: strcmp(soundex(’‘michi’‘), soundex(klang)) = 0) OR username LIKE ‘%‘michi’%’ LIMIT 10 $this->db->escape() This function determines the data type so that it can escape only string data. It also automatically adds single quotes around the data so you don't have to. The problem is that escape is adding single quotes and you have added them as well which ends up with a bad sql statement. anggie is right but the like part may need to be different. escaping sql - El Forum - 12-22-2008 [eluser]Henry Weismann[/eluser] Try: Code: $where = "(strcmp(soundex(" . $this->db->escape($username) . "), soundex(klang)) = 0) OR username LIKE '%" . $this->db->escape_str($username) . "%'"; |