12 public function __construct($params = array()){
13 $this->host = isset($params["host"]) ? $params["host"] : "localhost";
14 $this->base = isset($params["base"]) ? $params["base"] : "mtweb";
15 $this->user = isset($params["user"]) ? $params["user"] : "";
16 $this->password = isset($params["password"]) ? $params["password"] : "";
17 $this->EXTENTION_OK = function_exists("mysql_connect");
20 public function extention_ok(&$env){
21 return $this->EXTENTION_OK;
24 public function authentication_required(){
28 public function sgbd_name(){
32 public function connect($host, $base, $user, $password){
33 $this->link = @mysql_connect($host, $user, $password);
34 if(!$this->link) throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
35 @mysql_query("SET NAMES 'utf8'");
37 $connected = @mysql_select_db($base, $this->link);
38 if(!$connected) throw new Exception($this->exception_out("Impossible de selectioner la base ".$base));
43 public function select_db($db_name){
\r
44 $this->base = $db_name;
\r
45 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
46 return $this->query("USE ".$db_name);
\r
49 public function desc_table($table_name){
50 $sql = "SELECT * from information_schema.columns where table_name='".$table_name."'";
51 $rst = $this->query($sql);
53 "name" => $table_name,
54 "attributs" => array()
57 while($v_rst = $this->fetch_assoc($rst)){
\r
58 $desc["attributs"][$v_rst["COLUMN_NAME"]] = array(
59 "name" => $v_rst["COLUMN_NAME"],
60 "prymary_key" => $v_rst["COLUMN_KEY"] == "PRI" ? true : false,
61 "auto_increment" => $v_rst["EXTRA"] == "auto_increment" ? true : false
64 $this->free_result($rst);
67 throw new Exception($this->exception_out("Impossible de lire la description de la table"));
72 public function table_exists($table_name){
\r
73 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
76 $rst = $this->query("SHOW TABLES");
77 while($v_rst = mysql_fetch_row($rst)){
\r
78 if($v_rst[0] == $table_name){
83 $this->free_result($rst);
86 throw new Exception($this->exception_out("Impossible de savoir si la table existe"));
91 public function field_exists($table_name, $field_name){
92 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
93 if(!($desc = $this->desc_table($table_name))){
94 throw new Exception($this->exception_out("Impossible de lire la description de la table"));
97 foreach($desc["attributs"] as $attribut_name => $attribut){
98 if($field_name == $attribut_name){
106 public function query($query_string){
\r
107 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
108 $result = @mysql_query($query_string, $this->link);
\r
109 if(!$result) throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));
\r
113 public function fetch_assoc($rst){
116 return mysql_fetch_assoc($rst);
118 else throw new Exception($this->exception_out("fetch_assoc sans result handler"));
120 else throw new Exception($this->exception_out("fetch_assoc sans connexion"));
123 public function insert_id(){
125 return mysql_insert_id($this->link);
127 else throw new Exception($this->exception_out("insert_id sans connexion"));
130 public function free_result($rst){
133 return mysql_free_result($rst);
135 else throw new Exception($this->exception_out("free_result sans result handler"));
137 else throw new Exception($this->exception_out("free_result sans connexion"));
140 public function close(){
141 if($this->link) return mysql_close($this->link);
145 public function exception_out($message){
\r
146 return "[erreur] mysql : ".$message;
\r