TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-27-2007, 11:42 PM   #1 (permalink)
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
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:16 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design