O'Reilly logo

Learning PHP, MySQL, JavaScript, and CSS, 2nd Edition by Robin Nixon

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

setup.php

With the pair of included files written, it’s now time to set up the MySQL tables they will use. This is done with Example 21-3, setup.php, which you should type in and load into your browser before calling up any other files—otherwise, you’ll get numerous MySQL errors.

The tables created are kept short and sweet, and have the following names and columns:

  • members: username user (indexed), password pass

  • messages: ID id (indexed), author auth (indexed), recipient recip, message type pm, message message

  • friends: username user (indexed), friend’s username friend

  • profiles: username user (indexed), “about me” text

Because the function createTable first checks whether a table already exists, this program can be safely called multiple times without generating any errors.

It is very likely that you will need to add many more columns to these tables if you choose to expand on this project. If so, you may need to issue a MySQL DROP TABLE command before recreating a table.

Example 21-3. setup.php
<html><head><title>Setting up database</title></head><body> <h3>Setting up...</h3> <?php // setup.php include_once 'functions.php'; createTable('members', 'user VARCHAR(16), pass VARCHAR(16), INDEX(user(6))'); createTable('messages', 'id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, auth VARCHAR(16), recip VARCHAR(16), pm CHAR(1), time INT UNSIGNED, message VARCHAR(4096), INDEX(auth(6)), INDEX(recip(6))'); createTable('friends', 'user VARCHAR(16), friend VARCHAR(16), INDEX(user(6)), INDEX(friend(6))'); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required