View Single Post
Old 11-27-2007, 11:42 PM   #1 (permalink)
Gibou
The Contributor
 
Gibou's Avatar
 
Join Date: Nov 2007
Location: France, near Paris
Posts: 53
Thanks: 6
Gibou is on a distinguished road
Application Go A nice way to use a MySQL class

Hi !

Just to paste here the MySQL class I use most of time in my MVC API because its easy to use and automatic.

I suppose you have some knowledges in Model View Controler developement.

Imagine a model name "News" which is for the updates of a Website.
The following class is the MySQL Model class :

PHP Code:
class Mysql
{
    public function 
connect()
    {
        if(!@
mysql_connect(MYSQL_AUTH,MYSQL_LOGIN,MYSQL_PWD))
            return(
/*error class useless in this example*/);
        if(!@
mysql_select_db(MYSQL_BDD_NAME))
            return(
/*error class useless in this example*/);
    }

    public function 
close()
    {
        if(!
mysql_close())
            return(
/*error class useless in this example*/);
    }
... 
Until here, all is normal. Notice that I do not put the connexion in a constructor because it's better to open the connexion just before to send a query and to close it just after. That's why I don't want to allocate and destroy objects for all queries.

After, you'll agree that it won't be pleasant to call connect, call the right model, call the right method, call close.
So, in the MySQL class, we can have this kind of method :

PHP Code:
public function executeQuery($module,$argsC null,$argsM null)
{
    
/*
    * check that the module has well 2 parts
    * class.method
    */
    
$tab explode(".",$module);
    if(
count($tab) != 2)
        return(
/*still the same error class*/);
    else
    {
        if(
class_exists($tab[0]))
        {
            
$object = new $tab[0]($argsC);
            if(
$object instanceof Response)
                return(
$object);
            if(
method_exists($object,$tab[1]))
            {
                
$tmp self::connect();
                
// if the connection returns an error
                
if($tmp instanceof Response)
                    return(
$tmp);

                
$tmp $object->$tab[1]($argsM);
                
                
$tmp2 self::close();
                
// if the deconnection returns an error
                
if($tmp2 instanceof Response)
                {
                    unset(
$object);
                    return(
$tmp2);
                }
                unset(
$tmp2);
                unset(
$object);
                return(
$tmp);
            }
            else
                return(
/*bla bla error*/);
        }
        else
            return(
/*bla bla error*/);
    }

This method allows a call like that (in a Controller page) :

PHP Code:
/**
* @param : News.add -> call the "add" method of the "News" class
* @param : $argsArray -> hashtable useful for the News' constructor.
* @param : null -> no parameter is needed for the add method. All necessaries datas are passed in the constructor and called after.
*/
Mysql::executeQuery("News.add",$argsArray,null); 
__________________
Wedus project's Website

Last edited by Gibou : 11-28-2007 at 09:04 AM. Reason: some coments added
Send a message via MSN to Gibou
Gibou is offline  
Reply With Quote