| Dumped! |
If you're familiar with SQL, you know that there are four basic types of operations possible with a database:
SELECT a record;
INSERT a record;
UPDATE a record;
DELETE a record.
In order to demonstrate these operations, we're going to build a little application that requires each of the functions listed above - an address book which allows multiple users to store and view contact information online.
As always, one of the first things you have to think about when designing a data-driven application is the design of the database (duh!). For this application, we've decided to use a single table called "abook", which contains fields for different types of contact information - address, phone, fax, email address, and the like. Every user in the system has a unique login id, and each record in the database is "owned" by a specific user.
We've put together a "dump file", which lets you create the database tables and initial set of records quickly - we suggest that you import this data into your mySQL database server, as we'll be using it throughout this article.
To import the data, download the dump file and use this command at your mySQL prompt:
mysql> mysql -u username -p database < dumpfile
Or you could insert the contents manually - here is what you'll need:
# Table structure for table 'abook'
DROP TABLE IF EXISTS abook;
CREATE TABLE abook (
id int(11) unsigned NOT NULL auto_increment,
uid varchar(255) NOT NULL,
fname varchar(255) NOT NULL,
lname varchar(255) NOT NULL,
PRIMARY KEY (id)
# Dumping data for table 'abook'
INSERT INTO abook (id, uid, fname, lname, tel, fax, email, addr, company, comment) VALUES ( '1', 'john', 'Bugs', 'Bunny', '7376222', '', 'email@example.com', 'The Rabbit Hole, Dark Woods, Somewhere On Planet Earth', '', 'Big-ears in da house!');
INSERT INTO abook (id, uid, fname, lname, tel, fax, email, addr, company, comment) VALUES ( '2', 'john', 'Elmer', 'Fudd', '', '7628739', 'firstname.lastname@example.org', '', '', '');
INSERT INTO abook (id, uid, fname, lname, tel, fax, email, addr, company, comment) VALUES ( '3', 'joe', 'Peter', 'Parker', '162627 x34', '', 'email@example.com', 'Your Friendly Neighbourhood Newspaper', '', 'My spidey-sense is tingling!');
INSERT INTO abook (id, uid, fname, lname, tel, fax, email, addr, company, comment) VALUES ( '4', 'bill', 'Clark', 'Kent', '1-800-SUPERMAN', '', 'firstname.lastname@example.org', '', '', 'Is it a bird? Is it a plane?');
This will create a table named "abook" with columns for different types of contact information; these records are owned by three mythical users, "bill", "john" and "joe".
Now check whether or not the data has been successfully imported with a SELECT query (the SELECT SQL statement is used to retrieve information from a database). Enter this at your mySQL command prompt:
mysql> select uid, fname, lname from abook;
which, in English, means "display the columns uid, fname and lname from the address book". Here's what you should see:
''.preg_replace(array('/ /', '/ /'), array(' ', ' '), '
| uid | fname | lname |
| john | Bugs | Bunny |
| john | Elmer | Fudd |
| joe | Peter | Parker |
| bill | Clark | Kent |
4 rows in set (0.00 sec)
How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.
Read more, or grab your copy now!