Chapter 16. Databases

Modern software applications, particularly on the web, use state in order to function. State is a way to represent the current condition of the application for a specific request—who is logged in, what page they’re on, any preferences they’ve configured.

Typically, code is written to be more or less stateless. It will function the same way regardless of the state of the user’s session (which is what makes system behavior predictable within an application for multiple users). When a web application is deployed, it’s done so again in a stateless manner.

But state is vital for keeping track of user activity and evolving the way the application behaves for the user as they continue to interact with it. In order for an otherwise stateless piece of code to be aware of state, it must retrieve that state from somewhere.

Typically, this is done through the use of a database. Databases are efficient ways to store structured data. There are generally four kinds of databases you will work with in PHP: relational databases, key-value stores, graph databases, and document databases.

16.1 Relational Databases

A relational database breaks data down into objects and their relationships with one another. A particular entry—like a book—is represented as a row in a table, with columns containing data about books. These columns might include a title, ISBN, and subject. The key thing to remember about relational databases is that different data types reside in different tables. ...

Get PHP Cookbook 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.