query not allowing url id

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

query not allowing url id

Post 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!
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: query not allowing url id

Post 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)
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: query not allowing url id

Post by tomsace »

Thanks alot! Works great!
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: query not allowing url id

Post 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.
Post Reply