Creating a PHP-Based Content Management System, Part 2
Peter Zeidman
8/5/2004
Go to page: 1 2
Printer Friendly Version
All of the information
to be displayed in our Content Management System will be stored in a database.
It is sensible, therefore, to create a reusable PHP class that we can call
upon whenever we need to access our data. The code listed here is for connecting
to a MySQL database. If you'll be using a different system, such as
PostgreSQL, MS SQL or SQLite, then change the code appropriately.
It's obviously quite a bit longer than our previous class, but it performs
a number of very important tasks. The code follows:
<?php
require_once 'SystemComponent.php';
class DbConnector extends SystemComponent
{
var $theQuery;
var $link;
function DbConnector(){
$settings = SystemComponent::getSettings();
$host = $settings['dbhost'];
$db = $settings['dbname'];
$user = $settings['dbusername'];
$pass = $settings['dbpassword'];
$this->link = mysql_connect($host,
$user, $pass);
mysql_select_db($db);
register_shutdown_function(array(&$this,
'close'));
}
function query($query) {
$this->theQuery = $query;
return mysql_query($query,
$this->link);
}
function fetchArray($result) {
return mysql_fetch_array($result);
}
function close() {
mysql_close($this->link);
}
}
?>
Some explanation
is required. After we've named the class 'DbConnector', we state
'extends SystemComponent'. This tells PHP to grab all of the data and functions
from
SystemComponent, and provide us with access to them (we'll need this in order
to get the $settings variable we created earlier).
The first function,
'DbConnector', has the same name as the class that contains it, meaning
it's run automatically when DbConnector loads. It firstly calls the 'getSettings'
function we wrote
earlier, and extracts from it the various database settings.
It
then
uses
these settings to
connect
to the
database. (Note that we have no code to deal with errors, this will be covered
in detail next time.)
The other functions
are explained below:
| Function |
Purpose |
| |
|
| query |
Execute
a database query |
| fetchArray |
Create an
array containing each record found using the 'query' function
(above) |
| close |
Closes the
database connection. The register_shutdown_function command in the DbConnector
function ensures this happens automatically
when the object is no longer in use. |
Save the above
code (also attached at the bottom of this article) to the 'includes' folder,
with the name DbConnector.php. This class will be widely used in the Intranet
system, so
let me give you an example
of how
we'd create an instance of DbConnector, extract
some data, and display it to the user. Let's imagine that our database stores
the details
of one
customer,
and
we want to get hold of his / her name and display it. Here's the code:
<?php
require_once('DbConnector.php');
$connector = new DbConnector();
$result = $connector->query('SELECT firstname
FROM customers');
$row = $connector->fetchArray($result);
echo $row['firstname'];
?>
If you'd
like to try out the DbConnector class now, you'll need to save the above
code in the includes folder in a php file, and set up a 'customers' table
in your database. I'll be covering the set up of our Intranet's database
next time.
The importance
and power of using a database is clear — we can store information in a formal
way, and rapidly access, manipulate and change it. The information we extract
or store is specified using the 'query' function of the DbConnector
class, and we create instances of DbConnector using the 'new' command, as
shown above. This also demonstrates the usefulness of
classes — if the settings are changed in SystemComponent, then all of the
classes that extend it will automatically be changed.
Next month
we'll be adding code to deal with errors, and creating the first part of
the administration system that will allow you to add or remove information
on the intranet. Until then!
Files (in compressed format): .zip files (for Windows) .tar files (for Linux)
Read Part 3 of this series at: http://www.intranetjournal.com/articles/200409/ij_09_07_04a.html.
Go to page: 1 2
Printer Friendly Version