PHP Get ID from URL

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

PHP Get ID from URL

Post by tomsace »

Code: Select all

<?php
include('config.php'); 
// Performing SQL query
$query = 'SELECT * FROM games WHERE id = "1" ORDER BY id DESC';
$result = mysql_query($query) or die('Query failed: ' . mysql_error()); 
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
This is the code I currently have, it returns the results for 'id 1' but I want to be able to set which id I want from the url rather than change the code. (mysite.com/page.php?id=1 for example).
I have been searching the net for hours now trying different things, I have been trying to use something along the lines of:

Code: Select all

<?php
$id = $_get['id']; 
include('config.php'); 
// Performing SQL query
$query = "SELECT * FROM games WHERE id = '$id'";
$result = mysql_query($query) or die('Query failed: ' . mysql_error()); 
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
Which I think kind of works but then how do I use the results on my page? When I am using the code in the first example I use this to show results:

Code: Select all

<?php echo"$line[title]" ?>
Anyone see anything wrong or improvements I can make to the code or help showing results I would much appreciate it.
straightman
Forum Commoner
Posts: 48
Joined: Sun Apr 19, 2009 5:20 am

Re: PHP Get ID from URL

Post by straightman »

You need to pass it the variable that you have sent from the previous page, $whatever. that is, your WHERE = should be equal to the variable that was in the url link. WHERE id = $whatever

Straightman.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: PHP Get ID from URL

Post by tomsace »

Sorry I don't fully understand what you mean.
There isn't a previous page, I want to be able to access these pages by typing them in the url straight away if you know what I mean..??
mischievous
Forum Commoner
Posts: 71
Joined: Sun Apr 19, 2009 8:59 pm

Re: PHP Get ID from URL

Post by mischievous »

What straightman is talking about I believe is that to pass a variable is simple put something like

index.php?id=3
index.php?id=9
index.php?id=12

etc. etc.

thats using the get method. it doesnt have to be passed from a previous page it can be directly linked!

Hope that helps! :lol:


Also... be sure to restrict what goes into the variable or else you will be vulnerable to sql injections!

Code: Select all

 
//note you must be connected to the database for using this function
$id=mysql_real_escape_string($_GET['id']);
 
or

Code: Select all

 
$id = $_GET['id']; // user input from url
 
// define what to clean up in array
$badchars = array("\"", "\\", "/", "*", "'", "=", "-", "#", ";", "<", ">", "+", "%");
 
// clean user input (if it finds any of the values above, it will replace it with whatever is in the quotes - in this example, it replaces the value with nothing)
$myid = str_replace($badchars, "", $id); // works!
 
More info can be found at http://en.wikipedia.org/wiki/SQL_injection
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: PHP Get ID from URL

Post by susrisha »

Code: Select all

 
$query = "SELECT * FROM games WHERE id = '$id'";
 
i beleive there is an error in the above . the field id for the table seems to be an integer and if you place single quotes, it might not result in any mysql results.
try this instead

Code: Select all

 
$query = "SELECT * FROM games WHERE id = $id";
 
and there is an easier way i beleive to check if the id got from the url is a number.
i beleive isnumeric is the function.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: PHP Get ID from URL

Post by tomsace »

Thanks for the help everyone,
Thanks mischievous got it working using your code you game me!
mischievous
Forum Commoner
Posts: 71
Joined: Sun Apr 19, 2009 8:59 pm

Re: PHP Get ID from URL

Post by mischievous »

Glad I could help out! :D
Post Reply