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
Prochaine révision Les deux révisions suivantes
utilisateurs:hypathie:tutos:php_connexion_mysql [08/07/2021 16:16]
Hypathie [PHP connexion mysql]
utilisateurs:hypathie:tutos:php_connexion_mysql [08/07/2021 16:50]
Hypathie [Objet PDO]
Ligne 6: Ligne 6:
   * Débutant, à savoir : [[:​doc:​systeme:​commandes:​le_debianiste_qui_papillonne|Utiliser GNU/Linux en ligne de commande, tout commence là !.]] :-)   * Débutant, à savoir : [[:​doc:​systeme:​commandes:​le_debianiste_qui_papillonne|Utiliser GNU/Linux en ligne de commande, tout commence là !.]] :-)
  
-===== Objet PDO =====+===== BDD mysql/​mariadb ​=====
  
-=== créer ​une instance de l'​objet ​===+==== Créer ​une base ====
  
-<code php> +=== Script sql=== 
-// file name : Connexion.class.php +<code sql> 
-<?php +//nath_test.sql 
-class Database +CREATE DATABASE IF NOT EXISTS `nath_test`;
-+
-    private static $cont  ​null; +
-      +
-    public function __construct() { +
-        die('​Init function is not allowed'​);​ +
-    } +
-      +
-    public static function connect() +
-    { +
-      +
-       if ( null == self::$cont ) +
-       ​{ ​     +
-        try +
-        { +
-            self::​$cont ​ new PDO('​mysql:​host=localhost;​dbname=comgocom;​charset=utf8','​root',​ '​arthuretmax2020'​);  ​ +
-                     ​ +
-           ​// self::$cont =  new PDO('​mysql:​host=localhost;​dbname=essaiLogin;​charset=utf8','​essaiLogin',​ '​essaiLogin'​); ​ +
-        } +
-        catch(PDOException $e) +
-        { +
-          die($e->​getMessage()) +
-        } +
-       } +
-       ​return self::​$cont;​ +
-    } +
-      +
-    public static function disconnect() +
-    { +
-        self::$cont = null; +
-    } +
-+
-</​code>​+
  
-=== utiliser l'​objet ===+USE `nath_test`;​ 
 +  
 +CREATE TABLE IF NOT EXISTS `users` ( 
 +  `id`          int(11) NOT NULL, 
 +  `name` ​       varchar(250) NOT NULL, 
 +  `surname` varchar(250) NOT NULL 
 +) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=UTF8;
  
-<code php> +INSERT INTO `users` (`id`, `name`, `surname`) VALUES  
-// file name : RequetesIdentification.class.php+(1, '​Jean-Marc',​ '​Joseph'​),​ 
 +(2, '​Nathalie',​ 'D urso'​),​ 
 +(3, '​Lionel',​ '​Joseph'​),​ 
 +(4, '​Samuel',​ 'D urso'​),​ 
 +(5, '​Helene-Fleur',​ 'D urso'​);​
  
-<?php +ALTER TABLE `users` 
-error_reporting(E_ALL); +  ADD PRIMARY KEY (`id`); 
-ini_set('​display_errors',​ '​1'​);+   
 +ALTER TABLE `users` 
 +  MODIFY `id` int(11NOT NULL AUTO_INCREMENT,​AUTO_INCREMENT=28;
  
-require '​Connexion.class.php'​;+CREATE TABLE IF NOT EXISTS `tree_country_state_city` ( 
 +  `id` int(11) NOT NULL, 
 +  `name` varchar(250) NOT NULL, 
 +  `parent_id` int(11) NOT NULL 
 +) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=UTF8;
  
-class RequetesIdentification 
-{ 
  
-    public function __construct() +INSERT INTO `tree_country_state_city` ​(`id`, `name`, `parent_id`VALUES 
-    { +(1, '​USA',​ 0), 
-       $this->​pdo = Database :: connect(); +(2, '​Canada',​ 0), 
-    }+(3, '​Australia',​ 0), 
 +(4, 'New York', 1), 
 +(5, '​Alabama',​ 1), 
 +(6, '​California',​ 1), 
 +(7, '​Ontario',​ 2), 
 +(8, '​British Columbia',​ 2), 
 +(9, 'New South Wales',​ 3), 
 +(10, '​Queensland',​ 3), 
 +(11, 'New York city', 4), 
 +(12, '​Buffalo',​ 4), 
 +(13, '​Albany',​ 4), 
 +(14, '​Birmingham',​ 5), 
 +(15, '​Montgomery',​ 5), 
 +(16, '​Huntsville',​ 5), 
 +(17, 'Los Angeles',​ 6), 
 +(18, 'San Francisco',​ 6), 
 +(19, 'San Diego',​ 6), 
 +(20, '​Toronto',​ 7), 
 +(21, '​Ottawa',​ 7), 
 +(22, '​Vancouver',​ 8), 
 +(23, '​Victoria',​ 8), 
 +(24, '​Sydney',​ 9), 
 +(25, '​Newcastle',​ 9), 
 +(26, 'City of Brisbane',​ 10), 
 +(27, 'Gold Coast',​ 10);
  
  
 +ALTER TABLE `tree_country_state_city`
 +  ADD PRIMARY KEY (`id`);
  
-    public function isPseudoExists($info) 
-    { 
-        // On veut voir si tel pseudo ayant pour id $info existe.  ​ 
-        if (is_int($info)) 
-        { 
-            $q = $this->​pdo->​query("​SELECT COUNT(*) FROM personne WHERE id =$info;"​)->​fetchColumn();​ 
-        } 
-        else 
-        { 
-        // Sinon, c'est qu'on veut vérifier que le "​pseudo"​ existe ou pas. 
-        $clean_info= $this->​pdo->​quote($info);​ 
-        $q = $this->​pdo->​query("​SELECT COUNT(*) FROM personne WHERE pseudo=$clean_info;"​)->​fetchColumn();​ 
-        } 
-  ​ 
-        return (bool)$q; 
-    } 
- 
-  
-</​code>  ​ 
  
-=== Traitements ​===+ALTER TABLE `tree_country_state_city` 
 +  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,​AUTO_INCREMENT=28; 
 + 
 +</​code>​ 
 + 
 +=== Executer le script=== 
 + 
 +<code user> 
 +mysql -u root -p < ~/​user/​Test/​nath_test.sql 
 +</​code>​ 
 +===== Objet PDO ===== 
 + 
 +<​code>​ 
 +└───public 
 +      |_ MysqlSingleton.php  
 +      |_ gestionnairePDO.php 
 +      |_ data_users.php 
 +      |_ data_tree.php 
 +      |_ index.php 
 +</​code>​ 
 + 
 +=== MysqlSingleton.php ​===
  
 <code php> <code php>
-//file name : ManagerIdentification.class.php 
 <?php <?php
 error_reporting(E_ALL);​ error_reporting(E_ALL);​
 ini_set('​display_errors',​ '​1'​);​ ini_set('​display_errors',​ '​1'​);​
-require '​../​../​dto/​RequetesIdentification.class.php';​ 
-require('​Validations.class.php'​);​ 
  
-class ManagerIdentification+class MysqlSingleton
 { {
-    private $requeteIdentification+ const SQL_USER = '​root'​
-    ​private $msg1 "<p style='color:​purple'>Vous n'êtes pas identifié...</​p>"​+    ​const SQL_HOST ​'​localhost';​ 
-    private $msg2 "<p style='color:purple'>Vous n'êtes plus identifiéveuillez vous identifier à nouveau...</​p>"​+    const SQL_PASS ​= 'arawak'
-    ​private ​$msg3 = "<p style='color:red'>Pseudo et/ou mot de passe incorrect(s)...</​p>​"; +    const SQL_DTB = '​nath_test'; 
-    ​private ​$msg4 "<p style='​color:​blue'​>Bienvenue </​p>"​+    private ​static ​$connect  ​null; 
- + private $message ​null; 
- +  
-////   ​Conctructor //// + private function __construct() 
- +
-    ​public ​function ​__construct()+ // A singleton should not be instanced ! 
 +
 + 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')
 + echo "​CREATION CONNEXION PDO MYSQL"; 
 +
 + catch(PDOException ​$e) 
 +
 +   $this->​message=$e->getMessage()
 +   die(); 
 + } 
 +
 + return self::​$connect;​ 
 +
 +  
 + protected function getStatus() 
 +
 + return !is_null(self::​$connect);​ 
 +
 +  
 + protected function getErreurMessage() 
 +
 + return $this->​message;​ 
 + } 
 +  
 +    ​protected ​function ​disconnectMysql()
     {     {
-        $this->​requeteIdentification ​new RequetesIdentification();+        ​self::$connect ​null;
     }     }
 +}
 +</​code>​
  
 +=== gestionnairePDO.php ===
  
 +<code php>
  
 +</​code>​
  
-//////////////////////////////////// ​  ​méthodes pour la page identification.php   ///////////////////////////////​+=== data_users.php ===
  
-    private function creerTableauInfosIdentification($pseudo,​ $motpasse) +<code php>
-    { +
-        $infos = array ( +
-                '​pseudo'​ =$pseudo, +
-                '​motPasse'​ => $motpasse,​ +
-        ); +
-        return $infos; +
-    }+
  
-    private function infosAffichageSubmitIdentification() 
-    { 
-        if ( $_SERVER["​REQUEST_METHOD"​] == "​POST"​ && !empty($_POST)) 
-        { 
-            $pseudo = htmlspecialchars_decode((trim($_POST['​pseudoInput'​]))); ​       ​ 
-            $motPasse = "";​ 
-            $infos = $this->​creerTableauInfosIdentification($pseudo,​ $motPasse); 
-            //​var_dump($infos);​ 
-            return $infos; 
-        } 
-    } 
-} 
 </​code>​ </​code>​
  
-=== Front dossier public par exemple ​===+=== data_tree.php ​===
  
 <code php> <code php>
-// fale name : identification.php 
  
-<?php +</code>
-    error_reporting(E_ALL);​ +
-    ini_set('​display_errors',​ '​1'​);​ +
-    require '../../​outils/​ManagerIdentification.class.php';​ +
-?>+
  
-<​!DOCTYPE html> +=== index.php ===
-<​html>​ +
-    <​head>​ +
-        <meta http-equiv="​Content-Type"​ content="​text/​html;​ charset=UTF-8">​ +
-        <​title>​formulaire d'​identification</​title>​ +
-        <link rel="​stylesheet"​ href="../​../​style/​masterPage.css">​ +
-        <link rel="​stylesheet"​ href="​../​../​style/​formulaire.css">​ +
-        <link rel="​icon"​ type="​image/​x-icon"​ href="​../​../​images/​hypathieIco.ico"​ /> +
-    </​head>​ +
-    <​body>​ +
-        <div class="​header">​ +
-            <?php include("​../​includes/​headerPages.php"​);​ ?> +
-        </​div>​+
  
-        ​<div class="​body">​ +<code php>
-            <div class="​validation">​ +
-                <?php +
-                    $managerIdentification = new ManagerIdentification();​ +
-                    $infos = $managerIdentification->​infosVideIdentification();​ +
-                    $infos = $managerIdentification->​creerIdentification();​ +
-                     +
-                ?> +
-            </​div>​ +
-            <div class="​formulaire">​ +
-                <form action="<?​php echo htmlspecialchars($_SERVER["​PHP_SELF"​]);?>"​ method="​post">​ +
-                    <​legend>​S'​identifier : </​legend>​ +
-                        <​p><​span class="​error">​* Champs obligatoires</​span></​p>​ +
-                        <​label>​Pseudonyme</​label>​ +
-                        </​br>​ +
-                        <input type="​text"​ name="​pseudoInput"​ value="<?​php echo $infos['​pseudo'​] ?>">​ +
-                        <span class="​error">​*</​span><​br /> +
-                        <​label>​Mot de passe</​label>​ +
-                        </​br>​ +
-                        <input type="​password"​ name="​passInput"​ value="<?​php echo $infos['​motpasse'​] ?>"​ +
-                               ​placeholder="​**************"​ +
-                        > +
-                        <span class="​error">​*</​span><​br /> +
-                        <input type="​submit"​ name="​connexion"​ value="​Connexion"​  +
-                        <?php '<​script type="​text/​javascript">​location.reload(); ​ </​script>'​ ?>  +
-                        /> +
-                </​form>​ +
-            </​div>​ +
-        </​div>​ +
-        <div class="​footer">​ +
-            <​p>&​copy;​ 2020 comgocom.pw<​p>​ +
-        </​div>​ +
-    </​body>​ +
- +
-</​html>​ +
-<?php +
-    $managerIdentification->​msgInfoCookie();​ +
-?>+
  
 </​code>​ </​code>​
- 
- 
- 
- 
- 
utilisateurs/hypathie/tutos/php_connexion_mysql.txt · Dernière modification: 08/07/2021 17:34 par Hypathie

Pied de page des forums

Propulsé par FluxBB