|
|
|
|
Bonjour,
je tente de créer une classe de connexion pour MySQL avec les PDO. Mais j'ai un problème lorsque j'appelle certaines fonctions. Voici ma classe :
class dbConn {
public $dsn;
public $dbh;
public $database;
public $host;
public $username;
public $password;
public function __constructor($host, $username, $password, $database) {
$this->host = $host;
$this->database = $database;
$this->username = $username;
$this->password = $password;
$this->dsn = 'mysql:host='.$host.'dbname='.$database;
self::connect();
}
public function connect() {
try {
$this->dbh = new PDO($dsn, $this->username, $this->password);
$this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, TRUE);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Erreur: ' . $e->getMessage();
}
}
public function getInstance() {
return ($this->dbh);
}
}
Ensuite, j'appelle cet objet dans une autre partie du code:
$connexion = new dbConn('localhost', '***********','**********','******');
$dbh = $connexion->getInstance();
$sql = "SELECT * FROM *** WHERE email = :username AND password = :password LIMIT 1";
$stmt = $dbh->prepare($sql);
$stmt->setFetchMode(PDO::FETCH_OBJ);
$stmt->bindParam(':username', $this->username);
$stmt->bindParam(':password', $this->password);
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
echo $row->first_name . ' ' . $row->last_name;
}
Et là j'obtiens ce message: Fatal error: Call to a member function prepare() on a non-object in
Je ne comprends pas, pourtant je fais appel au DBH de ma classe de connexion, et c'est comme si il ne reconnaissait pas l'objet.. Quelqu'un peut m'aider?
Merci
Configuration: Windows Vista Firefox 3.0.1
Voila, je croix que votre code est correcte mais il y'a quelques erreurs syntaxique:
|

