Page 1 of 1

query not allowing url id

Posted: Sat Apr 25, 2009 5:02 am
by tomsace
Hi,
Heres the code:

Code: Select all

$id = mysql_real_escape_string($_GET['id']);
// Performing SQL query
$query = 'SELECT * FROM games WHERE category = "$id" ORDER BY id DESC';
I am trying to get the ID from the url eg. categories.php?id=action and it will query action games.

This is what I dont understand, I am trying the above code and if I echo $id it prints the id that is in the url fine, but still doesn't seem to work in the category="$id"... If I change the category to = "action" it works fine, I change it back to = $id and it doesn't, even though it is still works when I echo "$id"...

Any ideas? Im puzzled!

Re: query not allowing url id

Posted: Sat Apr 25, 2009 5:23 am
by jazz090
u can only use "$id" by itself when u r dealing with numbers, in this case you have a string not a number so u must soround id by single quotes like this:

category = ' ".$id." ' (be sure to remove the whitespace between ' and this ", i put them there so its more clear to view) so what u want is this: category = '".$id."'

(start ur mysql command with doubles instead of singles)

Re: query not allowing url id

Posted: Sat Apr 25, 2009 11:33 am
by tomsace
Thanks alot! Works great!

Re: query not allowing url id

Posted: Sat Apr 25, 2009 2:46 pm
by McInfo
jazz090 wrote:category = ' ".$id." ' (be sure to remove the whitespace between ' and this ", i put them there so its more clear to view) so what u want is this: category = '".$id."'
If you use the tags, your code will be easy to read even without additional spaces.

Edit: This post was recovered from search engine cache.