Get a better view
Passing variables
Let’s take a different view now and consider how information can be passed to another PHP page. One method is by using forms as we have done already; another is by using query strings. What are query strings? Change the line method= »post » to method= »get » in our script input.php. Now try submitting new data into the database with it. After clicking submit you will see our familiar « Thank you! Information entered » in the browser. But look at the URL. It looks something like:
http://yourhost/input.php?irst=Rick&last=Denver&nickname=Mike&email=j@xyz.com&salary=25000
Look closely. Now the information is passed as a string in the URL instead of posting directly. The sentence after the ? is the query string, and as you can see it contains the name of the variable and its values. When PHP receives a query string like ?first=John it automatically creates a variable named $first and assigns the value from the query string to it. So it is equivalent to $first= »John »; When more than one variable is present, the variables are separated by an ampersand (&).
Viewing individual rows
So now we will create a script that will display the information of a particular row in our database defined by the variable $id. Save the following code as view.php. Try viewing it through your Web server as http://yourhost/view.php?id=2 (here we have passed the variable $id=2 through the query string). The page should show information corresponding to the id 2 in the MySQL database.
<HTML>
<?php
$db = mysql_connect(« localhost », « root », « »);
mysql_select_db(« learndb »,$db);
$result = mysql_query(« SELECT * FROM personnel WHERE id=$id »,$db);
$myrow = mysql_fetch_array($result);
echo « First Name: « .$myrow[« firstname »];
echo « <br>Last Name: « .$myrow[« lastname »];
echo « <br>Nick Name: « .$myrow[« nick »];
echo « <br>Email address: « .$myrow[« email »];
echo « <br>Salary: « .$myrow[« salary »];
?>
</HTML>
Here the SQL command has changed and it tells the database to search for the row that has the value $id. But can’t multiple rows contain the same values of id? Generally a column can contain any value, the same or different. But in our database two rows can never have the same value of id, as we have defined id as UNIQUE when we created our database.
We immediately modify our previous viewdb.php to viewdb2.php so that it can call view.php with the proper query string.
Viewing this page will show a list of names and corresponding nicknames. Look at the third column with a hyperlink view. Take your mouse over the hyperlink and look what it points to.
The link should be something like http://yourhost/view.php?id=3 and the links in each row will be different. Click on one of the links. It will bring up our previously coded view.php showing the detailed information of that person. How is this achieved?
Let’s take a look at our code viewdb2.php. Look at line 11, where all the real stuff takes place. The only unfamiliar thing here should be those odd dots (.) all around the line. The dot (.) is a concatenating operator in PHP, which means it concatenates the two strings on its two sides, which in turn means that if we write echo « Hello ». »World », the output will actually be « HelloWorld ».