in PHP Get a better view

Passing variables for Get a better view

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 the following (the following line has been split for space reasons; you would enter it as one line):

http://yourhost/input.php?first=Rick&last=Denver&nickname=Mike&[email protected]  &salary=25000&submit=Enter+information

Look closely for Get a better view. 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 for Get a better view

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 for Get a better view.

<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 for  Get a better view

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 Get a better view. 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.

<HTML>  <?php  $db = mysql_connect("localhost", "root", "");  mysql_select_db("learndb",$db);  $result = mysql_query("SELECT * FROM personnel",$db);  echo "<TABLE BORDER=2>";  echo"<TR><TD><B>Full Name</B><TD><B>Nick Name</B><TD><B>Options</B></TR>";  while ($myrow = mysql_fetch_array($result))  {  echo "<TR><TD>".$myrow["firstname"]." ".$myrow["lastname"]."<TD>".$myrow["nick"];  echo "<TD><a href=\"view.php?id=".$myrow[id]."\">View</a>";  }  echo "</TABLE>";  ?>  </HTML>

Viewing this page will show a list of names and corresponding nicknames to Get a better view. 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. 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”. In our example we use the concatenate operator to generate a line like for Get a better view:

<TR><TD>Camilla Anderson<TD>Rose<TD><a  href="view.php?id=2">View</a>