Multimedia Services    DVD Authoring    DVD Duplication    CD Replication    CD Packaging    Website Solutions    Silk Screening Printing Services
TXP Media - DVD Authoring - DVD / CD Replication / Multimedia Services
Home Company Services Quote Builder Info Contact Us

Storage Utilities

The database class


The database class will be responsible for the most generic input and output from your preferred database. Note, the key about these utility libraries is they need to know nothing about what the rest of the framework does. They have specific purposes and are the most generic methods.

Lets start off with the simplest parameters that you will need in order to connect to your database: a host, a default database, a username, and a password. This can be most easily done by a few define statements on the top:
<?
    define
('HOST','localhost');
    
define('DATABASE','txpmedia');
    
define('USERNAME','root');
    
define('PASSWORD','test');
?>


Ok now that we know how to connect to our database, let's do the simplest connection and disconnection from the database. This can be best accomplished in the database class constructor and destructor:

<?
    
class db {
        private 
$prev_res;
    
        
// This will automatically get called when we instantiate
        // a DB object
        
__construct() {
            
mysql_connect(HOST,USERNAME,PASSWORD);
            
mysql_select_db(DATABASE);
        }
        
        
// This will be called when the DB object loses scope.
        
__destruct() {
            
mysql_close();
        }
    }
    
    
$_db = new db();
    
?>


Thanks to updated constructor and destructor functions you find in PHP 5, we can now automatically handle our connection to the database as soon as we instantiate our database object.

Now that we can connect to the database, the next thing we need to provide is the most basic form of communication with it. For that we will create a simple query function. The advantage of using our own query function rather than just calling mysql_query, for instance, is that we can trap errors and handle them in a graceful way. Let's take a look at a simple query function:

<?
    
function query$sql ) {
        
$res = @mysql_query($sql);
        if (
mysql_errno()) {
            print 
'mysql_error(): '.mysql_error().' on SQL Statement: '.$sql.'<BR>';
            return 
false;
        }
        
$this->prev_res $res;
        return 
$res;
    }
?>


With this our simple connection to the database is complete. Now there is two ways we will want to communicate to the database. To input data and to get data. Let's start off with getting data. Since the structure of query select statements is complex we will assume we attempt to communicate with the database directly by means of db::query(). With the ability to query the database in place, we simply need a way to iterate through the results of our queries.

A few utility functions are all we need. Most commonly you will either want to traverse row by row, or to have access to the entire resultset. So we have made next_row() and all_rows(). Here are their function definitions:

<?
    
function next_row$res null ) {
        if (
$res === null$res $this->prev_res;
        return 
mysql_fetch_assoc($res);
    }
    
    function 
all_rows$res null ) {
        if (
$res === null$res $this->prev_res;
        
$_ret = array();
        while (
$row=mysql_fetch_assoc($res)) $_ret[]=$row;
        return 
$_ret;
    }
?>


At this point we will be skipping how to insert data into the database. This is because ideally we want some method to take our basic object and transform it into a database compatable form.

Login:
Pass:
TXP Media  |  Company  |  Multimedia Services  |  Contact Us
Sitemap  |  Resources

Multimedia Tutorials and Technology Related Information Multimedia Tutorials and Technology Related Information



Copyright 2003-2017 TXP Media. All rights reserved.