title: Select all data WHERE x = $_SESSION value
body: <p>Since I have already posted this and some people discarded my question as a "duplicate" without actually answering it (the page where they told me to look wasn't relevant), and I still can't get any answer, here it is, again... </p>
  
  <p>I would like a page to display all results corresponding with a <code>id_slogan</code> that was set randomly on a previous page. So on the first page, a random info from the DB <code>liste_slogans</code> is generated, and its id is kept in a <code>$_SESSION</code> variable. On each on the three different pages involved, I start the session and I only destroy it at the end of the last one. In short, the first page is where the visitor interacts with the website by entering data, the second processes it and then redirects to the last page with a data list that is supposed to be linked to the information on the first page. Here's some code from the first one :</p>
  
  <pre><code>&lt;?php
      $bdd=new PDO('mysql:host=localhost;dbname=slogans', 'root', 'root', array(PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION));
      $reponse = $bdd-&gt;query('SELECT id, slogan, compagnie FROM liste_slogans ORDER BY rand() LIMIT 1,1');
      header("Content-type: text/html;charset=UTF-8");
  
      while ($donnees = $reponse-&gt;fetch())
      {
          echo '&lt;p class="apparition1" element style="color:aqua"&gt;' . ($donnees['slogan']) . ' &lt;element style="color:transparent"&gt;' . ($donnees['id']) . '' . ($donnees['compagnie']) . '&lt;/element style&gt;&lt;/p&gt;';
          $_SESSION['id_slogan']=$donnees['id'];
          $_SESSION['compagnie']=$donnees['compagnie'];
          $_SESSION['slogan']=$donnees['slogan']; 
      }                   
  ?&gt;
  </code></pre>
  
  <p>On the last page, I want to display all the entries in the DB <code>entrees</code> which have a <code>id_slogan</code> matching with the session variable set on the first page (<code>$_SESSION['id_slogan']</code>). I have tried this and many other things, but nothing seems to work :</p>
  
  <pre><code>&lt;?php
      try
      {
          $bdd=new PDO('mysql:host=localhost;dbname=slogans;charset=utf8', 'root', 'root');
      }
      catch(Exception $e)
      {
          die('Erreur : '.$e-&gt;getMessage());
      }   
  
      $nbid = $_SESSION['id_slogan'];
      $reponse=$bdd-&gt;query('SELECT pseudo, entree, id_slogan FROM entrees WHERE id_slogan = '.$nbid' ORDER BY ID DESC LIMIT 0, 50');
  
      while ($donnees=$reponse-&gt;fetch())
      {
          echo '&lt;h4 class=resultats&gt;&lt;strong&gt;' . htmlspecialchars($donnees['entree']) . ' &lt;/strong&gt;&lt;/h4&gt; &lt;p class=resultats&gt; ' . htmlspecialchars($donnees['pseudo']) . '&lt;/p&gt;';
      }
      $reponse-&gt;closeCursor();
  ?&gt;  
  </code></pre>
  
  <p>Could anyone please help me with this? I've spent hours searching on the internet but can't seem to find any answer... </p>
  
  <p>Edit : I guess I should show all my code... </p>
  
  <p>There's the first page :</p>
  
  <pre><code>    &lt;?php
  // Start the session
  session_start();
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta charset=utf-8/&gt;
          &lt;link rel="stylesheet" href="Pubweb1.css"/&gt;
          &lt;title&gt;(re)trouver le sens&lt;/title&gt;
          &lt;!--[if It IE9]&gt;
              &lt;script src="http://html5shiv.googlecode.com/svn/trunk/html15.js"&gt;&lt;/script&gt;
          &lt;![endif]--&gt;
      &lt;/head&gt;
  
      &lt;div class="tout"&gt;  
      &lt;body&gt;
      &lt;!--[if IE 6]&gt;&lt;body class="ie6 old_ie"&gt;&lt;!--[endif]--&gt;
      &lt;!--[if IE 7]&gt;&lt;body class="ie7 old_ie"&gt;&lt;!--[endif]--&gt;
      &lt;!--[if IE 8]&gt;&lt;body class="ie8"&gt;&lt;!--[endif]--&gt;
      &lt;!--[if IE 9]&gt;&lt;body class="ie9"&gt;&lt;!--[endif]--&gt;
      &lt;!--[if ! IE]&gt;&lt;body&gt;&lt;!--&gt;&lt;!--[endif]--&gt;
  
      &lt;div class="barredef"&gt;
          &lt;?php include("barredef.php");?&gt;
      &lt;/div&gt;
  
      &lt;div class="blocpage"&gt;
          &lt;header&gt;
              &lt;?php include("menu1.php");?&gt;
          &lt;/header&gt;
  
  
  
          &lt;!-- Affichage aléatoire d'une donnée de la table et enregistrement des infos ID, slogan et compagnie --&gt;
          &lt;section&gt;
              &lt;?php
                  $bdd=new PDO('mysql:host=localhost;dbname=slogans', 'root', 'root', array(PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION));
                  $reponse = $bdd-&gt;query('SELECT ID, slogan, compagnie FROM liste_slogans ORDER BY rand() LIMIT 1,1');
                  header("Content-type: text/html;charset=UTF-8");
  
                  while ($donnees = $reponse-&gt;fetch())
                  {
                      echo '&lt;p class="apparition1" element style="color:aqua"&gt;' . ($donnees['slogan']) . ' &lt;element style="color:transparent"&gt;' . ($donnees['ID']) . '' . ($donnees['compagnie']) . '&lt;/element style&gt;&lt;/p&gt;';
                      $_SESSION['id_slogan']=$donnees['ID'];
                      $_SESSION['compagnie']=$donnees['compagnie'];
                      $_SESSION['slogan']=$donnees['slogan']; 
                  }   
                  $nbid = $_SESSION['id_slogan'];             
              ?&gt;
              &lt;h2 class=apparition1&gt;(À vous de jouer...)&lt;/h2&gt;
              &lt;!-- Il faut que le slogan qui apparaît ait une entrée au moins --&gt;
          &lt;/section&gt;
  
          &lt;section&gt;
              &lt;br/&gt;&lt;h6&gt;Entrez votre question, votre idée, votre doute.&lt;br/&gt;
              Remettez en question l'énoncé publicitaire présenté.&lt;br/&gt;
              Gardez en tête ce que les mots signifient vraiment pour vous, et exprimez-le si vous le jugez pertinent.&lt;br/&gt;
              Pas inspiré(e)? Cliquez sur "Sélection aléatoire" dans le menu ci-haut.&lt;/h6&gt;
          &lt;form action='redirection.php' method='POST'&gt; 
              &lt;p&gt;&lt;input type="text" name="entree" id="entree" size="75" maxlenght="300" autofocus /&gt;
              &lt;label for='pseudo'&gt;&lt;h6&gt;Votre signature : &lt;/h6&gt;&lt;/label&gt;&lt;input type="text" name="pseudo" id="pseudo" size="75" maxlenght="30" /&gt;&lt;br/&gt;
              &lt;input type='hidden' name="id_slogan" id="id_slogan" size="40" maxlenght="4" value="&lt;?php echo $_SESSION['id_slogan'];?&gt;" /&gt;
              &lt;input type='hidden' name="compagnie" id="compagnie" size="40" maxlenght="4" value="&lt;?php echo $_SESSION['compagnie'];?&gt;" /&gt;&lt;br/&gt;&lt;br/&gt;
              &lt;input class="envoiemail" name="submit" type="submit" value="Soumettre"&gt;&lt;/p&gt;
          &lt;/form&gt;
  
              &lt;?php
          // Connexion à la base de données
              try
              {
                  $bdd=new PDO('mysql:host=localhost;dbname=slogans;charset=utf8', 'root', 'root');
              }
              catch(Exception $e)
              {
                  die('Erreur : '.$e-&gt;getMessage());
              }
  
              $reponse-&gt;closeCursor();
              ?&gt;
          &lt;/section&gt;
          &lt;/div&gt;
      &lt;/body&gt;
          &lt;/div&gt;
  
  &lt;/html
  </code></pre>
  
  <p>There's the processing/redirection page :</p>
  
  <pre><code>&lt;?php
  // Start the session
  session_start();
  ?&gt;
  &lt;?php
  //Connexion à la base de données
      try
      {
          $bdd = new PDO('mysql:host=localhost;dbname=slogans;charset=utf8', 'root', 'root');
      }   
      catch(Exception $e)
      {
          die('Erreur : '. $e-&gt;getMessage());
      }
  
  // Insertion de l'entrée à l'aide d'une requête préparée
      $req=$bdd-&gt;prepare('INSERT INTO entrees (pseudo, entree, date_entree, id_slogan) VALUES (?, ?, NOW(), ?)');
      $req-&gt;execute(array($_POST['pseudo'], $_POST['entree'], $_POST['id_slogan']));
  
  //Redirection du visiteur vers la page des résultats
      header('Location: resultats.php');
  ?&gt;
  </code></pre>
  
  <p>And there's the last one :</p>
  
  <pre><code>&lt;?php
  session_start();
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta charset="utf-8" /&gt;
          &lt;link rel="stylesheet" href="Pubweb1.css"/&gt;
          &lt;title&gt;(re)trouver le sens&lt;/title&gt;
          &lt;!--[if It IE9]&gt;
              &lt;script src="http://html5shiv.googlecode.com/svn/trunk/html15.js"&gt;&lt;/script&gt;
          &lt;![endif]--&gt;
      &lt;/head&gt;
  
          &lt;div class="tout"&gt;  
      &lt;div class="blocpage"&gt;
      &lt;body&gt;
      &lt;!--[if IE 6]&gt;&lt;body class="ie6 old_ie"&gt;&lt;!--[endif]--&gt;
      &lt;!--[if IE 7]&gt;&lt;body class="ie7 old_ie"&gt;&lt;!--[endif]--&gt;
      &lt;!--[if IE 8]&gt;&lt;body class="ie8"&gt;&lt;!--[endif]--&gt;
      &lt;!--[if IE 9]&gt;&lt;body class="ie9"&gt;&lt;!--[endif]--&gt;
      &lt;!--[if ! IE]&gt;&lt;body&gt;&lt;!--&gt;&lt;!--[endif]--&gt;
  
          &lt;header&gt;
              &lt;?php include("menu1.php");?&gt;   
          &lt;/header&gt;
  
          &lt;section&gt;
              &lt;h1&gt;&lt;?php echo $_SESSION['slogan']; 
              $nbid = $_SESSION['id_slogan']; ?&gt;&lt;/h1&gt;
          &lt;/section&gt;
  
          &lt;section&gt;
              &lt;p class=boutonsuivant&gt;&lt;a href=aleatoire.php&gt;Suivant&lt;/a&gt;&lt;/p&gt;
              &lt;p class=compagnie&gt;&lt;?php echo $_SESSION['compagnie']; ?&gt;&lt;/p&gt;
              &lt;p class=boutonnav&gt;&lt;a href=slogan1.php&gt;Naviguer&lt;/a&gt;&lt;/p&gt;
          &lt;/section&gt;
  
          &lt;p&gt;Numéro : &lt;?php echo $_SESSION['id_slogan']; ?&gt;.&lt;/p&gt;
  
          &lt;section&gt;
          &lt;?php
  // Connexion à la base de données
          try
          {
              $bdd=new PDO('mysql:host=localhost;dbname=slogans;charset=utf8', 'root', 'root');
          }
          catch(Exception $e)
          {
              die('Erreur : '.$e-&gt;getMessage());
          }   
  
  // Récupération des derniers messages
          $reponse=$bdd-&gt;query('SELECT pseudo, entree, id_slogan FROM entrees WHERE id_slogan = $nbid ORDER BY ID DESC LIMIT 0, 50');
          $bdd-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
  // Affichage de chaque message (toutes les données sont protégées par htmlspecialchrs)
          while ($donnees=$reponse-&gt;fetch())
          {
              echo '&lt;h4 class=resultats&gt;&lt;strong&gt;' . htmlspecialchars($donnees['entree']) . ' &lt;/strong&gt;&lt;/h4&gt; &lt;p class=resultats&gt; ' . htmlspecialchars($donnees['pseudo']) . '&lt;/p&gt;';
          }
          $reponse-&gt;closeCursor();
          ?&gt;  
  
          &lt;?php
          session_destroy();
          ?&gt;
          &lt;/section&gt;
  
      &lt;/body&gt;
      &lt;/div&gt;
      &lt;/div&gt;
  &lt;/html
  </code></pre>