Bonjour a tous, afin de tester un peux j'ai voulu me faire une petite class pour stocker mes infos de connexion mysql (user, mdp, database)
Au début je stockais simplement dans 3 variables les données de connexion ce qui fonctionne parfaitement, exemple :
1 2 3
| global $connStr,$user,$pass;
$dbh = new PDO($connStr, $user, $pass);
la suite ..... |
mais pour me casser un peux la tête j'ai voulu remplacer les variables par une petite class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class ConnectDb{
protected $connStr;
protected $user;
protected $pass;
function __construct(){
$this->connStr = 'mysql:host=localhost;dbname=db_test';
$this->user = 'root';
$this->pass = 'root';
}
function getStringData(){
$data = $this->connStr.', '.$this->user.', '.$this->pass;
return $data;
}
} |
Je l'appele alors comme ceci :
$dbh = new PDO($this->sgbd->getStringData());
Seulement j'ai un big message d'erreur :
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000] [1044] Access denied for user ''@'localhost' to database 'db_test, root, root'' in /Applications/MAMP/htdocs/test/_scriptroot/backend/users/db/index.php:61 Stack trace: #0 /Applications/MAMP/htdocs/test/_scriptroot/backend/users/db/index.php(61): PDO->__construct('mysql:host=loca...')
Partager