Bonjour,
j'ai une fonction que je souhaite transformer en classe (c'est la première)
Elle fonctionne parfaitement, le $pdo = new PDO($ConnexionString, $username,$password);.
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 function libatt($table,$pdo) { $sql = "select Att_Lib as lib from attribut where Att_Table = '$table' order by Att_ordre"; try { $result = $pdo->query($sql); $nblg=$result->rowCount(); } catch (PDOException $e) { $error = 'Erreur Recup liste attribut : '.$table . $e->getMessage(); include '../include/error.php.html'; exit(); } if ($nblg == 0) { $lib= ''; } else { foreach ($result as $row) { $lib[]=$row['lib']; } } return $lib; }
J'ai donc créer une classe (merci a ceux qui m'on aider avec les $this)
J'ai fait un test avec $npdo=newdb();.
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 class db { private $pdo; private $instance=null; private $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" ); private $host = 'localhost'; private $db_name = 't2i_erp'; private $user = 'root'; private $pass =''; private $dns = ''; public function __construct() { try { $this->dns = "mysql:host=$this->host;dbname=$this->db_name"; $this->instance = new PDO($this->dns, $this->user, $this->pass, $this->options); $this->instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { $error='Impossible de se connecter à la base de donnée '.$e->getMessage(). '. '. $dns.'. '. $this->user.'. '. $this->pass; include'error.php.html'; exit(); } } public function getinstance() { return $this->instance; } }
Maintenant pour transformer ma fonction en class
et cela plante sur la ligne $result = $this->db->query($sql);
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 class attribut { protected $db; var $lib =null; function __construct($db=null) { if ($db == null) { $this->db = new db(); $this->db->getinstance(); } else { $this->db = $db; } } public function libatt($table) { $sql = "select Att_Lib as lib from attribut where Att_Table = $table order by Att_ordre"; try { $result = $this->db->query($sql); $nblg=$result->rowCount(); } catch (PDOException $e) { $error = 'Erreur Recup liste attribut : '.$table . $e->getMessage(); include '../../include/error.php.html'; exit(); } if ($nblg == 0) { $this->lib= ''; } else { foreach ($result as $row) { $this->lib[]=$row['lib']; } } return $this->lib; } }avec le message
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 $attlib=new attribut(); $ltype = $attlib->libatt('contrat');comme si mon db n'était pas un object de type PDO.Call to undefined method db::query()
Merci pour votre aide.
Partager