Building a forum in PHP and Mysql

by Pablo Rigazzi - 22 April 2008

Php logoNote1: This article was first published in June of 2003 by “Maestros del Web“. The functions used in the code might need changes for newer versions of PHP and MySQL. We will be happy to receive any comments on this matter.

Note 2: Although the content of this post has been translated to English, the original PHP code is in Spanish.

In this tutorial we will create a forum from scratch by learning to separate code from design and modularising our application so that it is simple to setup and adapt

The Structure

First of all, we must prepare the table in the database that will contain all the discussions of our forum. What this table will have is messages information, their authors, and other data that will serve to identify what discussion the message belongs to. This is the proposed structure:

`id` int(7) NOT NULL auto_increment,
`autor` varchar(200) NOT NULL default ”,
`titulo` varchar(200) NOT NULL default ”,
`mensaje` text NOT NULL,
`fecha` datetime NOT NULL default ’0000-00-00 00:00:00′,
`respuestas` int(11) NOT NULL default ’0′,
`identificador` int(7) NOT NULL default ’0′,
`ult_respuesta` datetime default NULL,
KEY `id` (`id`)

Lets see in detail what fields our Forum’s table will have:

  • id:will be the main identifier of the table. It serves to differentiate each message.
  • autor: the message’s author
  • titulo: the title that the message will have. If this message is the initiator of a discussion, it will be the one that will show in the forum’s home.
  • mensaje: the message itself.
  • fecha: a field DATETIME that will indicate the date each message has been published.
  • respuestas:if this message is the initiator of a discussion, the number of answers that it receives will be accumulated here.
  • identificador:this field keeps the id value of the message that is being answered. If the message is initiator of a discussion, then this field will be in zero
  • ult_respuesta: if the message is a discussion initiator, the value will be updated with the date of the last answer that has received. It is useful to be able to maintain an order in the forums.

The above SQL command can be kept in a .sql file and run in your database from phpMyAdmin or, if you prefer it, it can be entered line by line in a text editor of mySQL (like mySQL monitor). Once we have created the table in your database, we can then advance to the following step. Before coding anything for the forum, we are going to create a small script that carries out a task to be repeated a lot, and that is to connect us to the database. This script will be included in each page where we have to access the database:

Sample of the page configuracion.php

$bd_host = “localhost”;
color=”#008000″>$bd_usuario = “user”;
$bd_password = “password”;
$bd_base = “nuestra_bd”;
$con = mysql_connect($bd_host, $bd_usuario, $bd_password);
mysql_select_db($bd_base, $con);

As we can see, there is not rocket science in this script, only variables containing the configuration of our database, and the minimum and necessary code to connect and to keep the resource from the connection in a variable $con that soon we will use when we make our queries.

Next page

4 Comments on “Building a forum in PHP and Mysql”

  1. Codespanish » Blog Archive » Why you shouldn’t code in Spanish Says:

    [...] Take for example this PHP code from the post “Building a forum in PHP and Mysql”. [...]

  2. Jaime recuerdo Says:

    Hola, gracias para estos consejos que me han servido para crear mi foro gratis en el sitio foroactivo. Ahí, tienen varias versiones de foros phpbb y sobre todo la posibilidad pasar de la versión phpbb2 a 3 o invision sin problema como un bueno webmaster. Además, esta nueva versión tiene muchas opciones gratis como mas de 3000 skin, la posibilidad de cambiar lo CSS y templates, crear perfiles personalizados, chatbox, sistema de reputación…y la ultima es cambiar los foros en blog y viceversa ! saludos

  3. Pablo Matamoros Says:

    No problems, as long as you link back or mention us.

  4. Abrir Correo Says:

    Es interesante lo que dices. Ya te agregué a mi lector de feeds RSS, adios

Leave a Reply