Building a dynamic site – Part 1

Just about every large website is backed by some kind of database. In this series I’ll go over how to build your very own website that is powered by PHP and MySQL.

First you’ll need to make sure you have the following:
– Text Editor (notepad)
– PHP4, MySQL database
– phpMyAdmin

When building a dynamic site, there are a few key differences from a static website. In a static website, when someone requests a page the server sends the file back. With a dynamic site the server first grabs the information from a database, and then sticks it into a template, and then sends the file back.

The best part of going dynamic is being able to make changes to a single template, and instantly all the pages are updated. The downside is that databases use more resources. However this shouldn’t stop you from going dynamic!

The first step is creating the actual database table. For simplicity sake we are going to store the title and body into a table, and have MySQL auto-assign a Page ID to each page.

CREATE TABLE `pages` (
`PID` INT NOT NULL AUTO_INCREMENT ,
`Title` TEXT NOT NULL ,
`Body` TEXT NOT NULL ,
PRIMARY KEY ( `PID` ) 
);

Copy and paste the code above into phpMyAdmin’s SQL query text box. This will create the database table for our tutorial.

The next step will be creating a template for our dynamic site. Since we are going simple, we will go with a simple template too:

<HTML>
<HEAD>
<TITLE>##TITLE##</TITLE>
</HEAD>
<BODY>
##BODY##
</BODY>
</HTML>

Notice the tags we inserted, ##TITLE##, and ##BODY##. These are our place holders for the data inside the database.

In a real life dynamic site, we would have tables, menus, and such with the ##BODY## tag being in the center table cell where all the content goes. Feel free to make yours more impressive than mine.

Name the template file “template.txt”.

In “Creating a dynamic site – Part 2” we’ll start building the PHP page that will display the final result. Until then, start creating some pages using phpMyAdmin. To do so, select the table ‘pages’ from the left menu, and click ‘Insert’ on the top of the right page.