Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
600 stephane 1
<?php
2
/*
3
Classe de gestion de base de donnée Mysql simple (suffisant pour nos besoin)
4
*/
5
/*
6
class sqlError  extends Exception {
7
 
8
    public function __construct($Msg) {
9
        parent :: __construct($Msg);
10
    }
11
    public function __toString() {
12
        $msg  = '<div><strong>' . $this->getMessage() . '</strong>';
13
        $msg .= ' at line ' . $this->getLine() . '</div>';
14
        return $msg;
15
    }
16
}
17
*/
18
class mysql
19
{
20
 
21
	// public properties
22
	// no public properties
23
 
24
	// private properties
25
	private $server		= '';
26
    private $database	= '';
27
    private $user		= '';
28
    private $pwd		= '';
29
    private $link		= '';   
30
 
31
	// protected properties
32
	// no protected properties
33
 
34
	// Class constructor
35
	public function __construct($server="127.0.0.1", $user="", $pwd="", $database="")
36
	{
37
		if (($user == "")||($database== "")) exit('Error : Mysql Database init'); // do Error
38
 
39
		// init
40
		$this->server = $server;
41
		$this->user = $user;
42
		$this->pwd = $pwd;
43
		$this->database = $database;
44
		// connect to database
45
		if ($this->connect() === false) exit('Error : Mysql connection');
46
		if ($this->select_database() === false) exit('Error : Mysql Database selection');
47
	}
48
 
49
	// Class destructor
50
	public function __destruct()
51
	{
52
		$this->close();
53
	}
54
	// public methods
55
	public function query($sql) // Exécute une requête SQL, retourne un jeu de résultats sous forme d'un Array()
56
	{ //capturer les erreurs
57
		$ressource	= @mysql_query($sql,$this->link);
58
		if ($ressource === false) return false;
59
 
60
		$n = 0;
61
		$result = array();
62
		while ($line = mysql_fetch_assoc($ressource))
63
		{
64
			foreach ($line as $key => $val) $result[$n][$key] = $val;
65
			$n++;
66
		}
67
		mysql_free_result($ressource);
68
 
69
		return $result;
70
	}
71
	public function exec($sql) // Exécute une requête SQL et retourne le nombre de lignes affectées
72
	{ //capturer les erreurs
73
		if (@mysql_query($sql,$this->link) === false) return false;
74
		return mysql_affected_rows($this->link);
75
	}
76
	public function lastInsertId() // Retourne l'identifiant de la dernière ligne insérée
77
	{
78
		return mysql_insert_id($this->link);
79
	}
80
	// private methods
81
 
82
	private function connect()
83
	{ //capturer les erreurs
84
		$this->link = @mysql_connect($this->server,$this->user,$this->pwd);
85
      if ($this->link === false) {
86
            return false;
87
        } else {
88
            return true;
89
        }
90
    }
91
 
92
	private function select_database()
93
	{ //capturer les erreurs
94
         if (false === @mysql_select_db($this->database,$this->link)) {
95
            return false;
96
         } else {
97
            return true;
98
         }
99
    }
100
	private function close() // ferme la connexion SQL
101
	{
102
		@mysql_close($this->link);
103
	}
104
	// protected methods
105
 
106
}
107
?>