Page 1 of 1
PHP Get ID from URL
Posted: Sun Apr 19, 2009 4:59 am
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:
Anyone see anything wrong or improvements I can make to the code or help showing results I would much appreciate it.
Re: PHP Get ID from URL
Posted: Sun Apr 19, 2009 5:32 am
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.
Re: PHP Get ID from URL
Posted: Sun Apr 19, 2009 6:11 pm
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..??
Re: PHP Get ID from URL
Posted: Sun Apr 19, 2009 9:08 pm
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!
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
Re: PHP Get ID from URL
Posted: Mon Apr 20, 2009 12:59 am
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.
Re: PHP Get ID from URL
Posted: Mon Apr 20, 2009 11:09 am
by tomsace
Thanks for the help everyone,
Thanks mischievous got it working using your code you game me!
Re: PHP Get ID from URL
Posted: Wed Apr 22, 2009 9:04 pm
by mischievous
Glad I could help out!
