1

I usee this class below to connect to my sql db. Everything is fine, but I think I'm not using it in the proper way. When a page is loaded on my website, some functions are called to display text, sponsors... and each function creates an mysql object. Is it wrong?? Can it create a too many connections issue? Should I create one object and pass it a a param for the functions?? or is there a way to make this object global?

class clsMysql
{
    var $con;
    var $last_id;

    function __construct($db = array()) 
    {
        $this->con = mysql_connect($db['host'], $db['user'], $db['pass'], true, MYSQL_CLIENT_INTERACTIVE) or die ('Error connecting to MySQL');
        mysql_select_db($db['db'], $this->con) or die('Database ' . $db['db'] . ' does not exist!');
    }

    function __destruct() {
        mysql_close($this->con);
    }

    // fonction qui exécute la requête SQL
    function fxQuery($sql) {
        return mysql_query($sql, $this->con);
    }

    // fonction qui retourne la variable du résultat de la requête SQL
    function fxGetVar($sql) {
        return $this->fxFetchArray(0, $sql);
    }

    // fonction qui retourne un tableau contenant la ligne unique du résultat de la requête SQL
    function fxGetRow($sql) {
        return $this->fxFetchArray(1, $sql);
    }

    // fonction qui retourne un tableau contenant le résultat de la requête SQL
    function fxGetResults($sql) {
        return $this->fxFetchArray(2, $sql);
    }

    // fonction qui met la requête dans un tableau
    function fxFetchArray($p, $sql)
    {
        $qry = $this->fxQuery($sql);
        $nb = mysql_num_rows($qry);
        $this->last_id = mysql_insert_id($this->con);

        if ($nb > 0)
        {
            switch ($p)
            {
                // fxGetVar
                case 0:
                    $rec = mysql_fetch_array($qry, MYSQL_NUM);
                    $mem_results = $rec[0];
                    break;

                // fxGetRow
                case 1:
                    $rec = mysql_fetch_array($qry, MYSQL_ASSOC);

                    foreach (array_keys($rec) as $field)
                        $mem_results[$field] = $rec[$field];
                    break;

                // fxGetResults
                case 2:
                    $ctr = 1;

                    while ($rec = mysql_fetch_array($qry, MYSQL_ASSOC))
                    {
                        foreach (array_keys($rec) as $field)
                            $mem_results[$ctr][$field] = $rec[$field];

                        $ctr++;
                    }
            }

            return $mem_results;
        }
        else
            return null;
    }

    //fonction pour échapper les caractères pour les variables post ou get
    //param: valeur
    //retourne la valeur échappée
    function fxEscape($values)
    {
        if (is_array($values))
            $values = array_map(array($this, 'fxEscape'), $values);
        else
        {
            // Stripslashes
            if (get_magic_quotes_gpc())
                $values = stripslashes($values);

            if ((int)ini_get('magic_quotes_sybase'))
                $values = str_replace("''", "'", $values);

            $values = mysql_real_escape_string($values, $this->con);
        }

        return $values;
    }

    function fxGetLastId() {
        return $this->last_id;
    }
}
1
  • add $GLOBALS['db'] = new clsMysql(); at the top of you main scripts and use $GLOBALS['db'] in all your functions. Commented Oct 2, 2012 at 5:27

2 Answers 2

2

You should be able to instantiate the object once and call the query method several times. It should prevent it from having a connection for each query.

$mysql = new clsMysql();

$mysql->fxQuery("select * from something");

$mysql->fxQuery("Another Query");

$mysql->fxQuery("Yet another query...");
Sign up to request clarification or add additional context in comments.

Comments

0

In an easy way:

// on the top of your code
$db = new clsMysql();

and then, in your class methods or functions:

// class
class abc {

    public function xyz () {
        global $db;            // make it global here

        // use it!
    }
}

In another way, more OOP:

// main class
class system {

    // variable to the store database object
    public $db;

    // constructor
    public function __constructor() {

        include('mysql.class.php');    // your mysql class file

        $this->db = new clsMysql();
    }

}

On the other classes, you can extend class system then.

// sample class
class sample extends system {

    // sample function
    public function sample_method() {

        $this->db->fxQuery('SELECT * FROM table');

}

And outside of the class:

$app = new system();
$result = $app->db->fxQuery('SELECT 1');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.