Welcome Guest, Not a member yet? Register   Sign In
Variable in a SQL Query
#1

[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)?
#2

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

[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
#4

[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));




Theme © iAndrew 2016 - Forum software by © MyBB