Specifications

grant select, insert, update, delete
on discussion.*
to discussion@localhost identified by ‘password’;
You can create this database structure by running this script through MySQL as follows:
mysql -u root -p < create_database.sql
You will need to supply your root password. You should probably also change the password we
have set up for the discussion user to something better.
To understand how this structure will hold articles and their relationship to each other, look at
Figure 29.3.
Building Practical PHP and MySQL Projects
P
ART V
718
LISTING 29.1 Continued
postid: 3
postid: 4 postid: 5
postid: 1
postid: 1 postid: 0
postid: 2 postid: 1
postid: 3 postid: 1
postid: 4 postid: 2
postid: 5 postid: 2
postid: 2
Database representation Tree representation
FIGURE 29.3
The database holds the tree structure in a flattened relational form.
As you can see, the parent field for each article in the database holds the postid of the article
above it in the tree. The parent article is the article that is being replied to.
You can also see that the root node, postid 1, has no parent. All new topics of discussion will
be in this position. For articles of this type, we store their parent as a 0 (zero) in the database.
Viewing the Tree of Articles
Next, we need a way of getting information out of the database and representing it back in the
tree structure. We will do this with the main page,
index.php. For the purposes of this explana-
tion, we have input some sample posts via the article posting scripts
new_post.php and
store_new_post.php. We will look at these in the next section.
We will cover the article list first because it is the backbone of the site. After this, everything
else will be easy.
35 7842 CH29 3/6/01 3:34 PM Page 718