PLEASE note: These pages are here solely for historic purposes. New articles have not been written since 2001; many links in the index are broken; and most ahref.com email addresses will now bounce. Try visiting ep Productions, Incorporated, the web programming and development company behind this site.

Tip: Want to know when we post new content? Subscribe to our newsletter.

web index ahref.com: a community space for web developers------ -----
IndexToolsCareersTalk
ahref.com > Guides > Technology

Technology Guide

Web Databases, Continued

So how does it work on the Web?

Let's look at what happens when a web user requests information from a database. Again, take the example of our Web Index. If someone wants to see what sites have been added recently, they can do that by clicking on "New Additions." The page they see is formatted just like a typical static HTML page. However, it's not a typical HTML page. It's the product of a web-connected database, some PHP programs, and an HTML shell.

So what happens behind the scenes when a visitor requests "New Additions?" Here's the rundown (don't worry too much if you don't understand the code listed here; just try to understand where the data is flowing):

1. The user sends "data" to the web server.

By clicking on the "New Additions" button, the visitor is actually sending data to our web server. The user's browser sends a request to our web server for the file /index/newsites.html when this happens.

2. The web server converts the data into a set of SQL queries.

All of the pages on our web site are parsed by PHP, a server-side scripting language. This means that whenever someone requests one of the HTML pages, our web server looks for any PHP code in the page and executes it. This is how we can get the type of functionality you generally expect from a CGI program (decision trees, database queries, etc.) in what appears to be a plain vanilla HTML page.

When a browser requests "/index/newsites.html", the web server runs the PHP code from that page:

     $query1 = "SELECT title, url, description FROM website
     WHERE TO_DAYS(NOW()) - TO_DAYS
     (website.create_date_stamp) 
     <= 7 ORDER BY sort_title";

This creates a query called $query1 that will be sent to the database server. It will find sites posted in the last seven days (WHERE TO_DAYS(NOW()) - TO_DAYS(website.create_date_stamp) <= 7) and sort them by title (ORDER BY sort_title).

3. The web server sends the queries to the database server.

The following PHP code, run by the web server, opens a connection to the database server and sends the query ($query1) to that server:

     $linker = mysql_connect ("db_server", "username", 
     "password");
     mysql_select_db ("db_name");
     $result1 = mysql_query ($query1);

The first two lines set up a connection between the web server and the database server. The web server connects to the machine db_server using the username and password entered in this line (hey, we can't tell you everything). The third line selects the database that the query will apply to. The fourth line tells the database server to execute the query and return the MySQL result identifier (the results) as $result1. The web server later uses the result identifier to retrieve the relevant information from the database. If there is an error, $result1 will be set to 0.

4. The database server performs the queries, selecting or modifying data in the database.

The database server selects all the fields that meet the criteria specified in the query.

5. The database server returns the results of the query.

The database server sends the result identifier (described above) to the web server.

6. The web server converts the query results into HTML.

The following PHP code, run by the web server, goes through the list of returned fields and assigns the appropriate HTML code for formatting.

7. The HTML is returned to the user.

The output is viewed in the user's web browser as a formatted HTML page listing "New Additions," sites added to the Index database in the last seven days.

You can view a diagram showing what this looks like.

continue reading >>>
or jump to a topic:

Introduction
What is a database, anyway?
How is a database structured?
So how does it work on the Web?
When wouldn't you use a database?


view a printable version of this article


To suggest a topic, please email guides@ahref.com.

 


HOME ||| ABOUT AHREF.COM ||| ADVERTISE ||| FEEDBACK ||| SEARCH THIS SITE ||| CONTRIBUTE

© 1998-1999 ep Productions, Inc. All rights reserved. Terms of use.