Subversion Repositories ALCASAR

Rev

Blame | Last modification | View Log

<?php
/*
Classe de gestion de base de donnée Mysql simple (suffisant pour nos besoin)
*/
/*
class sqlError  extends Exception {
    
    public function __construct($Msg) {
        parent :: __construct($Msg);
    }
    public function __toString() {
        $msg  = '<div><strong>' . $this->getMessage() . '</strong>';
        $msg .= ' at line ' . $this->getLine() . '</div>';
        return $msg;
    }
}
*/
class mysql
{

        // public properties
        // no public properties
        
        // private properties
        private $server         = '';
    private $database   = '';
    private $user               = '';
    private $pwd                = '';
    private $link               = '';   

        // protected properties
        // no protected properties
        
        // Class constructor
        public function __construct($server="127.0.0.1", $user="", $pwd="", $database="")
        {
                if (($user == "")||($database== "")) exit('Error : Mysql Database init'); // do Error

                // init
                $this->server = $server;
                $this->user = $user;
                $this->pwd = $pwd;
                $this->database = $database;
                // connect to database
                if ($this->connect() === false) exit('Error : Mysql connection');
                if ($this->select_database() === false) exit('Error : Mysql Database selection');
        }
        
        // Class destructor
        public function __destruct()
        {
                $this->close();
        }
        // public methods
        public function query($sql) // Exécute une requête SQL, retourne un jeu de résultats sous forme d'un Array()
        { //capturer les erreurs
                $ressource      = @mysql_query($sql,$this->link);
                if ($ressource === false) return false;
                
                $n = 0;
                $result = array();
                while ($line = mysql_fetch_assoc($ressource))
                {
                        foreach ($line as $key => $val) $result[$n][$key] = $val;
                        $n++;
                }
                mysql_free_result($ressource);

                return $result;
        }
        public function exec($sql) // Exécute une requête SQL et retourne le nombre de lignes affectées
        { //capturer les erreurs
                if (@mysql_query($sql,$this->link) === false) return false;
                return mysql_affected_rows($this->link);
        }
        public function lastInsertId() // Retourne l'identifiant de la dernière ligne insérée
        {
                return mysql_insert_id($this->link);
        }
        // private methods
        
        private function connect()
        { //capturer les erreurs
                $this->link = @mysql_connect($this->server,$this->user,$this->pwd);
      if ($this->link === false) {
            return false;
        } else {
            return true;
        }
    }
        
        private function select_database()
        { //capturer les erreurs
         if (false === @mysql_select_db($this->database,$this->link)) {
            return false;
         } else {
            return true;
         }
    }
        private function close() // ferme la connexion SQL
        {
                @mysql_close($this->link);
        }
        // protected methods

}
?>

Generated by GNU Enscript 1.6.6.