Location: PHPKode > scripts > SPL and Iterators > spl-and-iterators/users/mysql.cls.php
<?php
class mysql extends dbfactory {

    protected $default_cfg = array(
             'host'   => 'localhost',
             'user'   => 'root',
             'passwd' => '',
             'name'   => 'test');

    // connection à la base
    protected function connect () {
        $this->_config['link'] = @mysql_connect($this->_config['host'],
                                              $this->_config['user'],
                                              $this->_config['passwd']);
        if (!$this->_config['link'] ) {
            throw new Exception('Erreur lors de la connection vers : '.$this->_config['host'].'.');
        }

        $this->_config['base'] = @mysql_select_db($this->_config['name'], $this->_config['link']);
        if (!$this->_config['base'] ) {
            throw new Exception('Erreur lors de l\'ouverture de la base de donnée : '.$this->_config['name'].'.');
            unset($this->_config);
        }
    }

    // Fermeture de la base de données au moment de la destruction de la classe.
    public function __destruct() {
        mysql_close($this->_config['link']);
    }

    // création d'une requête et historisation
    public function query ($sql, $desc=NULL) {
        $start = microtime (true);
        $this->query = @mysql_query ($sql, $this->_config['link'] );
        $query_time = microtime (true) - $start;
        if ($this->query) {
            $this->query_id++;
            $this->history[$this->query_id] = array('desc' => $desc,
                                                    'query' => $sql,
                                                    'time' => $query_time);
            return $this->query;
    } else {
        throw new Exception (mysql_error() );
        return false;
        }

    }

    // récupère les résultats dans un tableau associatif
    public function fetch_assoc ($query=NULL) {
        if (isset($query)) {
            $this->query = $query;
        }
        return mysql_fetch_assoc ($this->query);
    }

    public function insert_id () {
    	return mysql_insert_id ();
    }

}
?>
Return current item: SPL and Iterators