Eléphant du PHP |
70 Messages
05 mai 2021, 11:38
Dans ces cas la pourquoi ne pas utiliser un Class pour ta connexion ?
class BDD {
PRIVATE PDO $dbh;
private string $host = "localhost";
private string $dbname = "";
private string $username = "root";
private string $password = "root";
public function __construct(){
try {
$this->dbh = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->username, $this->password);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
die('Connexion failed : '.$e->getMessage());
}
}
public function getConnection(){
return $this->dbh;
}
}
et en faisant un appel comme celui-ci :
$BDD = new BDD();
$dbh = $BDD->getConnection();
sans oublier de require ta Class évidemment
Sans compter la facilité à faire tes connexions, ça ajoutera une sécurité supplémentaire grâce au PRIVATE
" La révolution informatique fait gagner un temps fou aux Hommes, mais ils le passent avec leur ordinateur ! " by Khalil Assala