SQL Basics: An Address Book example

An introduction to using SQL in PHP using an address book example.

Address Book example

  • listcontacts.php - lists contacts (source)
  • newcontact.php - adds a new contact (source)

The database schema for the "addressbook" table:

+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| id         | int(11)     | NO   | PRI | NULL    | auto_increment | 
| first_name | varchar(25) | NO   |     |         |                | 
| last_name  | varchar(25) | NO   |     |         |                | 
| state      | char(2)     | NO   |     |         |                | 
| phone      | varchar(10) | NO   |     |         |                | 
+------------+-------------+------+-----+---------+----------------+