12 public function __construct($params = array()){
13 $this->host = isset($params["host"]) ? $params["host"] : "localhost";
14 $this->base = isset($params["base"]) ? $params["base"] : "mtweb";
15 $this->user = isset($params["user"]) ? $params["user"] : "";
16 $this->password = isset($params["password"]) ? $params["password"] : "";
17 $this->EXTENTION_OK = (extension_loaded("pdo") && extension_loaded("pdo_mysql"));
20 public function get_link(){
24 public function extention_ok(&$env){
25 return $this->EXTENTION_OK;
28 public function authentication_required(){
32 public function sgbd_name(){
36 public function connect($host, $base, $user, $password){
38 $this->link = new PDO("mysql:host=".$host.";dbname=".$base, $user, $password);
39 $this->link->query("SET NAMES 'utf8'");
41 catch(PDOException $e){
42 throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
47 public function select_db($db_name){
\r
48 $this->base = $db_name;
\r
49 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
50 return $this->query("USE ".$db_name);
\r
53 public function desc_table($table_name){
54 $sql = "SELECT * from information_schema.columns where table_name='".$table_name."'";
55 $rst = $this->query($sql);
57 "name" => $table_name,
58 "attributs" => array()
61 while($v_rst = $this->fetch_assoc($rst)){
\r
62 $desc["attributs"][$v_rst["COLUMN_NAME"]] = array(
63 "name" => $v_rst["COLUMN_NAME"],
64 "prymary_key" => $v_rst["COLUMN_KEY"] == "PRI" ? true : false,
65 "auto_increment" => $v_rst["EXTRA"] == "auto_increment" ? true : false
68 $this->free_result($rst);
71 throw new Exception($this->exception_out("Impossible de lire la description de la table"));
76 public function table_exists($table_name){
77 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
80 $rst = $this->query("SHOW TABLES");
81 while($v_rst = $rst->fetch()){
\r
82 if($v_rst[0] == $table_name){
87 $this->free_result($rst);
90 throw new Exception($this->exception_out("Impossible de savoir si la table existe"));
95 public function field_exists($table_name, $field_name){
96 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
97 if(!($desc = $this->desc_table($table_name))){
98 throw new Exception($this->exception_out("Impossible de lire la description de la table"));
101 foreach($desc["attributs"] as $attribut_name => $attribut){
102 if($field_name == $attribut_name){
110 public function query($query_string){
\r
111 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
112 if(!($result = $this->link->query($query_string))){
113 throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));
118 public function fetch_assoc($rst){
120 throw new Exception($this->exception_out("fetch_assoc sans connexion"));
123 throw new Exception($this->exception_out("fetch_assoc sans result handler"));
126 $tuple = $rst->fetch(PDO::FETCH_ASSOC);
129 throw new Exception($this->exception_out("erreur fetch_assoc"));
134 public function insert_id(){
136 throw new Exception($this->exception_out("insert_id sans connexion"));
139 $id = $this->link->lastInsertId();
142 throw new Exception($this->exception_out("erreur insert_id"));
147 public function free_result($rst){
149 throw new Exception($this->exception_out("free_result sans connexion"));
152 throw new Exception($this->exception_out("free_result sans result handler"));
159 throw new Exception($this->exception_out("erreur free_result"));
164 public function close(){
169 public function exception_out($message){
\r
170 return "[erreur] mysql : ".$message;
\r