PHP Variables in URL and Link Text
For our first of three PHP examples, we'll use retreiving data from a MySQL table (which PHP is very good at doing, which is why it's used so often to do this). Let's say we fetch all of a MySQL table's rows and display them. (If you're concerned with security, check here for security tips, where you'll see why, theoretically, every single echo or print statement should use the htmlspecialchars() function for it's output. But this page you're on is about PHP variables in URLs and link text and mailto links, not security.) In this first example, we establish a MySQL connection, then select all (which is what * means) from the table and print it out into a nice neat HTML table. One row is called 'URL' and we didn't need it in our display, but we did need it to create a link. Our goal is to make the 'Firstname' row all be links to the person's website. In this theoretical example, this will have been the reason the 'URL' field was entered into the database in the first place. So what's the syntax that gets a click on any Firstname data resulting in surfing off to a website? We stick the 'URL' row into a PHP variable $e, then stick this variable into another variable, $m, created with concatenation with link tag syntax. The only thing missing is the anchor tag that ends the link: </a> That gets put at the end of the PHP echo statement that comes next. Note that in echo $m.$row['Firstname']."</a>"; the $m is the link tag which is concatenated with the $row['Firstname'] variable and the end anchor to form an active HTML link. We could throw a target='_blank' attribute in the link tag to have it open on a new page, if desired. Note also that in $m="<a href='".$e."'>"; it's critical that the double quotes are next to the dots, not the single quotes. The positioning of quotes in echo $m.$row['Firstname']."</a>"; are likewise vital.
<?php
// Make a MySQL Connection
mysql_connect("localhost", "your_user_name", "your_password") or die(mysql_error());
mysql_select_db("name_of_db") or die(mysql_error());
// Retrieve all the data from the table as a MySQL Resource
$result = mysql_query("SELECT * FROM $table") or die(mysql_error());
echo "<table border='1' width='578'>";
echo "<tr><th width='141'>Name</th><th width='25'>Status</th><th width='412'>Comment</th></tr>";
// below—keeps getting the next row until there are no more to get
while($row = mysql_fetch_array($result)) {
// then it prints out the contents of each row into a table
echo "<tr><td>";
$e=$row['URL'];
$m="<a href='".$e."'>";
echo $m.$row['Firstname']."</a>";
echo "</td><td>";
echo $row['Status'];
echo "</td><td>";
echo $row['Comment'];
echo "</td></tr>";
}
echo "</table>";
?>
For our second of three PHP examples, we'll use retreiving data from a MySQL table again. Let's say we fetch all of a MySQL table's rows and display them. In this situation, we have an 'Email' row so we are looking to send whoever clicks on the 'Firstname' person to that person's default email program so they can email them a message. This is a real, not theoretical example—we really are using this code. Note we added a subject to the email: PSB Mail. Anyway, this once more is a situation where the syntax is critical. Here we stick the 'Email' row in the PHP variable $e, and concatenate this into a mailto link. Placement of single and double quotes is vital. Then, like with the URL example, we concatenate together the parts of this mailto link so it becomes live and viable for emailing whoever 'Firstname' is.
<?php
// Make a MySQL Connection
mysql_connect("localhost", "your_user_name", "your_password") or die(mysql_error());
mysql_select_db("name_of_db") or die(mysql_error());
// Retrieve all the data from the table as a MySQL Resource
$result = mysql_query("SELECT * FROM $table") or die(mysql_error());
echo "<table border='1' width='578'>";
echo "<tr><th width='141'>Name</th><th width='25'>Status</th><th width='412'>Comment</th></tr>";
// below—keeps getting the next row until there are no more to get
while($row = mysql_fetch_array($result)) {
// then it prints out the contents of each row into a table
echo "<tr><td>";
$e=$row['Email'];
$m="<a href='mailto:".$e."?subject=PSB Mail'>";
echo $m.$row['Firstname']."</a>";
echo "</td><td>";
echo $row['Status'];
echo "</td><td>";
echo $row['Comment'];
echo "</td></tr>";
}
echo "</table>";
?>
For our third of three PHP examples, we'll use retreiving data from a MySQL table again. Let's say we fetch all of a MySQL table's rows and display them. But this time we don't want the 'Firstname' row to be a link, but just a column that gets displayed. In this situation, we have a displayed 'Email' row so we are looking to send whoever clicks on this 'Email' row to that person's default email program so they can email them a message. This is a theoretical example—we didn't need this exact code, but did need the second example, above. Note we added a subject to the email: PSB Mail. Anyway, this once more is a situation where the syntax is critical. Here we stick the 'Email' row in the PHP variable $e, and concatenate this into a mailto link. Placement of single and double quotes is vital. Then, like with the other examples, we concatenate together the parts of this mailto link so it becomes live and viable for emailing whoever got their email address in this 'Email' field.
<?php
// Make a MySQL Connection
mysql_connect("localhost", "your_user_name", "your_password") or die(mysql_error());
mysql_select_db("name_of_db") or die(mysql_error());
// Retrieve all the data from the table as a MySQL Resource
$result = mysql_query("SELECT * FROM $table") or die(mysql_error());
echo "<table border='1' width='578'>";
echo "<tr><th width='141'>Name</th><th width='25'>Status</th><th width='412'>Comment</th></tr>";
// below—keeps getting the next row until there are no more to get
while($row = mysql_fetch_array($result)) {
// then it prints out the contents of each row into a table
echo "<tr><td>";
echo $row['Firstname'];
echo "</td><td>";
$e=$row['Email'];
$m="<a href='mailto:".$e."?subject=PSB Mail'>";
echo $m.$row['Email']."</a>";
echo "</td><td>";
echo $row['Status'];
echo "</td><td>";
echo $row['Comment'];
echo "</td></tr>";
}
echo "</table>";
?>