I want to get value from URL to choose data from database by ID. I want value for id.
For example if I open www.example.com/index.php?id=12
I want to get value whose id = 12 in database.
If I open www.example.com/index.php?id=7
get value whose id = 7 in database and so on.
I have some code :
$results = mysql_query("SELECT * FROM next WHERE id=$id");
while ($row = mysql_fetch_array($results))
{
$url = $row['url'];
echo $url;
}
You can get that value by using the
$_GET
array. So the id value would be stored in$_GET['id']
.So in your case you could store that value in the
$id
variable as follows:You can also get a query string value as:
You can access those values with the global $_GET variable
You should check all "incoming" user data - so here, that "id" is an INT. Don't use it directly in your SQL (vulnerable to SQL injections).
Website URL:
Code:
There are two ways to get variable from URL in PHP:
When your url is:
http://www.example.com/index.php?id=7
you can get thisid
via$_GET['id']
or$_REQUEST['id']
command and store in$id
variable.Lest's take a look:
same will be:
difference is that variable can be passed to file via url or via POST method.
if variable is passed through url, then you can get it with
$_GET['variable_name']
or$_REQUEST['variable_name']
but if variable is posted, then you need to you$_POST['variable_name']
or$_REQUST['variable_name']
So as you see
$_REQUEST['variable_name']
can be used in both ways.P.S: Also remember - never do like this:
$results = mysql_query("SELECT * FROM next WHERE id=$id");
it may cause MySQL Injection and your database can be hacked.Try to use: