title: Can’t get all the results using fetch(PDO::FETCH_ASSOC);
body: <p>I am trying to get a list of sites ids according to a user id.</p>
  <p>This is what I've got so far.</p>
  <pre><code>public function getSites() {
      $stmt = $this-&gt;db-&gt;prepare(&quot;SELECT Sites_idSites FROM favorites WHERE User_idUser=:idUser&quot;);
      $userId = $_SESSION['user_session']['idUser'];
      $stmt-&gt;bindparam(&quot;:idUser&quot;, $userId);
      $stmt-&gt;execute();
      $siteList = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
  
      foreach($siteList as $value) {
          foreach($value as $key) {
          $stmt = $this-&gt;db-&gt;prepare(&quot;SELECT * FROM sites WHERE idSites=:siteList&quot;);
          $stmt-&gt;bindparam(&quot;:siteList&quot;, $key);
      }
      $stmt-&gt;execute();
      $userSites = $stmt-&gt;fetch(PDO::FETCH_ASSOC);
  }
  
  return $userSites;
  </code></pre>
  <p>This is the part that I go to &quot;favorites&quot; table and get all the site's ids that correspond to the user id.</p>
  <pre><code>$stmt = $this-&gt;db-&gt;prepare(&quot;SELECT Sites_idSites FROM favorites WHERE User_idUser=:idUser&quot;);
  $userId = $_SESSION['user_session']['idUser'];
  $stmt-&gt;bindparam(&quot;:idUser&quot;, $userId);
  $stmt-&gt;execute();
  $siteList = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
  </code></pre>
  <p>$siteList return as an array like this:</p>
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [Sites_idSites] =&gt; 20
          )
  
      [1] =&gt; Array
      (
          [Sites_idSites] =&gt; 21
      )
  
      [2] =&gt; Array
      (
          [Sites_idSites] =&gt; 22
      )
   )
  </code></pre>
  <p>Now, I want to go to the table where all the sites are and only get the ones with those ids.</p>
  <p>This is what I am using, but it only gets the last one:</p>
  <pre><code>foreach($siteList as $value) {
      foreach($value as $key) {
          $stmt = $this-&gt;db-&gt;prepare(&quot;SELECT * FROM sites WHERE idSites=:siteList&quot;);
          $stmt-&gt;bindparam(&quot;:siteList&quot;, $key);
      }
      $stmt-&gt;execute();
      $userSites = $stmt-&gt;fetch(PDO::FETCH_ASSOC);
   }
  </code></pre>
  <p>If I do <code>print_r($userSites)</code>, I will return:</p>
  <pre><code>Array
  (
      [idSites] =&gt; 22
      [name] =&gt; rwrwer
      [url] =&gt; werwerwerwer
      [Category_idCategory] =&gt; 1
  )
  </code></pre>
  <p>As you can see, it is only returning the last one. How do I make it return an array with all the sites in it? Am I doing something wrong?</p>
  <hr/>
  <p>I finally fixed it:</p>
  <pre><code>public function getSites() {
      $stmt = $this-&gt;db-&gt;prepare(&quot;SELECT sites.* FROM favorites INNER JOIN sites ON favorites.Sites_idSites = sites.idSites WHERE favorites.User_idUser = :idUser&quot;);
      $userId = $_SESSION['user_session']['idUser'];
      $stmt-&gt;bindparam(&quot;:idUser&quot;, $userId);
      $stmt-&gt;execute();
      $siteList = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
  
      return $siteList;
  }
  </code></pre>