host=MYSQL_HOST; else $this->host=$host; if ($user=='') $this->user=MYSQL_USER; else $this->user=$user; if ($password=='') $this->password=MYSQL_PASS; else $this->password=$password; if ($database=='') $this->database=MYSQL_DEFAULT_DB; else $this->database=$database; $this->con_id = mysql_pconnect($this->host, $this->user, $this->password); if (!($this->con_id === false)) { if (mysql_select_db($this->database, $this->con_id) === false) { echo date('Y/m/d - H:i:s') ." - ERREUR ".mysql_errno()." : Connection à la base de données impossible !".EOL; echo date ('Y/m/d - H:i:s'). mysql_error(); die(); } } } public function setCharSet($charSet) { return (mysql_query("SET CHARACTER SET $charSet;", $this->con_id)); } /** INSERTION d'un tableau dans une table. ** Les index doivent avoir les mêmes noms que les champs. ** @param string Table ** @param array Valeurs insérer ** @return int Dernière valeur de l'auto-incrément, 1 si pas d'auto-incrément et 0 si erreur **/ public function insert($table, $toAdd, $debug=false){ $fields = implode(array_keys($toAdd), '`,`'); foreach (array_values($toAdd) as $key=>$array_values) $tmp[$key]=checkaddslashes($array_values); $values = "'".implode(array_values($tmp), "','")."'"; # better $query = 'INSERT INTO `'.$table.'` (`'.$fields.'`) VALUES ('.$values.');'; if ($debug) $tdeb=microtime_float(); $res = mysql_query($query, $this->con_id); if ($res!==false) { if (mysql_insert_id()>0) $res=mysql_insert_id(); else $res=true; } if ($debug) $this->trace($query, $res, $tdeb); return $res; } public function update($table, $update, $where, $debug=false){ $fields = array_keys($update); $values = array_values($update); $i=0; $query='UPDATE `'.$table.'` SET '; while(isset($fields[$i])){ if($i>0) { $query.=', '; } $query.=' `'.$fields[$i]."`='".addslashes($values[$i])."'"; $i++; } $query.=' WHERE '.$where.' LIMIT 1;'; if ($debug) $tdeb=microtime_float(); $res=mysql_query($query, $this->con_id); if ($debug) $this->trace($query, $res, $tdeb); return true; } public function delete($table, $where, $debug=false) { $query='DELETE FROM `'.$table.'` WHERE '.$where.' LIMIT 1;'; if ($debug) $tdeb=microtime_float(); $res=mysql_query($query, $this->con_id); if ($debug) $this->trace($query, $res, $tdeb); return true; } public function select($table, $fields, $where, $debug=false, $assoc=MYSQL_BOTH, $huge=false) { if (mysql_select_db($this->database, $this->con_id) === false) { echo date('Y/m/d - H:i:s') ." - ERREUR ".mysql_errno()." : Connection à la base de données impossible !".EOL; echo date ('Y/m/d - H:i:s'). mysql_error(); die(); } $query="SELECT $fields FROM $table WHERE $where;"; if ($debug) $tdeb=microtime_float(); $this->result=mysql_query($query, $this->con_id);// or die(mysql_error()); if (mysql_errno()) die(mysql_errno() .' : '. mysql_error()); // echo $query; if (!$huge) { $tab=array(); while ($ligne = mysql_fetch_array($this->result, $assoc)) $tab[]=$ligne; if ($debug) $this->trace($query, sizeof($tab), $tdeb); return $tab; } else { $nbRows=mysql_num_rows($this->result); if ($debug) $this->trace($query, $nbRows, $tdeb); return $nbRows; } } public function fetch($assoc=MYSQL_BOTH) { return mysql_fetch_array($this->result, $assoc); } public function trace($query, $res='', $tdeb=-1) { if (!$fp=fopen('mysql_insert.log', 'a')) return false; $errnum=mysql_errno($this->con_id); if ($tdeb>-1) $duree=substr(''.microtime_float()-$tdeb, 0, 5); else $duree='N/D'; if (!fwrite($fp, date('Y/m/d - H:i:s') ." - $errnum - $res - $duree - $query\n")) return false; if (!fclose($fp)) return false; return true; } /** Exécute la requête passé en paramètre **/ public function query($query, $debug=false){ return mysql_query($query, $this->con_id); } /** Retourne le libellé de la dernière erreur **/ public function getLastErrorMsg() { return mysql_error($this->con_id); } /** Retourne le numéro de la dernière erreur **/ public function getLastErrorNum() { return mysql_errno($this->con_id); } /** Retourne le libellé et le numéro de la dernière erreur **/ public function getLastError() { return mysql_error($this->con_id).' ('.mysql_errno($this->con_id).')'; } } ?>