Creating a PHP CMS – Part 2

This post is part of a series about creating a PHP CMS. Click here to start from the beginning.

In this post, we will be using PHP to create a connection to the MySQL database.

Defining Constants

First, we will be defining constants for the CMS. These hold values that will not change, such as database connection information. We will define the constants in the config.php file.

To define a constant, we will use the define function. This function accepts two arguments, inside of parentheses, separated by commas. The first one is the name of the constant, and the second one is the value of the constant. After a constant is defined, whenever the name of the constant is used (outside of a string), it will be replaced with its value.

For example, we can define this constant.

define('CONST_NAME', 'value');

This means that whenever 'CONST_NAME' is found, without the quotation marks, it will be replaced with 'value' (this time, with the quotation marks).

In config.php, define these constants. Don't forget to add <?php at the beginning of your PHP, and ?> to the end of your PHP code.

  • DB_NAME for the name of the database you created
  • DB_USER for your MySQL username
  • DB_PASS for your MySQL password
  • DB_HOST for your database hostname (usually localhost)

Finally, define a constant called BASE_URL. The value of this constant will be the base URL for the CMS, without the trailing slash. For example, on my server, the config.php file is located at http://localhost/cms/config.php. So the value of this constant is http://localhost/cms.

Now, your config.php file should look something like this. I've added in some comments to explain what the code is for.

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
// Simple CMS main constants configuration file
 
// Database configuration
define('DB_NAME', 'cms');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_HOST', 'localhost');
 
// Base URL
define('BASE_URL', 'http://localhost/cms');
 
?>

Connecting to the Database

Now that we have our database connection information, we can use it to connect to the database.

Whenever we do anything with MySQL, we need to connect to the database first. We will use the mysql_connect function to do this. Like define, mysql_connect also accepts arguments. However, we will be using three arguments this time. The first one is the database hostname, the second is the username, and the third one is the password.

mysql_connect('localhost', 'username', 'password');

Because we already defined constants with these values in config.php, we can use them here. We can bring the constant definitions from config.php into another file using require_once.

require_once 'config.php';

We could also use require, include and include_once. The difference between require and include is that with require, the script is stopped if it doesn't work, and with include the script continues. The ones ending with '_once' will not include the file again if it has already been included.

Now, we can use the constants to connect to the database like this.

mysql_connect(DB_HOST, DB_USER, DB_PASS);

If there's a problem connecting to the database, we will need to handle it. We will use die for this. This shows a message if there is an error.

mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Could not connect. ' . mysql_error());

This would connect to the database, but if there's an error (for example, if the database password is wrong), it will display the message 'Could not connect. ' and some details on what the error is.

After we've connected to the database, we need to select a database to work with, using mysql_select_db. We will only use one argument for this function: the name of the database.

mysql_select_db(DB_NAME);

Again, we will use die to handle any errors with selecting the database.

mysql_select_db(DB_NAME) or die('Could not select database. ' . mysql_error());

We can now execute MySQL queries after we've connected to the database. However, we will have to use this code in every file containing MySQL queries. To make things easier, we will create our own function.

In functions.php, enter this code.

function connect() {
}

function tells us that what's coming next is a new function. The name of this function is 'connect'. The parentheses are there because our functions can accept arguments, but they are empty here because we aren't using arguments for this function. The opening curly brace marks the beginning of the function and the closing curly brace marks the end of the function.

We will enter the code we just wrote to connect to the database between the curly braces.

Our function should look like this now.

function connect() {
    mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Could not connect. ' . mysql_error());
    mysql_select_db(DB_NAME) or die('Could not select database. ' . mysql_error());
}

Be sure to indent everything between curly braces. It's not necessary for it to work, but it helps make the code a lot more readable. If you aren't already, use a text editor with automatic indentation.

Whenever we need to use our function, we can just use

connect();

Now we just need to include config.php at the top of functions.php. Whenever I say to include a file, use require_once unless another way would be better.

Also, be sure to add comments to your code to explain what you're doing.

Now, your functions.php file should be something like this.

<?php
// Simple CMS main functions file
 
require_once 'config.php';
 
// Connect to MySQL database
function connect() {
    mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Could not connect. ' . mysql_error());
    mysql_select_db(DB_NAME) or die('Could not select database. ' . mysql_error());
}
 
?>

I had originally planned to include writing to the database in this part of the series, but this post has gotten a bit long, so I will talk about that in the next post.

If you have any questions, be sure to leave a comment.

Click here for Part 3

Stay Updated

Did you enjoy this post? Don't miss a single post by getting free updates!

16 Comments

  1. August 12, 2009

    Hi,

    Very nice tutorial.Your way of teaching excellet.
    I am waiting 3rd part. I have to learn php

  2. August 14, 2009

    Great post! Although I work at a web design company, I'm more on the design and customer care side of things. But I understood this post perfectly - thanks! :)

  3. August 18, 2009

    I recommended you replace require_once to require, because require is faster than require_once.
    And the definition of BASE_URL is perfect! Very good!

  4. August 18, 2009

    I also recommended you delete mysql_error() in the connection of mysql, because it is top secret to visitors.

  5. August 18, 2009

    Also recommended you add @ before mysql_connect for security reason.

  6. August 18, 2009

    Hua Chen: Thanks for the tips. Right now, when doing development, it's helpful to have error messages to find out if something may be wrong, but when actually using this, you can remove mysql_error(), and replace it with something less specific, like 'Database Error'.

  7. June 11, 2010

    It would be nice to know that we are working in the functions.php right from the beginning of Connecting to the Database part, and not at the end ;)

  8. Edward Kufoba
    October 14, 2010

    This is really a great tutorial for a starter like me.I've been struggling with books and videos, but so far so good your tutorial is one of the best I've ever come across.You're doing a good job.Thanks alot.

  9. January 2, 2011

    This is a very complicated subject. I am surprised to see someone tackling it.

  10. June 1, 2011

    Nice post i will no doubt use some of this in my next project. Thanks.

  11. shreekant
    July 26, 2011

    thanks for making a nice tutorial.i have started to make my own CMS after observing your tutorial..please suggest me the url making procedure in details

  12. September 4, 2011

    Great starting point tutorial, will pass this on to newbies

  13. September 30, 2011

    IS this method better than using a Class for settings?
    something like:

    class settings{
    var $settings;
    function get settings{
    SETTINGS['DB_NAME']='DATABASE NAME';
    and etc...
    }
    }

    Thanks again for the good job.

Trackbacks/Pingbacks