logo Debian Debian Debian-France Debian-Facile Debian-fr.org Forum-Debian.fr Debian ? Communautés logo inclusivité

Debian-facile

Bienvenue sur Debian-Facile, site d'aide pour les nouveaux utilisateurs de Debian.

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 → ODT PDF Export

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
utilisateurs:hypathie:tutos:php_connexion_mysql [08/07/2021 16:50]
Hypathie [Objet PDO]
utilisateurs:hypathie:tutos:php_connexion_mysql [08/07/2021 17:34] (Version actuelle)
Hypathie [Singleton php]
Ligne 1: Ligne 1:
-====== PHP connexion mysql ======+====== PHP singleton de connexion mysql ======
  
   * Objet : code php de connexion   * Objet : code php de connexion
Ligne 87: Ligne 87:
 mysql -u root -p < ~/​user/​Test/​nath_test.sql mysql -u root -p < ~/​user/​Test/​nath_test.sql
 </​code>​ </​code>​
-===== Objet PDO =====+===== Singleton php =====
  
 <​code>​ <​code>​
Ligne 107: Ligne 107:
 class MysqlSingleton class MysqlSingleton
 { {
- const SQL_USER = 'root';+    ​const SQL_USER = 'toto';
     const SQL_HOST = '​localhost';​     const SQL_HOST = '​localhost';​
-    const SQL_PASS = 'arawak';+    const SQL_PASS = '********';
     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() +     ​self::​$connect ​=  new PDO('​mysql:​dbname='​.self::​SQL_DTB.';​host='​.self::​SQL_HOST,​self::​SQL_USER ,​self::​SQL_PASS);  
-  +     echo "​CREATION CONNEXION PDO MYSQL";​ 
- if(is_null(self::​$connect ))+
 + catch(PDOException $e)
  {  {
- try +      $this->​message=$e->​getMessage();​ 
-+      die();
- 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'​);​ +
- echo "​CREATION CONNEXION PDO MYSQL";​ +
-+
- catch(PDOException $e) +
-+
-   ​$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() +    ​protected function getErreurMessage()
-+
- return $this->​message;​ +
-+
-  +
-    protected function disconnectMysql()+
     {     {
-        self::$connect = null;+ return ​$this->​message;
     }     }
 +
 +   ​protected function disconnectMysql()
 +   {
 +       ​self::​$connect = null;
 +   }
 } }
 </​code>​ </​code>​
Ligne 157: Ligne 156:
  
 <code php> <code php>
 +<?php
 + 
 +require '​MysqlSingleton.php';​
 +
 +class GestionPDO extends MysqlSingleton
 +{
 +    private $PDO_connexion = null;
 +    private $callingBy = null;
 +    ​
 +    public function __construct($callingBy)
 +    {
 +       ​$this->​PDO_connexion = parent::​firstConnexion();​
 +    ​$this->​callingBy = $callingBy;
 +    echo " ​ <​br/>​Constructeur GestionPDO.php calling by " . $callingBy . ". ";
 +    }
 +
 +    /*public function queryFetch($query)
 +    {
 + return $this->​securConnexion()->​query($query)->​fetch();​
 +    }*/
 +    public function logSqlErreur()
 +    {
 +       // TODO
 +    }
 +
 +    public function securConnexion()
 +    {
 + if(is_null($this->​PDO_connexion))
 + $this->​getConnexion();​
 + return $this->​PDO_connexion;​
 +    }
 +    private function getConnexion()
 +    {
 + if(!$this->​getStatus())
 + $this->​PDO_connexion = parent::​firstConnexion();​
 + return $this->​PDO_connexion;​
 +    }
 +    public function getErreurMessage()
 +    {
 + return parent::​getErreurMessage();​
 +    }
 +    public function getStatus()
 +    {
 + return parent::​getStatus();​
 +    }
 +    public function getCallingBy()
 +    {
 + return $this->​createBy ;
 +    }
 +    public function disconnectMysql()
 +    {
 + parent::​disconnectMysql();​
 +    }
 + public function __destruct(){
 + echo " ​ <br/> Destructeur de gestionnairePDO ​ ";
 +    }
 +}
  
 </​code>​ </​code>​
  
-=== data_users.php ===+=== data_tree.php ===
  
 <code php> <code php>
 +<?php
  
 +require_once '​gestionnairePDO.php';​
 +
 +
 +class DAO_dataTree ​
 +{
 + const F_NAME = __FILE__;
 + private $gestionPDO;​
 +
 +    public function __construct()
 +    {
 + $this->​gestionPDO = new GestionPDO(self::​F_NAME);​
 + echo " ​ <​br/>​Constructeur DAO_dataTree.php ​ ";
 +    }
 +
 +    public function getCountryById($id)
 +    {
 + $q = "​SELECT name FROM tree_country_state_city where id=" . $id . ";";​
 + $result = $this->​gestionPDO->​securConnexion()->​query($q)->​fetch();​
 +
 + //ou 
 + //$result = $this->​gestionPDO->​queryFetch($q);​
 + return $result;
 +
 +    }
 +}
 </​code>​ </​code>​
  
-=== data_tree.php ===+ 
 +=== data_users.php ===
  
 <code php> <code php>
 +<?php
  
 +require_once '​gestionnairePDO.php';​
 +
 +
 +class DAO_dataUsers
 +{
 +
 + const F_NAME = __FILE__;
 + private $gestionPDO;​
 +
 +    public function __construct()
 +    {
 + $this->​gestionPDO = new GestionPDO(self::​F_NAME);​
 + echo " ​ <​br/>​Constructeur DAO_dataUsers.php ​ ";
 +    }
 +
 +    public function getNomfromId($id)
 +    {
 + $q = "​SELECT name FROM users where id=" . $id . ";";​
 + $result = $this->​gestionPDO->​securConnexion()->​query($q)->​fetch();​
 +
 + //ou 
 + //$result = $this->​gestionPDO->​queryFetch($q);​
 + return $result;
 +
 +    }
 +}
 </​code>​ </​code>​
  
Ligne 175: Ligne 285:
  
 <code php> <code php>
 +<?php
  
 + require '​data_tree.php';​
 + require '​data_users.php';​
 +
 + $dataTree = new DAO_dataTree('​index.php'​);​
 +
 + $country = $dataTree->​getCountryById(1);​
 + var_dump($country);​
 +
 + $dataUser = new DAO_dataUsers('​index.php'​);​
 +
 + $name = $dataUser->​getNomfromId(1);​
 + var_dump($name);​
 +
 +?>
 </​code>​ </​code>​
 +
 +=== Exécution du code ===
 +
 +<note tip>
 +Comme attendu, nous avons bien qu'une seule fois "​CREATION CONNEXION PDO MYSQL" !
 +</​note>​
 +
 +  * Dans le navigateur : **localhost/​testSingleton/​index.php**
 +
 +Voir le wiki prendre en main apache pour installer l'​alias ou le virtualhost "​testSingleton"​.
 +
 +<code retour>
 +CREATION CONNEXION PDO MYSQL
 +Constructeur GestionPDO.php calling by /​home/​hypathie/​www/​Test/​public/​data_tree.php.
 +Constructeur DAO_dataTree.php
 +
 +/​home/​hypathie/​www/​Test/​public/​index.php:​10:​
 +array (size=2)
 +  '​name'​ => string '​USA'​ (length=3)
 +  0 => string '​USA'​ (length=3)
 +
 +
 +Constructeur GestionPDO.php calling by /​home/​hypathie/​www/​Test/​public/​data_users.php.
 +Constructeur DAO_dataUsers.php
 +
 +/​home/​hypathie/​www/​Test/​public/​\index.php:​15:​
 +array (size=2)
 +  '​name'​ => string '​Jean-Marc'​ (length=9)
 +  0 => string '​Jean-Marc'​ (length=9)
 +
 +</​code>​
 +
 +
utilisateurs/hypathie/tutos/php_connexion_mysql.1625755812.txt.gz · Dernière modification: 08/07/2021 16:50 par Hypathie

Pied de page des forums

Propulsé par FluxBB