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.
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:
"your_site_dir/inc/sql.inc.php"
<?PHP 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 }else{ 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"
<?PHP class config {
public $dbhost; public $dbuser; public $dbpass; public $dbname;
function config(){ $this->dbhost="localhost"; $this->dbuser="root"; $this->dbpass=""; $this->dbname="engine"; } }
?>
These may not directly tie in to each other at the moment, but they will in just a bit.