Introduction to database-driven documents
A database-driven document (also called a dynamic document) is a Web document in which some or all of the content does not exist in the document itself but is retrieved from a database when the document is requested by a Web browser.
How database-driven documents work
When a Web browser requests an ordinary HTML document, the Web server simply sends the document as it is, without modification. As a result, the copy of the document in the browser’s memory is exactly the same as the copy on the server’s file system. Database-driven documents, however, work differently. When a browser requests a database-driven document, what it receives from the server is not the document that exists on the server’s file system, but rather a generated document, based on the original, that the server creates “on the fly” when it receives the request. The original document acts as a kind of template, which the server “fills in” with content from a database.
A database-driven document contains instructions in server-side scriptsA program that is stored as plain text in a Web document and is executed by a host program on the Web server. that tell the server to retrieve specific information from a database. When the server receives a request from the browser for a database-driven document, the following things happen:
- The server opens the document and reads the instructions in it.
- The server retrieves the data specified in the script from the specified database.
- The server generates an HTML document from the retrieved data, using the original document as a template.
- The server sends the generated document to the browser.
Why use database-driven documents?
A database-driven approach is primarily useful for large sites that have many similar pages, because the Web developer can design a single document (the template) that becomes the basis for many generated documents. This approach separates content (the data) from presentation (the HTML formatting), allows content to be centralized, and makes it much easier to change the design of the site.
As an example, consider a typical online bookstore. It may have hundreds or even thousands of pages, one for each book the store carries, yet all are probably much the same. Each page displays a book title, an author name, a price, a brief description, and so forth, all formatted the same way from page to page. To author every page individually would be a huge task, and there would be a lot of duplication (and thus wasted disk space), since much of the HTML would be exactly repeated on every page. And if the Web developer decided to change the design of the site even a little, every page would have to be edited—another huge project.
But if the online bookstore is database-driven, these problems are avoided. Instead of a document for every book, a single document template along with a database serve as the basis for thousands of generated documents. The information remains in a central location, where it is easily managed, and changing the design of the generated pages requires editing just the template.
About query strings
In our hypothetical online bookstore, each link to a book description page uses the same document URL, but attaches a different query string to the end of the URL. The query string tells the Web server which book the user is interested in. For example, a link to the description page for The Grapes of Wrath might look like http://www.example.com/detail.asp?bookid=11424. “Detail.asp” is the file name of the dynamic document, “bookid” is a query parameter, and 11424 is the parameter’s value—the ID of the requested book. Instructions in the document itself tell the server in which database table to look for the information, while the query string identifies the particular record to retrieve.