14 function extention_ok(&$env) { return $this->EXTENTION_OK; }
16 function authentication_required() { return true; }
18 function sgbd_name() { return "MySql"; }
20 function mw_mysql($params = array()){
21 $this->host = isset($params["host"]) ? $params["host"] : "localhost";
22 $this->base = isset($params["base"]) ? $params["base"] : "mtweb";
23 $this->user = isset($params["user"]) ? $params["user"] : "";
24 $this->password = isset($params["password"]) ? $params["password"] : "";
25 $this->EXTENTION_OK = function_exists("mysql_connect");
28 function connect($host, $base, $user, $password){
29 $this->link = @mysql_connect($host, $user, $password);
30 if(!$this->link) throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
31 @mysql_query("SET NAMES 'utf8'");
33 $connected = @mysql_select_db($base, $this->link);
34 if(!$connected) throw new Exception($this->exception_out("Impossible de selectioner la base ".$base));
39 function select_db($db_name){
\r
40 $this->base = $db_name;
\r
41 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
42 return $this->query("USE ".$db_name);
\r
45 function desc_table($table_name){
46 $sql = "SELECT * from information_schema.columns where table_name='".$table_name."'";
47 $rst = $this->query($sql);
49 "name" => $table_name,
50 "attributs" => array()
53 while($v_rst = $this->fetch_assoc($rst)){
\r
54 $desc["attributs"][$v_rst["COLUMN_NAME"]] = array(
55 "name" => $v_rst["COLUMN_NAME"],
56 "prymary_key" => $v_rst["COLUMN_KEY"] == "PRI" ? true : false,
57 "auto_increment" => $v_rst["EXTRA"] == "auto_increment" ? true : false
60 $this->free_result($rst);
63 throw new Exception($this->exception_out("Impossible de lire la description de la table"));
68 function table_exists($table_name){
\r
69 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
72 $rst = $this->query("SHOW TABLES");
73 while($v_rst = mysql_fetch_row($rst)){
\r
74 if($v_rst[0] == $table_name){
79 $this->free_result($rst);
82 throw new Exception($this->exception_out("Impossible de savoir si la table existe"));
87 function field_exists($table_name, $field_name){
88 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
89 if(!($desc = $this->desc_table($table_name))){
90 throw new Exception($this->exception_out("Impossible de lire la description de la table"));
93 foreach($desc["attributs"] as $attribut_name => $attribut){
94 if($field_name == $attribut_name){
102 function query($query_string){
\r
103 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
104 $result = @mysql_query($query_string, $this->link);
\r
105 if(!$result) throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));
\r
109 function fetch_assoc($rst){
112 return mysql_fetch_assoc($rst);
114 else throw new Exception($this->exception_out("fetch_assoc sans result handler"));
116 else throw new Exception($this->exception_out("fetch_assoc sans connexion"));
119 function insert_id(){
121 return mysql_insert_id($this->link);
123 else throw new Exception($this->exception_out("insert_id sans connexion"));
126 function free_result($rst){
129 return mysql_free_result($rst);
131 else throw new Exception($this->exception_out("free_result sans result handler"));
133 else throw new Exception($this->exception_out("free_result sans connexion"));
137 if($this->link) return mysql_close($this->link);
141 function exception_out($message){
\r
142 return "[erreur] mysql : ".$message;
\r