Web Development with PHP and Mysql

Development of the RemindMe Service

Previous | Next


Database Access

For all projects dealing with PHP and database access I make use of the data abstraction class from the PHPLIB library. PHP does not include a set of generic database functions. Each type of database has it's own uniquely named set of functions. Using the PHPLIB class and it's generic functions lets me switch databases in the future if needed without having to rewrite the code to use the database-specific functions in PHP.

For this project I will need three files from the PHPLIB library: prepend.php3, local.inc, and db_mysql.inc. The very first thing I'll do is rename these files to prepend.php, local.php, and db_mysql.php. There are two reasons for renaming these files.

  1. The most important is to change the file extensions from .inc to .php. On most websites the file extension .inc is not processed by the PHP parser. This means that anyone will be able to look at code in local.inc and db_mysql.inc simply by typing the names of these files into their web browser. The local.inc file will include your database name, username, and password, so this could potentially be disastrous. Want to see the difference? Try clicking on these links to see what is exposed each time: This is a commonly overlooked problem when using the PHPLIB library.
  2. The .php3 extension is left from the days of PHP3, when the PHPLIB library was originally written. I'm using PHP4 which defaults to a .php extension.

The next step is to modify local.php so that it contains a database access sub-class with my specific mysql access information:

class remindme_db extends DB_Sql {
  var $Host     = "localhost";
  var $Database = "remindme";
  var $User     = "remindme";
  var $Password = "my_password";
}

Then, I'll modify the prepend.php file to remove any extra include statements for files that I'm not using on this project:

// Modified heavily off prepend.php3 from phplib

require($DOCUMENT_ROOT . "/phplib/db_mysql.php");  /* Change this to match your database. */
require($DOCUMENT_ROOT . "/phplib/local.php"); 
A Quick Primer on PHPLIB for Database Access

Accessing a database through PHPLIB is very easy. Here is a quick example:

	$l_db = new remindme_db;

	$l_sql = "SELECT event_description FROM bday_reminders";
	$l_db->query($l_sql);
	while ($l_db->next_record())
		echo $l_db->f("event_description") . "<BR>\n";
This example will print out a list of the event_description value for each reminder in our database.
  1. $l_db = new stuff_db;
    This statement instantiates an instance of the remindme_db class that is defined in the local.php file, and assigns that instance to the $l_db variable.
  2. $l_sql = "SELECT event_description FROM bday_reminders";
    The SQL statement that we wish to execute is assigned to the $l_sql variable.
  3. $l_db->query($l_sql);
    We execute the query() method of the database class, passing in the SQL statement that we wish to execute.
  4. while ($l_db->next_record())
    While it is possible to fetch another record from the query results, loop. The next_record() method will return FALSE when we have reached the end of the matching records.
  5. echo $l_db->f("event_description") . "<BR>\n";
    The f() method returns the value of one column from the result set. In this case we are requesting the value of the event_description column, so we use $l_db->f("event_description") to retrieve the value.

Previous | Next
  1. Introduction
  2. Research
  3. Data Modelling
  4. Database Access
  5. Website Design
  6. User Management
  7. Account Creation / Logging In
  8. Page Layout
  9. Reminder Events
  10. Sending Reminders
  11. Conclusion