functions.php

Let’s jump right into the project, starting with Example 21-1, functions.php, the include file of main functions. This file contains a little more than just the functions, though, because I have added the database login details here instead of using yet another separate file.

The first half-dozen lines of code define the host, database name, username, and password of the database to use. It doesn’t matter what you call the database, as long as it already exists (see Chapter 8 for how to create a new database). Also make sure to correctly assign a MySQL username and password to $dbuser and $dbpass. With correct values, the subsequent two lines will open a connection to MySQL and select the database. The last of the initial instructions sets the name of the social networking site by assigning the value “Robin’s Nest” to the variable $appname. If you want to change the name, here’s the place to do so.

The project uses five main functions:

All of these should be obvious in their action to you by now, with the possible exception of showProfile, which looks for an image of the name <user>.jpg (where <user> is the username of the current user) and, if found, displays it. It also displays any “about me” text the user may have saved.

I have ensured that error handling is in place for all the functions that need it, so that they can catch any typographical or other errors you may introduce and generate error messages. However, if you use any of this code on a production server, you will probably want to provide your own error-handling routines to make the code more user friendly.

Type in Example 21-1 and save it as functions.php (or download it from the companion website), and you’ll be ready to move on to the next section.