<?php
- class mw_mysql
- {
+ class mw_mysql{
+
var $link;
var $host;
function extention_ok(&$env) { return $this->EXTENTION_OK; }
- function mw_mysql($host, $base, $user, $password)
- { $this->host = $host;
+ function mw_mysql($host, $base, $user, $password){
+ $this->host = $host;
$this->base = $base;
$this->user = $user;
$this->password = $password;
$this->EXTENTION_OK = function_exists("mysql_connect");
}
- function connect($host, $base, $user, $password)
- { $this->link = @mysql_connect($host, $user, $password);
- if(!$this->link) return null;
+ function connect($host, $base, $user, $password){
+ $this->link = @mysql_connect($host, $user, $password);
+ if(!$this->link) throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
@mysql_query("SET NAMES 'utf8'");
- if($base)
- { $connected = @mysql_select_db($base, $this->link);
- if(!$connected) return null;
+ if($base){
+ $connected = @mysql_select_db($base, $this->link);
+ if(!$connected) throw new Exception($this->exception_out("Impossible de selectioner la base ".$base));
}
return true;
}
- function select_db($db_name)
- { $this->base = $db_name;
- if(!$this->link)
- { if(!$this->connect($this->host, $this->base, $this->user, $this->password)) return null;
- }
- return $this->query("USE ".$db_name);
+ function select_db($db_name){\r
+ $this->base = $db_name;\r
+ if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
+ return $this->query("USE ".$db_name);\r
}
- function table_exists($table_name)
- { $sql = "SHOW TABLES LIKE '".$table_name."'";
- $rst = $this->query($sql);
- if(isset($rst))
- { $exists = false;
- $v_rst = $this->fetch_assoc($rst);
- if($v_rst) $exists = true;
- $this->free_result($rst);
- return $exists;
- }
- return null;
+ function table_exists($base, $table){\r
+ if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
+ $this->select_db($base);\r
+ $rst = $this->query("SHOW TABLES");\r
+ while($v_rst = mysql_fetch_array($rst)){\r
+ if(strcmp($v_rst[0], $table) == 0) return true;\r
+ }\r
+ mysql_free_result($rst);\r
+ return false;\r
+ }
+
+ function query($query_string){\r
+ if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
+ $result = @mysql_query($query_string, $this->link);\r
+ if(!$result) throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));\r
+ return $result;\r
}
- function query($query_string)
- { if(!$this->link)
- { if(!$this->connect($this->host, $this->base, $this->user, $this->password)) return null;
+ function fetch_assoc($rst){
+ if($this->link){
+ if($rst){
+ return mysql_fetch_assoc($rst);
+ }
+ else throw new Exception($this->exception_out("fetch_assoc sans result handler"));
}
- $result = @mysql_query($query_string, $this->link);
- if(!$result) return null;
- return $result;
+ else throw new Exception($this->exception_out("fetch_assoc sans connexion"));
}
- function fetch_assoc($rst)
- { if($rst && $this->link) return mysql_fetch_assoc($rst);
- return null;
+ function insert_id(){
+ if($this->link){
+ return mysql_insert_id($this->link);
+ }
+ else throw new Exception($this->exception_out("insert_id sans connexion"));
}
- function insert_id()
- { if($this->link) return mysql_insert_id($this->link);
- return null;
+ function free_result($rst){
+ if($this->link){
+ if($rst){
+ return mysql_free_result($rst);
+ }
+ else throw new Exception($this->exception_out("free_result sans result handler"));
+ }
+ else throw new Exception($this->exception_out("free_result sans connexion"));
}
- function free_result($rst)
- { if($rst && $this->link) return mysql_free_result($rst);
- return null;
+ function close(){
+ if($this->link) return mysql_close($this->link);
+ return true;
}
- function close()
- { if($this->link) return mysql_close($this->link);
- return null;
+ function exception_out($message){\r
+ return "[erreur] mysql : ".$message;\r
}
}