简体   繁体   English

显示来自INNER JOIN的结果

[英]Display result from an INNER JOIN

I would like to display results from an INNER JOIN the easiest way as possible. 我想以最简单的方式显示INNER JOIN的结果。 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). 我知道我的SQL语句可以正常工作(我已经在MySQL中对其进行了测试)。 I'm using PDO. 我正在使用PDO。 The only thing that is print_ here is the number "1". 这里唯一的print_是数字“ 1”。

I would like to understand why this code does not work. 我想了解为什么此代码不起作用。 Isn't my variable $resultat an array? 我的变量$ resultat不是数组吗? Can't I print_r the result from a join? 我不能print_r联接的结果吗? 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. execute()仅返回表示错误的布尔值。 so, print_r($resultat) prints true or false . 因此, print_r($resultat)打印truefalse

The answer for your question: 您问题的答案:

What is the easiest war to display this result 显示此结果最简单的战争是什么

use fetchAll() . 使用fetchAll() You can see it here PHP manaual 你可以在这里看到它的PHP手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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