简体   繁体   中英

Display result from an INNER JOIN

I would like to display results from an INNER JOIN the easiest way as possible. I tried this but that does not work:

<?php
        $serveur  = "localhost";
        $login = "root";
        $pass = "root"; 

        try{
            $conn = new PDO("mysql:host=$serveur;dbname=test2", $login, $pass);

            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            //On place notre requête dans une variable
            $jointure_int =
                "SELECT Inscrits.prenom, Commentaires.commentaire
                FROM Inscrits
                INNER JOIN Commentaires
                ON Inscrits.id = Commentaires.id_inscrit";

            //On prépare notre requête
            $requete = $conn->prepare($jointure_int);

            //On exécute notre requête
            $resultat = $requete->execute();

            echo "<pre>";
            print_r($resultat);
            echo "</pre>";

        }

        catch(PDOException $e) { 
            echo 'Echec : ' . $e->getMessage(); 
        }
?>

I know my SQL statement works fine (I've tested it in MySQL). I'm using PDO. The only thing that is print_ here is the number "1".

I would like to understand why this code does not work. Isn't my variable $resultat an array? Can't I print_r the result from a join? What is the easiest war to display this result (am I obliged to fetch everything?)?

I created this ode only for self-training purpose, I do not mean to apply it in any website by the way.

Thank you in advance!

execute() just returns boolean value which indicates there was an error. so, print_r($resultat) prints true or false .

The answer for your question:

What is the easiest war to display this result

use fetchAll() . You can see it here PHP manaual

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM