March 2, 2021
Hot Topics:

Building Database-Driven Applications with PHP and MySQL: Part II

  • By Elizabeth Fulghum
  • Send Email »
  • More Articles »

The Front End – Article Display

The final part of this application is the front end. As is the case with most applications, building this part is significantly less involved than the back end. Most of the programming involves grabbing the news items from the database. The rest involves designing how the information should be laid out. Take a look at the script:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<title>News Articles</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
td,body {
   font-family: Arial, Helvetica, sans-serif;
   font-size: 12px;

//include global file, establishes connection to database
include 'global.php';

$result = mysql_query("SELECT * FROM news ORDER BY date DESC");

//output each article
while($row=mysql_fetch_array($result)) {
   print '<h3>'.$row['title'].' - '.date("F j, Y", $row['date']).'</h3><p><b>Author:</b> '.$row['author'].'</p><p>'.nl2br($row['article']).'</p>';


In many respects, this script is identical to the administrative article listing. Here, too, we are retrieving all of the articles from the database and outputting them. This time though, all the columns in the table are retrieved. We also add an ORDER BY clause to the query so the latest article is listed first; MySQL will sort on the date column here.

Within the loop, there is some additional formatting of various fields. Because the date was stored as a Unix timestamp, it needs to be converted to something that's understandable. PHP's date() function is used to change it to "Day Month, Year" format. We also run nl2br() on the article body, so that any line breaks entered when the article was created are preserved as HTML <br> tags.


Whether it is a guestbook for a small site or a shopping cart with a 500 product inventory, every dynamic application is composed of the same basic parts. Admin pages, which are typically password protected, allow content to be added to a database, edited and deleted and a front end accessible to all users organizes, which assembles and displays the content in a meaningful way.

In this article you've learned how to use the basics of PHP/MySQL interaction to develop a complete application which incorporates all of these elements.

The next article will revisit the news application; you'll learn how to expand it using some of MySQL more advanced features.

Stay Tuned!

Page 4 of 4

This article was originally published on August 25, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date