import mtweb.0.4.1
[mtweb] / web / 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) return null;
27       @mysql_query("SET NAMES 'utf8'");
28       if($base)
29       { $connected = @mysql_select_db($base, $this->link);
30         if(!$connected) return null;
31       }
32       return true;
33     }
34
35     function select_db($db_name)
36     { $this->base = $db_name;
37       if(!$this->link)
38       { if(!$this->connect($this->host, $this->base, $this->user, $this->password)) return null;
39       }
40       return $this->query("USE ".$db_name);
41     }
42
43     function table_exists($table_name)
44     { $sql =  "SHOW TABLES LIKE '".$table_name."'";
45       $rst = $this->query($sql);
46       if(isset($rst))
47       { $exists = false;
48         $v_rst = $this->fetch_assoc($rst);
49         if($v_rst) $exists = true;
50         $this->free_result($rst);
51         return $exists;
52       }
53       return null;
54     }
55
56     function query($query_string)
57     { if(!$this->link)
58       { if(!$this->connect($this->host, $this->base, $this->user, $this->password)) return null;
59       }
60       $result = @mysql_query($query_string, $this->link);
61       if(!$result) return null;
62       return $result;
63     }
64
65     function fetch_assoc($rst)
66     { if($rst && $this->link) return mysql_fetch_assoc($rst);
67       return null;
68     }
69
70     function insert_id()
71     { if($this->link) return mysql_insert_id($this->link);
72       return null;
73     }
74
75     function free_result($rst)
76     { if($rst && $this->link) return mysql_free_result($rst);
77       return null;
78     }
79
80     function close()
81     { if($this->link) return mysql_close($this->link);
82       return null;
83     }
84
85   }
86
87 ?>