PDA

View Full Version : Getting A string from the URL and assigning it to a variable?


chrisgray999
12-14-2002, 05:35 PM
Hi, I Know a bit of php and want to get something from the end of a URL

e.g. http://www.mysite.com/get.php?ID=tft

and then if possible connect to a database (via MySQL) and get the relevent field to be displayed in the page.

Is this possible?

My Example:

if url= blabla.com/get.php?ID=tft

then to get the field from the database and display whatever I want

I hope this is clear, because I have found it hard to explain it! If this is not possible, could someone tell me how to just get the variable from the URL?

Thanks in advance

HormonX
12-14-2002, 05:49 PM
of course it's possible. MySQL query would look like this;


$query = "SELECT * FROM tablename WHERE ID='$ID'";


if you only want to print the ID to the browser just do this;


<?
echo "My ID is $ID";
?>




Hope this helps :)

HormonX

firepages
12-15-2002, 02:58 AM
HormonoX is right but you really need to get used to using the 'new' superglobals, which for a URL valiable is a GET variable and should be accessed like...

<?

$ID=$_GET['ID'];
?>


if you have an older PHP version you can use
$HTTP_GET_VARS['ID'];
but thats depracated.

eventually most hosts will be following the guidelines and turning the register_globals php configuration setting to 'OFF'

this stops all GET,POST,COOKIE etc variables from being automatically registered and will mean that

echo $ID wont work whilst echo $_GET['ID'] will.

Ökii
12-16-2002, 01:37 PM
the long version $HTTP_GET_VARS['varname']; should be used if your host hasn't upgraded/switched reg_globals off in scripts rather than just $varname.

This is simply because updating your scripts will be much much much easier. A search replace (most decent editors have this functionality) of
find : $HTTP_GET_VARS
replace : $_GET

would be all you'd need.

If you just used $varname, you'd need to reference every single one individually.