FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
January 01, 2002
One Wicked Site Admin Combo

WebReview.com: PHP and MySQL: One Wicked Site Admin Combo

April 28, 2000 Issue > Developers

PHP and MySQL: One Wicked Site Admin Combo

By Jason Gilmore

Rank: 3

PHP and MySQL Resources

Some helpful links for anyone interested in PHP and MySQL:

PHP- The Hypertext Preprocessor home page. Check out their PHP FAQs, links, downloads, and documentation.

MySQL- The place to go if you are looking for MySQL FAQ's, services, and consultants.

O'Reilly Networks- A MySQL source will soon be available from O'Reilly Networks. Check them out for articles, support, and community for open source and emergent technologies.

Effective adminstration of a Web site can ensure that site's success. Many developers are seeking technologies to achieve this success and make their jobs more streamlined. One way to do this is to use the powerful and popular combination of PHP and MySQL.

This tutorial will help you design and deploy a simple yet useful Web-based email administration application. Along with building a cool little app, we'll cover the following:

  1. User-defined functions, which help demonstrate the importance of building easily manageable code
  2. Basic templates, which save time and tears when scaling a Web site

Let's begin by outlining the functionality we want to have within the application:

  1. Add subscriber: We want the ability to add subscribers to the list.
  2. Delete subscriber: We need this should anyone desire to be removed from our list (the nerve!).
  3. Create newsletter: This creates the HTML newsletter that you'll send to your subscribers.
  4. Send newsletter: This sends the newsletter to all subscribers.

Before coding anything, let's turn our attention towards setting up the MySQL table that will hold subscriber information. This is easy, since there is really only one table to define (the one that holds the email addresses). I'll call it "newsletter_table."

mysql>create table newsletter_table (
 email char(65);
);

Easy enough! Now we're going to connect to the MySQL server and subsequently to the newsletter_table in order to perform queries. Instead of repeating these variables within each script, I like to put them in one central location, and use the INCLUDE command in order to use them as needed.

Why do I do this? The main reason is that it eliminates redundant code. Should you need to change a variable later, you would only need to make one modification. Less modification means less chance of an error. Also, if you want to add another variable—such as a second table—only one addition is needed.

Let's place these global variables into a file called init.inc, which you can check out in Listing 1. You can call this file whatever you want, but name it something that makes sense to you. After that, you'll need to make changes to the init.inc file so it connects to your MySQL server.

Okay! Now that the administrative part is out of the way, let's begin the fun stuff—coding.

The Importance of PHP Functions

User-defined functions provide several important services for the programmer:

  • Functions eliminate code redundancy.
  • Functions maximize code reusability.
  • Functions can be individually tested much more thoroughly than can a large, cumbersome piece of code.

We will develop user-defined functions to handle each of the services that our application will perform. I'm going to place all of these functions into a file titled "email_admin_funcs.inc". Check out the code in Listing 2. Here are some issues to be aware of.

  • The add function adds an email address to the MySQL database.
  • The delete function—you guessed it—deletes an email address from the database.
  • The send_newsletter sends the newsletter out to all subscribers. I wrote this function as an example—you'll want to extend and modify it to your specific needs.

Interface Intelligence

With the functions built, we now come to my personal favorite part of Web application development—interface design. I made this interface very generic, leaving room for you to add your own features or design as needed.

Figure 1 shows the simple interface I created. You can check out the basic code in Listing 3.

a screenshot of a simple interface made with  php.
A simple interface made with PHP.

What's especially important about the interface is that it will act as a template. We'll be calling upon this file to execute all of the functionality of the application. The function called upon depends on the value of the $func variable passed along in the response.

Final Thoughts

Web administrators will want to extend this simple example. Here are couple of suggestions on where to go from here:

  • Add integrity checking. Just a few simple lines of script can detect for duplicate or invalid email addresses within the database, and report an error.
  • Add user-automated subscriptions. Naturally, you don't want to be stuck having to administrate every part of the list. In fact, you probably would like to allow the subscriber to automatically subscribe and unsubscribe without having to contact you. Reuse of the add/delete functions will come in handy for this.
  • Offer an HTML-style newsletter. A simple parameter addition to PHP's mail()function, and you can send out HTML-formatted newsletters in no time. Check out the PHP documentation regarding the mail() function for more information.

PHP provides users with the capability to develop useful applications quickly and with ease. As you can see from this exercise, you can create powerful tools in just a few short steps. Use some forethought about function usage and the general layout of the application, and you're that much closer to making your tasks much easier, and your site visitors much happier.


Jason is a Web/wireless application developer for Unstrung. If you add any cool functions to the above script, let him know!


Related Articles
Secrets of SQL
What You Should Know About PHP

TOP 5 ARTICLES
No Top Articles.
DR. DOBB'S CAREER CENTER
Ready to take that job and shove it? open | close
Search jobs on Dr. Dobb's TechCareers
Function:

Keyword(s):

State:  
  • Post Your Resume
  • Employers Area
  • News & Features
  • Blogs & Forums
  • Career Resources

    Browse By:
    Location | Employer | City
  • Most Recent Posts:
    MEDIA CENTER  more
    NetSeminar
    Modernize your Development by Moving Build and Code Quality Upstream
    Moderated by Jon Erickson, Editor-in-Chief of Dr. Dobb's, this interactive panel discussion brings industry experts Anders Wallgren, CTO of Electric Cloud and Gwyn Fisher, CTO of Klocwork together for a candid discussion of the cost savings, productivity and quality benefits that can be achieved by stabilizing builds and code quality as early in the development cycle as possible.

    The reality of today's development environment - geographically distributed teams, the use of Agile development practices, increasing application complexity, etc. - is straining the viability of the traditional coding, build and release process. To stay ahead of the curve, development teams are modernizing their approach to dealing with these issues, and as a result are achieving new levels of development productivity. Register for the webcast.
    Date: Wednesday, July 15, 2009
    Time: 11 am PT/2 pm ET
    Modernize your Development by Moving Build and Code Quality Upstream
    Moderated by Jon Erickson, Editor-in-Chief of Dr. Dobb's, this interactive panel discussion brings industry experts Anders Wallgren, CTO of Electric Cloud and Gwyn Fisher, CTO of Klocwork together for a candid discussion of the cost savings, productivity and quality benefits that can be achieved by stabilizing builds and code quality as early in the development cycle as possible.

    The reality of today's development environment - geographically distributed teams, the use of Agile development practices, increasing application complexity, etc. - is straining the viability of the traditional coding, build and release process. To stay ahead of the curve, development teams are modernizing their approach to dealing with these issues, and as a result are achieving new levels of development productivity. Register for the webcast.
    Date: Wednesday, July 15, 2009
    Time: 11 am PT/2 pm ET
                                   
    INFO-LINK

    Resource Links: