Page 1 of 1

Query Strings - detecting and accessing

Posted: Thu Feb 20, 2003 4:05 am
by WillowFae
Can any one tell me how to access a query string and how to test if it exists?

Basically when a user hits a page I need to test to see if a query string has been defined. If it is the first time they come to the page, then it won't be there and so needs to display a list of links which link to the same page but with a query string. So the second time they get there the query string will exist and it will pull a record from the database based on that query string.

My work in the past has been in ColdFusion and I know I would do it there by using isdefined("url.id") etc.

Thanks
WillowFae

Posted: Thu Feb 20, 2003 7:10 am
by WillowFae
An update on this - I have now "sort of" determined how to access the query string. Obviously using the method below brings everything after the .php? - how do I just get the bit after the =

I am also still not sure how to test to see if it has been defined

The code I have so far is

$memid = $_SERVER['QUERY_STRING'];

if (!isset($memid))
{
printf("no query string");
}
else
{
printf($memid);
}

If I manually add a query string to the end of the url in the address bar, then it processes fine and goes to the else statement. But if there isn't one, I get the following error

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING'

Posted: Thu Feb 20, 2003 7:56 am
by superwormy
All of the varialbes passed by the query string are set in the $_GET array.

So if you access this page:

http://www.domainname.com/index.php?myv ... econdvalue


Then $_GET['myvariable'] will equal 'mynewvalue'
Also $_GET['anothervar'] will equal 'mysecondvalue'


If you want soemthing other than that, you need to use $_SERVER['QUERY_STRING']

Posted: Thu Feb 20, 2003 8:18 am
by WillowFae
Excellent, thanks Keith. Just what I was after :)

And it has fixed the other problem of determining whether or not the query string exists :)