1. Home
  2. Computing & Technology
  3. PHP / MySQL

Create a Link from a Database

By , About.com Guide

Create a Link from a Database:

Often people do our database tutorials, and are able to fetch the information they need and echo it onto a page, but struggle with figuring out how to link their results. This is actually a very simple process. Basically you just echo the appropriate HTML and call the URL in the middle of it. Here are some examples. In this example we are fetching an array and assigning it to $info, and one of the fields holds email addresses.
while($info = mysql_fetch_array( $data ))
{
Print $info['name'] . "<br> ";
Print "<a href=mailto:".$info['email'] . ">" .$info['email'] . "</a><br>";
}
Notice how we called .$info['email'] twice, once to display the e-mail and once to be used in the link. The actual href linking code is placed around the information using print or echo, and separated with dots.

Here is another example using a web address and website name.

while($info = mysql_fetch_array( $data ))
{
Print "<a href=".$info['siteurl'] . ">" .$info['sitetitle'] . "</a><br>";
}
Again we first print the <a href= then add a dot to show we are switching gears and now printing something different, in our case the website URL we get from our database array. Finally we add another dot so we can finish our link code </a><br>.
Explore PHP / MySQL
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. PHP/MySQL Basics
  5. Create a Link from a Database - Make a Link Using Database Data

©2009 About.com, a part of The New York Times Company.

All rights reserved.