Chapter 9. Getting PHP to Talk to MySQL

Now that you’re comfortable using the MySQL client tools to manipulate data in the database, you can begin using PHP to display and modify data from the database. PHP has standard functions for working with the database.

First, we’re going to discuss PHP’s built-in database functions. We’ll also show you how to use the PEAR database functions that provide the ability to use the same functions to access any supported database. This type of flexibility comes from a process called abstraction. Abstraction is the information you need to log into a database that is placed into a standard format. This standard format allows you to interact with MySQL as well as other databases using the same format. Similarly, MySQL-specific functions are replaced with generic ones that know how to talk to many databases.

In this chapter, you’ll learn how to connect to a MySQL server from PHP, learn how to use PHP to access and retrieve stored data, and how to correctly display information to the user.

The Process

The basic steps of performing a query, whether using the mysql command-line tool or PHP, are the same:

  • Connect to the database.

  • Select the database to use.

  • Build a SELECT statement.

  • Perform the query.

  • Display the results.

We’ll walk through each of these steps for both plain PHP and PEAR functions.

Resources

When connecting to a MySQL database, you will use two new resources. The first is the link identifier that holds all of the information necessary to connect ...

Get Learning PHP and MySQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.