PHP #2

Créer des sessions et factoriser le code

Rappels

INSERT

<?php 

    $userName = $_POST["name"];
    $userAge = $_POST["age];
    
    $req = $connexion->prepare("INSERT INTO TABLE_NAME(name,age)
                                VALUES(:name,:age)");
    $req_e = $req->execute(array(
               'name'=>$userName,
               'age'=>$userAge
    ));
?>

Sélectionner des données

Requête

<?php 


    $monFiltre = "Toto";
    $req = $db->prepare('SELECT champ_1,champ_2, champ_3 
                            FROM maTable 
                            WHERE champ_1 = :quelqueChose');
    $req_e = $req->execute(array(
        'quelqueChose' => $monFiltre
    ));

?>

Sélectionner des données

Récupérer les résultats

Deux façons : 

FetchAll() : récupère tous les résultats sous forme de tableau

Fetch() : récupère une seule ligne

Sélectionner des données

Afficher les résultats - Avec un foreach

<?php 

    $monFiltre = "Toto";
    $req = $db->prepare('SELECT champ_1,champ_2, champ_3 
                            FROM maTable 
                            WHERE champ_1 = :quelqueChose');
    $req_e = $req->execute(array(
        'quelqueChose' => $monFiltre
    ));
    
    $resultats = $req->fetchAll();
    
    foreach($resultats as $result) {
        echo "Champ 1 :".$result["champ_1"]." <br/> Champ 2 :".$result["champ_2"];
    }
?>

FOREACH

<?php 
    $tableau = array('francois', 'Michel', 'Nicole');

    foreach($tableau as $key => value) {
        echo "Valeur :".$value." <br/> // Key :".$key;
    }
?>
Clé Valeur
0 francois
1 Michel
2 Nicole

Sélectionner des données

Afficher les résultats - Avec un while et fetch()

<?php 

    $monFiltre = "Toto";
    $req = $db->prepare('SELECT champ_1,champ_2, champ_3 
                            FROM maTable 
                            WHERE champ_1 = :quelqueChose');
    $req_e = $req->execute(array(
        'quelqueChose' => $monFiltre
    ));
    
    $resultats = $req->fetchAll();
    
    while($result = $req->fetch()) {
        echo "Champ 1 :".$result["champ_1"]." <br/> Champ 2 :".$result["champ_2"];
    }
?>

Les sessions

Connexion, déconnexion, variables de session

Pré-requis : formulaire

<form method="POST">

    <input type="email" placeholder="Email" name="email" />
    <input type="password" name="password" />
    <input type="submit" name="connexion" value="Connexion" />

</form>

Les sessions

Connexion, déconnexion, variables de session

Requête de connexion

<?php 

    if($_POST) {    
        $mail = $_POST["email"];
        $psw = $_POST["password"];
        
        $req = $db->prepare('SELECT COUNT(*) AS nb, utilisateur_id, nom, prenom 
                                FROM utilisateurs
                                WHERE email = :email AND password = :password');
        $req_e = $req->execute(array(
            'email'=>$mail,
            'password'=>$psw
        ));
    }

?>

Les sessions

Connexion, déconnexion, variables de session

Exploitation des résultats

<?php 
        // Suite du code
        
        $req_f = $req->fetch();
        $nb = $req_f["nb"];

        if($nb==1) {
            // On crée la session
            session_start();
            
            // On peut créer des variables de session qui vont nous servir pour la suite
            // Exemple : stocker le prénom
            $_SESSION["user_firstName"] = $req_f["prenom"];
        }
    }

?>

Les sessions

Connexion, déconnexion, variables de session

Afficher des erreurs

<?php 
        // Suite du code

        if($nb==1) {
            // On crée la session
            session_start();
            
            // On peut créer des variables de session qui vont nous servir pour la suite
            // Exemple : stocker le prénom de l'utilisateur
            $_SESSION["user_firstName"] = $req_f["prenom"];
        }
        else {
            // Le couple email/mdp est erroné
            echo "Le mail ou le mot de passe sont erronés";
        }
    }

?>

Les sessions

Connexion, déconnexion, variables de session

Lancer des sessions

<?php 

    session_start();

?>

Détruire une session

<?php 

    session_destroy();

?>

Factoriser le code

Include, require

Permet d'inclure des fichiers

 

Inclure systématiquement le fichier

include('connexion.php');

 

Inclure le fichier si ce n'est pas déjà fait

include_once('connexion.php');

Factoriser le code

Include, require

Inclure systématiquement le fichier

require('connexion.php');

 

Inclure le fichier si ce n'est pas déjà fait

require_once('connexion.php');

PHP #2

Des questions ? 

Birthday marrakech

By pierre dugard

Birthday marrakech

How to organize a birthday in marrakech? Do you have a birthday to celebrate in Marrakech? Your children's, your own or your best friend's, but you can't welcome all your guests in your home. Head to the guest house Tchina Mandarina in Marrakech to celebrate the occasion in a unique setting and a frantic atmosphere. In the Palmeraie of Marrakech, Riad Tchina Mandarina opens the doors of its little paradise to organise unforgettable birthday parties. ✔ An animated birthday party for children For the younger ones, Riad Tchina Mandarina in Marrakech is the ideal address to offer an afternoon as festive as it is memorable. In the large garden, the children have fun around fun and recreational outdoor games. Sponge, mirror, straw or pirate games, treasure hunts, observation or table football ... They spend part of the day snogging in a natural and privileged setting. All year round, the little ones take advantage of the two swimming pools to dive and play in the water for a successful birthday party in marrakech. The outdoor swimming pool is perfect in summer, and when the cold season arrives, the indoor pool with its water heated to 29° is perfect for exercising. At snack time, the traditional birthday cake is not forgotten. We blow out the candles in the shade of an olive tree and enjoy sweets and sweet treats before going back to play with friends until early evening. ✔ A crazy evening for the older children. Birthday parties are not just for the little ones, and Riad vendome also remains the perfect place to celebrate this occasion between adults. Day or night, make sure to invite your friends to a successful party at Tchina Mandarina in Marrakech. Afternoon chill-out at the waterfront, grills and salad buffet for lunch, aperitif at the bar at sunset, gourmet evening and wild party until the early hours of the morning, create unforgettable memories with Tchina Mandarina. The plus: make the pleasure last longer by staying overnight at Riad Tchina Mandarina. A dozen rooms welcome your guests and you can enjoy breakfast by the pool before you start celebrating again! According to your preferences and desires, Riad Tchina Mandarina takes care of everything. Dancers or Live Band, musical evening or generous dinner, choose the atmosphere, Tchina Mandarina will make your wishes come true. At any age and whatever the atmosphere you are looking for, have a successful birthday party by entrusting the organisation to Tchina Mandarina. All you have to do is enjoy!

  • 55
Loading comments...

More from pierre dugard