Query Strings
Using Query Strings:
Query strings are variable name/value pairs. Example:
www.example.com/page.php?fname=John&lname=Doe
The file "page.php" is being passed variables, along with their
respective values, using a query string. Here, the variables "fname"
and "lname" are being passed, with values "John" and
"Doe," respectively.
- question mark (?) indicates beginning of query string
- variable_name=value assignments follow question mark
- ampersand (&) symbol separates variable_name=value assignments
- requested page able to read variable/value pairs, and use them accordingly
- query strings are accessed using superglobal array $_GET
Example:
www.example.com/page.php?fname=John&lname=Doe
//access values of variables fname and lname in $_GET associative array (key/value pairs)
echo $_GET['fname']; //displays "John" (w/o quotation marks)
echo $_GET['lname']; //displays "Doe" (w/o quotation marks)
//or assign value local variable
$cusFname = $_GET['fname'];
$cusLname = $_GET['lname'];
perform some action on variables...
Example: Query String Process
<form method="GET" action="page.php">
First Name: <input type="text" name="fname" />
Last Name: <input type="text" name="lname" />
<input type="submit" value="submit" />
</form>
Example:
Two ways to pass variables to SQL queries:
1) Select * FROM customer WHERE cus_last='" . $_GET['lname'] . "'
2)
$cusLname = $_GET['lname'];
Select * FROM customer WHERE cus_last='$cusLname'
Note:
Difference in (') and ('") in examples above.
Note:
Can use POST/GET/REQUEST
References:
Using query strings in PHP
How to use the query string in PHP