Chapter 8: Connecting to Databases Within PHP

Chapter 8: Connecting to Databases Within PHP

Overview

After all this talk of databases, you might be eager to connect a database to your PHP programs. PHP is well known for its seamless integration of databases, especially MySQL. It's actually quite easy to connect to a MySQL database from within PHP. Once you've established the connection, you'll be able to send SQL commands to the database and receive the results as data you can use in your PHP program. By the end of this chapter, you'll build the adventure game featured at the beginning of Chapter 7, "Using MySQL to Create Databases." As you'll see, if the data is designed well, the programming isn't very hard. Specifically, you'll learn how to:

  • Get a connection to a MySQL database from within PHP.

  • Use a particular database.

  • Send a query to the database.

  • Parse the query results.

  • Check for data errors.

  • Build HTML output from data results.