Skip to content
Advertisement

How get value from URL [closed]

I want to get the value from the URL to choose data from the database by ID. I want the value for the id.

For example, if I were to open `www.example.com/index.php?id=12′. I want to get a value whose id = 12 in the database.

If I open `www.example.com/index.php?id=7′.

I want to get the value whose id = 7 in the 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;    
}

Advertisement

Answer

Website URL:

http://www.example.com/?id=2

Code:

$id = intval($_GET['id']);
$results = mysql_query("SELECT * FROM next WHERE id=$id");    
while ($row = mysql_fetch_array($results))     
{       
    $url = $row['url'];
    echo $url; //Outputs: 2
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement