Chapter 13
Serving XML
Goals

Over the past few chapters we have learned why a DBMS is superior to a flat file. But so far our working system transmitted XML to the Flash client either from flat files or from hard-coded tables in the script.

In this chapter, we will use PHP to access a MySQL database, and dynamically generate the question that is then passed on to the Flash client. To do so, first we will have to learn how to connect to the database. Then we will use PHP to query the database. The information will be returned to us in a table, which our PHP script must translate to XML. Finally we send this XML to the Flash client.

At the end of the chapter, we will have this model in place: database to PHP to Flash .

Introduction

We will exclusively write PHP code in this chapter (although one of the PHP commands we will learn can take MySQL code as a parameter). PHP has several functions which allow a script to connect to a MySQL server, so we must have access to one before we can play with this code. In addition to connecting to the MySQL database via a series of functions, PHP also has a collection of functions which act upon the returned data. The results of queries are stored by PHP as variables, so once the query is actually collected, it is quick to go through. Starting with the code from Chapter 10 we will only have to replace the constructor of the Question class. Then, our PHP code will be serving questions out of a database.