Home arrow PHP arrow Building a Site Engine with PHP, Part 2

Building a Site Engine with PHP, Part 2

If you read the first article in this series, you’d know that we’re about to start talking about the plug-in and module system for our site engine. Along with the plug-ins and modules, we’re also going to be discussing some of the general functions that are needed to make site engine work correctly.

  1. Building a Site Engine with PHP, Part 2
  2. Best Site() Ever Made
  3. OK, Plug it in Now
  4. Module Madness
  5. Bring it Together
By: James Murray
Rating: starstarstarstarstar / 70
June 14, 2004

print this article



Don't Plug it in Yet

In the last article I discussed briefly how the plug-in and module systems worked. This article will get more in-depth on the subject, but, before we talk about the plug-ins (in fact, before they'll even work) we'll have to make a few other functions. I'd also like to take this time to let you know that for this project we're going to be using PHP5rc2 with MySQL.

Since all the information about the plug-ins and modules is in the database, we're going to want to make a class that will let us perform the most common SQL functions with ease. Also, since the site engine will run multiple sites, each of which can have their own plug-ins and modules, we'll want to make the functions that let the engine know what site is requesting the plug-ins and modules. Let's get started with our general functions. I hope you learn something new and cool that you'll like.

This is the SQL class; save the following as:


class sql{
    public $qcount;    //this keeps count of how many queries we make
    function sql(){    //we name the main function the same as the class so that it initializes when the class is called
        $this->qcount=0;    //set the default of $qcount to 0 because we don't yet have any queries
$db=@mysql_connect($host,$user,$pass,$dbname);    //create connection string ready to accept the parameters to connect with
        if($db){    // check to see if a connection is made
            return $db;    //return the connection ID if it's a valid connection
            die("<b>Error:</b> Database offline.<br/>".mysql_error() );    //if connection is invalid quit script and return error
    function _select_db($dbname){
        return mysql_select_db($dbname);    //this is the function to select a MySQL database (of course...)
    function _query($query){
        $this->qcount++;    //add 1 to the $qcount for each query, this will update $qcount everything the _query function is called
        return mysql_query($query);        //This is the function to execute a Query on a MySQL database

    function _fetch_row($id){
        return mysql_fetch_row($id);    //this fetches one row of data from the result of calling the _query function, it returns data as an array with numeric offsets
    function _fetch_array($id){
        return mysql_fetch_array($id);    //this function is just like the _fetch_row function, only this on returns both numeric offsets, and offsets named like the database field
    function _insert_id(){
        return mysql_insert_id();    //this function returns the ID generated for an AUTO_INCREMENT column by the previous INSERT query
    function _affected_rows($id){
        return mysql_affected_rows($id);    //this function returns the number of rows affected by the last INSERT, UPDATE or DELETE query
    function _num_rows($id){
        return mysql_num_rows($id);        //this function counts the number of rows returned form a previous SELECT query
    function _fetch_object($id){
        return mysql_fetch_object($id);        //This function is my favorite MySQL function, it's just like the _fetch_array function except an object is returned instead of an array
    function _mysql_error(){
        return mysql_error();    //This function will return and errors generated by MySQL

Now to complement our previous class file, we'll make another little class called config. This should be self explanatory; save the following as "your_site_dir/inc/config.inc.php"

class config {
    public $dbhost;
    public $dbuser;
    public $dbpass;
    public $dbname;
    function config(){

These may not directly tie in to each other at the moment, but they will in just a bit.

>>> More PHP Articles          >>> More By James Murray

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: