diff options
| author | polo <ordipolo@gmx.fr> | 2022-11-27 02:34:34 +0100 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2022-11-27 02:34:34 +0100 |
| commit | a2fdd37ffb5a3cc80b6fedb332024057553148f8 (patch) | |
| tree | cb28a5ddb1a2dd520ba1c2e41dfea942b15dc17e /php/ConnectionDatabase.php | |
| parent | efe371fd6e883dde99ca6d90a7aae99eb4aeadea (diff) | |
| download | AppliGestionPHP-a2fdd37ffb5a3cc80b6fedb332024057553148f8.tar.gz AppliGestionPHP-a2fdd37ffb5a3cc80b6fedb332024057553148f8.tar.bz2 AppliGestionPHP-a2fdd37ffb5a3cc80b6fedb332024057553148f8.zip | |
connexion BDD orientée objet
Diffstat (limited to 'php/ConnectionDatabase.php')
| -rw-r--r-- | php/ConnectionDatabase.php | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/php/ConnectionDatabase.php b/php/ConnectionDatabase.php new file mode 100644 index 0000000..b26d0bd --- /dev/null +++ b/php/ConnectionDatabase.php | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | <?php | ||
| 2 | // php/ConnectionDatabase.php | ||
| 3 | |||
| 4 | class Connection extends PDO | ||
| 5 | { | ||
| 6 | // paramètres du constructeur de PDO, avec sqlite seul le premier est nécessaire | ||
| 7 | public static $dsn = ''; // Data Source Name = 1er paramètre | ||
| 8 | public static $user = ''; | ||
| 9 | public static $password = ''; | ||
| 10 | public static $options = ''; | ||
| 11 | private static $Instance; | ||
| 12 | |||
| 13 | public function __construct() | ||
| 14 | { | ||
| 15 | try | ||
| 16 | { | ||
| 17 | parent::__construct(self::$dsn); // renseigne la variable $dsn de la classe PDO | ||
| 18 | $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // $this pour la méthode du parent PDO | ||
| 19 | } | ||
| 20 | catch(PDOException $e) | ||
| 21 | { | ||
| 22 | die('Erreur : '.$e->getMessage()); | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | // créer son objet avec: $bdd = Connection::getInstance(); | ||
| 27 | public static function getInstance(): self | ||
| 28 | { | ||
| 29 | if(self::$Instance === null) | ||
| 30 | { | ||
| 31 | self::$Instance = new self(); | ||
| 32 | } | ||
| 33 | return self::$Instance; | ||
| 34 | } | ||
| 35 | } | ||
