Selecting Data

Now that the database has some records in it, you can begin to retrieve the information with the most used of all SQL terms, SELECT. This type of query returns rows of records based upon certain criteria, using the syntax SELECT which columns FROM which table.

The simplest SELECT query is

SELECT * FROM tablename;

The asterisk means that you want to view every column. Your other choice would be to specify the columns to be returned, with each separated by a comma.

SELECT user_id, first_name, last_name FROM users;

There are a few benefits to being explicit about which columns are selected. The first is performance: There’s no reason to fetch columns you will not be using. The second is order: You can return columns in an order ...

Get PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide 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.