CodeIgniter Forums
Variable in a SQL Query - 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: Variable in a SQL Query (/showthread.php?tid=38537)



Variable in a SQL Query - El Forum - 02-10-2011

[eluser]invision[/eluser]
Hi,

I'm using the following code to select data from my database:
Code:
$this->db->query("SELECT entry.id, city.*, country.* FROM entry, city WHERE  city.country = {$country}");

This query outputs as:
Quote:SELECT entry.id, city.*, country.* FROM entry, city WHERE city.country = gbr
but I need it as:
Quote:SELECT entry.id, city.*, country.* FROM entry, city WHERE city.country = 'gbr'


How do I change the query to treat gbr[b] as [b]'gbr'(a string)?


Variable in a SQL Query - El Forum - 02-10-2011

[eluser]JHackamack[/eluser]
why not use:
Code:
$this->db->query("SELECT entry.id, city.*, country.* FROM entry, city WHERE  city.country = '{$country}'");



Variable in a SQL Query - El Forum - 02-10-2011

[eluser]invision[/eluser]
Thanks for the reply. I'm sure I tried this before and it failed, but I'll give it a shot Smile


Variable in a SQL Query - El Forum - 02-10-2011

[eluser]Rick Jolly[/eluser]
Always make sure your values are escaped. Query bindings do that for you and it's cleaner:
Code:
$this->db->query('SELECT entry.id, city.*, country.* FROM entry, city WHERE  city.country = ?', array($country));