O'Reilly logo

Learning PHP, MySQL, and JavaScript by Robin Nixon

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

rnfunctions.php

So let’s jump right into the project, starting with Example 20-1, rnfunctions.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. So 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 Functions

The project uses six main functions:

createTable

Checks whether a table already exists and, if not, creates it.

tableExists

Returns a value of 1 if a table already exists, otherwise 0.

queryMysql

Issues a query to MySQL, outputting an error message if it fails.

destroySession

Destroys a PHP session and clears its data to log users out.

sanitizeString

Removes potentially malicious code or tags from user input.

showProfile

Displays a user’s image and “about me” if they have one.

All of these should be obvious in their action to you by now, with the possible exception ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required