3 class mw_mysql extends mw_sgbd{
5 public function name(){
9 public function default_params(){
11 "host" => "localhost",
18 public function validate_extention(){
19 return function_exists("mysql_connect");
22 public function authentication_required(){
26 public function connect($host, $base, $user, $password){
27 $this->link = @mysql_connect($host, $user, $password);
28 if(!$this->link) throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
29 @mysql_query("SET NAMES 'utf8'");
31 $connected = @mysql_select_db($base, $this->link);
32 if(!$connected) throw new Exception($this->exception_out("Impossible de selectioner la base ".$base));
37 public function select_db($db_name){
\r
38 $this->base = $db_name;
\r
39 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
40 return $this->query("USE ".$db_name);
\r
43 public function desc_table($table_name){
44 $table_name = $this->replace_prefixes($table_name);
45 $sql = "SELECT * from information_schema.columns where table_name='".$table_name."'";
46 $rst = $this->query($sql);
48 "name" => $table_name,
49 "attributs" => array()
52 while($v_rst = $this->fetch_assoc($rst)){
\r
53 $desc["attributs"][$v_rst["COLUMN_NAME"]] = array(
54 "name" => $v_rst["COLUMN_NAME"],
55 "prymary_key" => $v_rst["COLUMN_KEY"] == "PRI" ? true : false,
56 "auto_increment" => $v_rst["EXTRA"] == "auto_increment" ? true : false
59 $this->free_result($rst);
62 throw new Exception($this->exception_out("Impossible de lire la description de la table"));
67 public function table_exists($table_name){
68 $table_name = $this->replace_prefixes($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 public function field_exists($table_name, $field_name){
88 $table_name = $this->replace_prefixes($table_name);
89 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
90 if(!($desc = $this->desc_table($table_name))){
91 throw new Exception($this->exception_out("Impossible de lire la description de la table"));
94 foreach($desc["attributs"] as $attribut_name => $attribut){
95 if($field_name == $attribut_name){
103 public function query($query_string){
104 $query_string = $this->replace_prefixes($query_string);
\r
105 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
106 $result = @mysql_query($query_string, $this->link);
\r
107 if(!$result) throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));
\r
111 public function fetch_assoc($rst){
114 return mysql_fetch_assoc($rst);
116 else throw new Exception($this->exception_out("fetch_assoc sans result handler"));
118 else throw new Exception($this->exception_out("fetch_assoc sans connexion"));
121 public function insert_id(){
123 return mysql_insert_id($this->link);
125 else throw new Exception($this->exception_out("insert_id sans connexion"));
128 public function free_result($rst){
131 return mysql_free_result($rst);
133 else throw new Exception($this->exception_out("free_result sans result handler"));
135 else throw new Exception($this->exception_out("free_result sans connexion"));
138 public function close(){
139 if($this->link) return mysql_close($this->link);
143 public function exception_out($message){
\r
144 return "[erreur] mysql : ".$message;
\r