4.3. Retrieving Information from a Database

The only purpose in storing information is to have it available when you need it. A database lives to answer questions. What products are for sale? Who are the customers? How many customers live in Indiana? What do the customers buy?

Many questions are answered by retrieving data from the database. For instance, to find out how many customers live in Indiana, you can retrieve all customer records where the field named state contains IN. Very often, you ask these kinds of questions in a PHP script and display the answer in a Web page. In a PHP script, you might retrieve all the records for Indiana customers and display a list of their names and addresses on a Web page.

To answer specific questions, you use the SELECT query. You can ask very precise, complex, and detailed questions with a SELECT query. Even if you're using phpMyAdmin to look at some specific information, you need to build and send an SQL query. phpMyAdmin provides some features that allow you to browse the data or ask some simple questions, as described in the previous section, but you need to use a SELECT query to ask most questions.

The simplest SELECT query is

SELECT * FROM tablename

This query retrieves all the information from the table. The asterisk (*) is a wildcard meaning all the columns.

The SELECT query can be much more selective. SQL words and phrases in the SELECT query can pinpoint the information needed to answer your question. Here are some tricks you can ...

Get PHP & MySQL® Web Development All-in-One Desk Reference for Dummies® 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.