991ac4d6164bd045f6d592ee4c25011c652f3df0
[mtweb] / mw / app / data / impl / mw_mysql.php
1 <?php
2
3   class mw_mysql{
4
5     var $link;
6
7     var $host;
8     var $base;
9     var $user;
10     var $password;
11
12     var $EXTENTION_OK;
13
14     function extention_ok(&$env) { return $this->EXTENTION_OK; }
15
16     function mw_mysql($host, $base, $user, $password){
17       $this->host = $host;
18       $this->base = $base;
19       $this->user = $user;
20       $this->password = $password;
21       $this->EXTENTION_OK = function_exists("mysql_connect");
22     }
23
24     function connect($host, $base, $user, $password){
25       $this->link = @mysql_connect($host, $user, $password);
26       if(!$this->link) throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
27       @mysql_query("SET NAMES 'utf8'");
28       if($base){
29         $connected = @mysql_select_db($base, $this->link);
30         if(!$connected) throw new Exception($this->exception_out("Impossible de selectioner la base ".$base));
31       }
32       return true;
33     }
34
35     function select_db($db_name){\r
36       $this->base = $db_name;\r
37       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
38       return $this->query("USE ".$db_name);\r
39     }
40
41     function table_exists($table){\r
42       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
43       $rst = $this->query("SHOW TABLES");\r
44       while($v_rst = mysql_fetch_array($rst)){\r
45         if(strcmp($v_rst[0], $table) == 0) return true;\r
46       }\r
47       mysql_free_result($rst);\r
48       return false;\r
49     }
50
51     function field_exists($table_name, $field_name){
52       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
53       $sql = "SHOW COLUMNS FROM `".$table_name."` LIKE ".$field_name;
54       $rst = $this->query($sql);
55       $exists = false;
56       $v_rst = $this->fetch_assoc($rst);
57       if($v_rst) $exists = true;
58       $this->free_result($rst);
59       return $exists;
60     }
61
62     function query($query_string){\r
63       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
64       $result = @mysql_query($query_string, $this->link);\r
65       if(!$result) throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));\r
66       return $result;\r
67     }
68
69     function fetch_assoc($rst){
70       if($this->link){
71         if($rst){
72           return mysql_fetch_assoc($rst);
73         }
74         else throw new Exception($this->exception_out("fetch_assoc sans result handler"));
75       }
76       else throw new Exception($this->exception_out("fetch_assoc sans connexion"));
77     }
78
79     function insert_id(){
80       if($this->link){
81         return mysql_insert_id($this->link);
82       }
83       else throw new Exception($this->exception_out("insert_id sans connexion"));
84     }
85
86     function free_result($rst){
87       if($this->link){
88         if($rst){
89           return mysql_free_result($rst);
90         }
91         else throw new Exception($this->exception_out("free_result sans result handler"));
92       }
93       else throw new Exception($this->exception_out("free_result sans connexion"));
94     }
95
96     function close(){
97       if($this->link) return mysql_close($this->link);
98       return true;
99     }
100
101     function exception_out($message){\r
102       return "[erreur] mysql : ".$message;\r
103     }
104
105   }
106
107 ?>