J'ai recuperé une classe Mysql sur ce site :
Classe Mysql.php
Voici la classe "Compte" que j'ai créé :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 <?php // Classe Mysql.php class Mysql { var $host = 'localhost'; var $user = 'root'; var $pass = ''; var $base; var $connect_id = 0; var $cache_path = 'cache'; var $gc_time=86400; var $error; // PRIVATE : Constructeur initialise les parametre de la connexion function Mysql($Host='localhost', $User='root', $Pass='', $Base='') { $this->host=$Host; $this->user=$User; $this->pass=$Pass; $this->base=$Base; require_once('gc.php'); $gcsql = new GC(); srand((double)microtime()*1000000); if (!rand(0,99)) $gcsql->rungc($this->cache_path,$this->gc_time); } // PRIVATE : connection à la DB function connect($Host, $User, $Pass, $Base) { $this->connect_id = mysql_connect($Host, $User, $Pass); if ($this->connect_id) { if(mysql_select_db($Base, $this->connect_id)){ return $this->connect_id; } else return FALSE; } else return FALSE; } // PUBLIC : Envoi d'une requete a la DB function Send_Query($query) { if ( !$this->connect_id ) $this->connect($this->host,$this->user,$this->pass,$this->base); if ( $this->result_id = mysql_query($query, $this->connect_id) ) { $this->query = trim($query); $this->error = ''; return $this->result_id; } else { $this->error= mysql_error(); return FALSE; } } // PUBLIC : return the last auto increment insert ID, only use on MySQL function last_insert_id() { return @mysql_insert_id(); } // PUBLIC : renvoi le nombre d'enregistrement affecté function num_rows() { if ( isset($this->result_id) ) { if ( preg_match('`^select`i',$this->query) ) return mysql_num_rows($this->result_id); if ( preg_match('`^(insert|update|delete)`i',$this->query) ) return mysql_affected_rows($this->result_id); } else { return count($this->records); } } function get_object($query) { return @mysql_fetch_object($query); } function get_array($query, $mode='ASSOC') { switch($mode) { case 'NUMERIC' : return @mysql_fetch_array($query, MYSQL_NUM); break; case 'BOTH' : return @mysql_fetch_array($query, MYSQL_BOTH); break; case 'ASSOC' : break; default : return @mysql_fetch_assoc($query); } } // PUBLIC : renvoi le msg d'erreur mysql function return_error() { return @mysql_error(); } // PUBLIC : ferme la connection; function close() { return mysql_close($this->connect_id) ; } // PUBLIC : vide les resultat des requete function free_result() { return @mysql_free_result($this->connect_id); } // PUBLIC : execute une requete(SELECT) et renvoi le tableau des resultat via le cache function get_cached_data($request,$time=0) { $this->cachename=$this->cache_path.'/'.md5($request).'.cachesql'; if ( file_exists($this->cachename) && filemtime($this->cachename) > (time() - $time)) { $records = unserialize(file_get_contents($this->cachename)); } else { if (!($this->result=$this->Send_Query($request))) return FALSE; while ($record = mysql_fetch_array($this->result, MYSQL_ASSOC) ) { $records[] = $record; } $OUTPUT = serialize($records); $fp = fopen($this->cachename,"wb"); @flock($fp,2); fputs($fp, $OUTPUT); @flock($fp,3); fclose($fp); } return $records; } // PUBLIC : ajoute les slashe sql et C pour le stockage des BLOB function prepare_blob($file) { $blob = file_get_contents($file); $blob = addslashes($blob); $blob = addcslashes($blob, "\0"); return $blob; } } ?>
Classe Compte.php
Et voici mon fichier test.php pour tester ma classe Compte.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 <?php // Compte.php - Classe interface a la table compte require_once("Mysql.php"); //require_once("Config.php"); class Compte { //Propriétés var $C_Id; var $C_Libelle; var $C_DBConnection; var $C_Fields = "C_Id, C_Libelle"; //Constructeur function Compte($in_Numero, $in_DBConnection) { $this->C_DBConnection = $in_DBConnection; if (is_int($in_Numero)) { $this->C_Id = $in_Numero; } } //Getters and Setters function getLibelle() { return($this->C_Libelle); } function setLibelle($in_Numero) { $this->C_Libelle = htmlentities($in_Numero, ENT_QUOTES); } function getId() { return($this->C_Id); } function setId($in_Numero) { $this->C_Id = htmlentities($in_Numero, ENT_QUOTES); } // Methode: makeSelectCompteQueryById() // // Retourne la requete SQL pour charger le compte function makeSelectCompteQueryById() { return("SELECT " . $this->C_Fields . " FROM compte WHERE C_Id = ". $this->C_Id); } // Methode: makeInsertCompteQuery() // // Retourne la requete SQL pour ajouter un compte function makeInsertCompteQuery() { return("INSERT INTO compte ( " . $this->C_Fields . " ) VALUES ('" . $this->C_Id . "', '" . $this->C_Libelle . "' ) "); } // Methode: makeUpdateCompteQuery() // // Retourne la requete SQL pour ajouter un compte function makeUpdateCompteQuery() { return("UPDATE compte SET " . "C_Id = '" . $this->C_Id . "',". "C_Libelle = '" . $this->C_Libelle ); } // Methode: makeDeleteCompteQuery() // // Retourne la requete SQL pour ajouter un compte function makeDeleteCompteQuery() { return("DELETE FROM compte WHERE C_Id = " . $this->C_Id); } // Methode: Insert // // function insertCompte() { $query = $this->makeInsertCompteQuery(); $link = $this -> C_DBConnection; $result = $this-> C_DBConnection -> Send_Query($query); } // Methode: delete() // // Efface un compte function delete() { $this->C_DBConnection->Send_Query($this->makeDeleteCompteQuery()); } } ?>
J'obtiens toujours l'erreur
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 <?php // test // require_once("Compte.php"); $host = 'localhost'; $user = 'root'; $passwd = ''; $base = 'test_telephonie'; $Bdd = new Mysql($host, $user, $passwd, $base); if(!$link = $Bdd -> connect($host, $user, $passwd, $base)) die( $Bdd -> return_error() ); $addcompte = new Compte('3213', $link); $addcompte->setId(3213); $addcompte->setLibelle('ancien compte'); $addcompte->insertCompte(); //print("getid: ".$addcompte->getId()."<br>"); //print("getlibelle: ".$addcompte->getLibelle()."<br>"); ?>Le probleme vient de la classe Compte.php dans la fonction insertCompte()Fatal Error : Call to a member function on a non-object....
a la ligne "$result = $this-> C_DBConnection -> Send_Query($query);", je n'appelle pas correctement la fonction Send_Query !!!
Cela fait un moment que je n'ai pas touché aux classes, j'avoue que je suis un peu perdu, si quelqu'un avait une idée cela m'aiderai grandement
Tout suggestion est la bienvenue,
Merci
Partager