Vous n'êtes pas identifié(e).
L'icône rouge permet de télécharger chaque page du wiki visitée au format
PDF et la grise au format ODT →
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente Prochaine révision Les deux révisions suivantes | ||
utilisateurs:hypathie:tutos:php_connexion_mysql [08/07/2021 16:57] Hypathie [Objet PDO] |
utilisateurs:hypathie:tutos:php_connexion_mysql [08/07/2021 17:01] Hypathie [Objet PDO] |
||
---|---|---|---|
Ligne 107: | Ligne 107: | ||
class MysqlSingleton | class MysqlSingleton | ||
{ | { | ||
- | const SQL_USER = 'root'; | + | const SQL_USER = 'root'; |
const SQL_HOST = 'localhost'; | const SQL_HOST = 'localhost'; | ||
const SQL_PASS = 'arawak'; | const SQL_PASS = 'arawak'; | ||
const SQL_DTB = 'nath_test'; | const SQL_DTB = 'nath_test'; | ||
private static $connect = null; | private static $connect = null; | ||
- | private $message = null; | + | private $message = null; |
- | private function __construct() | + | private function __construct() |
+ | { | ||
+ | // A singleton should not be instanced ! | ||
+ | } | ||
+ | protected static function firstConnexion() | ||
+ | { | ||
+ | if(is_null(self::$connect )) | ||
{ | { | ||
- | // A singleton should not be instanced ! | + | try |
- | } | + | |
- | protected static function firstConnexion() | + | |
- | { | + | |
- | if(is_null(self::$connect )) | + | |
{ | { | ||
- | try | + | self::$connect = new PDO('mysql:dbname='.self::SQL_DTB.';host='.self::SQL_HOST,self::SQL_USER ,self::SQL_PASS); |
- | { | + | //self::$connect = new PDO('mysql:host=localhost;dbname=nath_test;charset=utf8','root', 'arawak'); |
- | self::$connect = new PDO('mysql:dbname='.self::SQL_DTB.';host='.self::SQL_HOST,self::SQL_USER ,self::SQL_PASS); | + | echo "CREATION CONNEXION PDO MYSQL"; |
- | //self::$connect = new PDO('mysql:host=localhost;dbname=nath_test;charset=utf8','root', 'arawak'); | + | } |
- | echo "CREATION CONNEXION PDO MYSQL"; | + | catch(PDOException $e) |
- | } | + | { |
- | catch(PDOException $e) | + | $this->message=$e->getMessage(); |
- | { | + | die(); |
- | $this->message=$e->getMessage(); | + | |
- | die(); | + | |
- | } | + | |
} | } | ||
- | return self::$connect; | ||
} | } | ||
+ | return self::$connect; | ||
+ | } | ||
- | protected function getStatus() | + | protected function getStatus() |
- | { | + | { |
- | return !is_null(self::$connect); | + | return !is_null(self::$connect); |
- | } | + | } |
- | + | ||
- | protected function getErreurMessage() | + | |
- | { | + | |
- | return $this->message; | + | |
- | } | + | |
- | protected function disconnectMysql() | + | protected function getErreurMessage() |
{ | { | ||
- | self::$connect = null; | + | return $this->message; |
} | } | ||
+ | |||
+ | protected function disconnectMysql() | ||
+ | { | ||
+ | self::$connect = null; | ||
+ | } | ||
} | } | ||
</code> | </code> | ||
Ligne 163: | Ligne 163: | ||
class GestionPDO extends MysqlSingleton | class GestionPDO extends MysqlSingleton | ||
{ | { | ||
- | private $PDO_connexion = null; | + | private $PDO_connexion = null; |
- | private $callingBy = null; | + | private $callingBy = null; |
+ | | ||
public function __construct($callingBy) | public function __construct($callingBy) | ||
{ | { | ||
Ligne 234: | Ligne 235: | ||
} | } | ||
- | public function getDataTreeCountryStateCity() | + | public function getDataTreeCountryStateCity() |
- | { | + | { |
- | $q = "SELECT name FROM tree_country_state_city where id=1;"; | + | $q = "SELECT name FROM tree_country_state_city where id=1;"; |
- | $result = $this->gestionPDO->securConnexion()->query($q)->fetch(); | + | $result = $this->gestionPDO->securConnexion()->query($q)->fetch(); |
- | //ou | + | //ou |
- | //$result = $this->gestionPDO->queryFetch($q); | + | //$result = $this->gestionPDO->queryFetch($q); |
- | return $result; | + | return $result; |
- | } | + | } |
} | } | ||
</code> | </code> |