title: Warning: Invalid argument supplied for foreach() ->for trying to make paging in dynamic page
body: <p>This question <strong>DOES NOT</strong> already has an answer here:
  PDO Statement Returns FALSE 1 answer</p>
  
  <p><strong>The above question is only error handling see the errors I get! (already have error handling) and all my queries are working if I run them in phpmyadmin.</strong></p>
  
  <p>Hi I am trying to get paging into my dynamic page. But I get the warning:  </p>
  
  <p>Warning: Invalid argument supplied for foreach() on line 137</p>
  
  <p>I have tried to fix it but I can seem to get it to work. I have tried to change </p>
  
  <pre><code>foreach($results as $row)
  </code></pre>
  
  <p>To: </p>
  
  <pre><code>foreach($row = $results-&gt;fetch(PDO::FETCH_ASSOC)) 
  </code></pre>
  
  <p>And</p>
  
  <pre><code>foreach($row = $results-&gt;fetchAll())
  </code></pre>
  
  <p>But than I get the error:</p>
  
  <p>Parse error: syntax error, unexpected ')' on line 137</p>
  
  <p><strong>I have tried to find the answer on the internet but i can find one that fixes this problem.</strong></p>
  
  <p>Does somebody know how I can fix this? And what I am doing wrong... I try to work with PDO. I am a still a little new to PHP and PDO.</p>
  
  <p>My code:</p>
  
  <pre><code>    &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"&gt;&lt;/script&gt; 
  &lt;?php 
      include('php/loginHandlerconnect.php'); 
  
      ini_set('display_errors',1);
      ini_set('display_startup_errors',1);
      error_reporting(-1);
  
      $db = loginHandlerconnect(); 
  
      //$tableName="vacature";        
      $targetpage = "vacatureoverzicht";  
      $limit = 10; 
  
      $query = "SELECT COUNT(vacatureID) as num FROM vacature";
      $res = $db-&gt;query($query);
      $total_pages = $res-&gt;fetch(PDO::FETCH_ASSOC);
      $total_pages = $total_pages['num'];
  
      $stages = 3;
      $page = $db-&gt;quote($_GET['page']);
      if($page){
          $start = ($page - 1) * $limit; 
      }else{
          $start = 0; 
      }
  
      $sql = 'SELECT vacature.*, werkgever.image, werkgever.plaats, werkgever.provincie, werkgever.naamBedrijf FROM vacature LEFT JOIN werkgever ON (vacature.werkgeverID = werkgever.werkgeverID) ORDER BY vacature.datum DESC LIMIT '. $start .', '. $limit .''; 
      $results = $db-&gt;query($sql); 
  
      // Initial page num setup
      if ($page == 0){$page = 1;}
      $prev = $page - 1;  
      $next = $page + 1;                          
      $lastpage = ceil($total_pages/$limit);      
      $LastPagem1 = $lastpage - 1;                    
  
  
      $paginate = '';
      if($lastpage &gt; 1)
      {   
          $paginate .= "&lt;div class='paginate'&gt;";
          // Previous
          if ($page &gt; 1){
              $paginate.= "&lt;a href='$targetpage?page=$prev'&gt;previous&lt;/a&gt;";
          }else{
              $paginate.= "&lt;span class='disabled'&gt;previous&lt;/span&gt;";   }
  
          // Pages    
          if ($lastpage &lt; 7 + ($stages * 2))  // Not enough pages to breaking it up
          {   
              for ($counter = 1; $counter &lt;= $lastpage; $counter++)
              {
                  if ($counter == $page){
                      $paginate.= "&lt;span class='current'&gt;$counter&lt;/span&gt;";
                  }else{
                      $paginate.= "&lt;a href='$targetpage?page=$counter'&gt;$counter&lt;/a&gt;";}                    
              }
          }
          elseif($lastpage &gt; 5 + ($stages * 2))   // Enough pages to hide a few?
          {
              // Beginning only hide later pages
              if($page &lt; 1 + ($stages * 2))       
              {
                  for ($counter = 1; $counter &lt; 4 + ($stages * 2); $counter++)
                  {
                      if ($counter == $page){
                          $paginate.= "&lt;span class='current'&gt;$counter&lt;/span&gt;";
                      }else{
                          $paginate.= "&lt;a href='$targetpage?page=$counter'&gt;$counter&lt;/a&gt;";}                    
                  }
                  $paginate.= "...";
                  $paginate.= "&lt;a href='$targetpage?page=$LastPagem1'&gt;$LastPagem1&lt;/a&gt;";
                  $paginate.= "&lt;a href='$targetpage?page=$lastpage'&gt;$lastpage&lt;/a&gt;";       
              }
              // Middle hide some front and some back
              elseif($lastpage - ($stages * 2) &gt; $page &amp;&amp; $page &gt; ($stages * 2))
              {
                  $paginate.= "&lt;a href='$targetpage?page=1'&gt;1&lt;/a&gt;";
                  $paginate.= "&lt;a href='$targetpage?page=2'&gt;2&lt;/a&gt;";
                  $paginate.= "...";
                  for ($counter = $page - $stages; $counter &lt;= $page + $stages; $counter++)
                  {
                      if ($counter == $page){
                          $paginate.= "&lt;span class='current'&gt;$counter&lt;/span&gt;";
                      }else{
                          $paginate.= "&lt;a href='$targetpage?page=$counter'&gt;$counter&lt;/a&gt;";}                    
                  }
                  $paginate.= "...";
                  $paginate.= "&lt;a href='$targetpage?page=$LastPagem1'&gt;$LastPagem1&lt;/a&gt;";
                  $paginate.= "&lt;a href='$targetpage?page=$lastpage'&gt;$lastpage&lt;/a&gt;";       
              }
              // End only hide early pages
              else
              {
                  $paginate.= "&lt;a href='$targetpage?page=1'&gt;1&lt;/a&gt;";
                  $paginate.= "&lt;a href='$targetpage?page=2'&gt;2&lt;/a&gt;";
                  $paginate.= "...";
                  for ($counter = $lastpage - (2 + ($stages * 2)); $counter &lt;= $lastpage; $counter++)
                  {
                      if ($counter == $page){
                          $paginate.= "&lt;span class='current'&gt;$counter&lt;/span&gt;";
                      }else{
                          $paginate.= "&lt;a href='$targetpage?page=$counter'&gt;$counter&lt;/a&gt;";}                    
                  }
              }
          }
  
                  // Next
          if ($page &lt; $counter - 1){ 
              $paginate.= "&lt;a href='$targetpage?page=$next'&gt;next&lt;/a&gt;";
          }else{
              $paginate.= "&lt;span class='disabled'&gt;next&lt;/span&gt;";
              }
  
          $paginate.= "&lt;/div&gt;";       
      }
      echo $total_pages.' Results';
      // pagination
      echo $paginate; 
  
      /*
      ini_set('display_errors',1);
      ini_set('display_startup_errors',1);
      error_reporting(-1);
      */
      /* 
      * Nu hebben we een resultset met de volgende data per row 
      * vacatureID 
      * werkgeverID 
      * functie 
      * datum 
      * omschrijvingKort 
      * omschrijving 
      * image (werkgever) 
      * plaats (werkgever) 
      * provincie (werkgever) 
      * naamBedrijf (werkgever) 
      */ 
      foreach($results as $row) // line 137
      { 
          //Clean record 
          cleanRecord($row);
          echo generateDiv($row); 
      }
  
      function cleanRecord($row) 
      { 
          if (strlen($row['functie']) &gt; 20) { 
  
              // truncate string 
              $stringCut = substr($row['functie'], 0, 20); 
  
              // make sure it ends in a word so assassinate doesn't become ass... 
              $row['functie'] = substr($stringCut, 0, strrpos($stringCut, ' ')).'...'; 
          } 
          if (strlen($row['naamBedrijf']) &gt; 25) { 
  
              // truncate string 
              $stringCut2 = substr($row['naamBedrijf'], 0, 14); 
  
              // make sure it ends in a word so assassinate doesn't become ass... 
              $row['naamBedrijf'] = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
          } 
          if (strlen($row['omschrijvingKort']) &gt; 63) { 
  
              // truncate string 
              $stringCut2 = substr($row['omschrijvingKort'], 0, 63); 
  
              // make sure it ends in a word so assassinate doesn't become ass... 
              $row['omschrijvingKort'] = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
          } 
  
          return $row; 
      } 
      function generateDiv($row) 
      { 
          $date = date("d-m-Y", strtotime($row['datum'])); 
          return " 
              &lt;div class='vacatureinfo2'&gt; 
                  &lt;img class='userimg2' src='../../uploads/userimage/". $row['image'] ."' id='imge'/&gt; 
                  &lt;p class='func-br-pl pull-left'&gt;". $row['functie'] ." - ". $row['naamBedrijf'] ." - ". $row['plaats'] ."&lt;/p&gt;&lt;p class='dtm pull-right'&gt;". $date ."&lt;/p&gt; 
                  &lt;div class='omschrijvingkort2'&gt; 
                      ". $row['omschrijvingKort'] ." 
                  &lt;/div&gt; 
                  &lt;p class='pull-right'&gt;&lt;a href='reactie?id=" . $row['vacatureID'] . "'&gt;Reageer&lt;/a&gt; | &lt;a href='informatie-vacature?id=" . $row['vacatureID'] . "'&gt;Meer informatie&lt;/a&gt;&lt;/p&gt; 
              &lt;/div&gt; 
              &lt;hr&gt; 
          "; 
      }
  ?&gt;
  </code></pre>
  
  <p>UPDATE:</p>
  
  <pre><code>    &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"&gt;&lt;/script&gt; 
  &lt;?php 
      include('php/loginHandlerconnect.php'); 
  
      ini_set('display_errors',1);
      ini_set('display_startup_errors',1);
      error_reporting(-1);
  
      $db = loginHandlerconnect(); 
  
      $targetpage = "vacatureoverzicht";  
      $limit = 10; 
  
      $query = "SELECT COUNT(vacatureID) as num FROM vacature";
      $res = $db-&gt;query($query);
      $total_pages = $res-&gt;fetch(PDO::FETCH_ASSOC);
      $total_pages = $total_pages['num'];
  
      $stages = 3;
      $page = $db-&gt;quote($_GET['page']);
      if($page){
          $start = ($page - 1) * $limit; 
      }else{
          $start = 0; 
      }
  
      $sql = $db-&gt;query('SELECT vacature.*, werkgever.image, werkgever.plaats, werkgever.provincie, werkgever.naamBedrijf FROM vacature LEFT JOIN werkgever ON (vacature.werkgeverID = werkgever.werkgeverID) ORDER BY vacature.datum DESC LIMIT '. $start .', '. $limit .''); 
      $results = $sql-&gt;fetchAll();
  
      // Initial page num setup
      if ($page == 0){$page = 1;}
      $prev = $page - 1;  
      $next = $page + 1;                          
      $lastpage = ceil($total_pages/$limit);      
      $LastPagem1 = $lastpage - 1;                    
  
  
      $paginate = '';
      if($lastpage &gt; 1)
      {   
          $paginate .= "&lt;div class='paginate'&gt;";
          // Previous
          if ($page &gt; 1){
              $paginate.= "&lt;a href='". $targetpage ."?page=". $prev ."'&gt;previous&lt;/a&gt;";
          }else{
              $paginate.= "&lt;span class='disabled'&gt;previous&lt;/span&gt;";   
          }
  
          // Pages    
          if ($lastpage &lt; 7 + ($stages * 2))  // Not enough pages to breaking it up
          {   
              for ($counter = 1; $counter &lt;= $lastpage; $counter++)
              {
                  if ($counter == $page){
                      $paginate.= "&lt;span class='current'&gt;". $counter ."&lt;/span&gt;";
                  }else{
                      $paginate.= "&lt;a href='". $targetpage ."?page=". $counter ."'&gt;". $counter ."&lt;/a&gt;";}                  
              }
          }
          elseif($lastpage &gt; 5 + ($stages * 2))   // Enough pages to hide a few?
          {
              // Beginning only hide later pages
              if($page &lt; 1 + ($stages * 2))       
              {
                  for ($counter = 1; $counter &lt; 4 + ($stages * 2); $counter++)
                  {
                      if ($counter == $page){
                          $paginate.= "&lt;span class='current'&gt;". $counter ."&lt;/span&gt;";
                      }else{
                          $paginate.= "&lt;a href='". $targetpage ."?page=". $counter ."'&gt;". $counter ."&lt;/a&gt;";}                  
                  }
                  $paginate.= "...";
                  $paginate.= "&lt;a href='". $targetpage ."?page=". $LastPagem1 ."'&gt;". $LastPagem1 ."&lt;/a&gt;";
                  $paginate.= "&lt;a href='". $targetpage ."?page=". $lastpage ."'&gt;". $lastpage ."&lt;/a&gt;";     
              }
              // Middle hide some front and some back
              elseif($lastpage - ($stages * 2) &gt; $page &amp;&amp; $page &gt; ($stages * 2))
              {
                  $paginate.= "&lt;a href='". $targetpage ."?page=1'&gt;1&lt;/a&gt;";
                  $paginate.= "&lt;a href='". $targetpage ."?page=2'&gt;2&lt;/a&gt;";
                  $paginate.= "...";
                  for ($counter = $page - $stages; $counter &lt;= $page + $stages; $counter++)
                  {
                      if ($counter == $page){
                          $paginate.= "&lt;span class='current'&gt;". $counter ."&lt;/span&gt;";
                      }else{
                          $paginate.= "&lt;a href='". $targetpage ."?page=". $counter ."'&gt;". $counter ."&lt;/a&gt;";}                  
                  }
                  $paginate.= "...";
                  $paginate.= "&lt;a href='". $targetpage ."?page=". $LastPagem1 ."'&gt;". $LastPagem1 ."&lt;/a&gt;";
                  $paginate.= "&lt;a href='". $targetpage ."?page=". $lastpage ."'&gt;". $lastpage ."&lt;/a&gt;";     
              }
              // End only hide early pages
              else
              {
                  $paginate.= "&lt;a href='". $targetpage ."?page=1'&gt;1&lt;/a&gt;";
                  $paginate.= "&lt;a href='". $targetpage ."?page=2'&gt;2&lt;/a&gt;";
                  $paginate.= "...";
                  for ($counter = $lastpage - (2 + ($stages * 2)); $counter &lt;= $lastpage; $counter++)
                  {
                      if ($counter == $page){
                          $paginate.= "&lt;span class='current'&gt;". $counter ."&lt;/span&gt;";
                      }else{
                          $paginate.= "&lt;a href='". $targetpage ."?page=". $counter ."'&gt;". $counter ."&lt;/a&gt;";}                  
                  }
              }
          }
  
                  // Next
          if ($page &lt; $counter - 1){ 
              $paginate.= "&lt;a href='". $targetpage ."?page=". $next ."'&gt;next&lt;/a&gt;";
          }else{
              $paginate.= "&lt;span class='disabled'&gt;next&lt;/span&gt;";
              }
  
          $paginate.= "&lt;/div&gt;";       
      }
      echo $total_pages.' vacatures';
  
      /*
      ini_set('display_errors',1);
      ini_set('display_startup_errors',1);
      error_reporting(-1);
      */
      /* 
      * Nu hebben we een resultset met de volgende data per row 
      * vacatureID 
      * werkgeverID 
      * functie 
      * datum 
      * omschrijvingKort 
      * omschrijving 
      * image (werkgever) 
      * plaats (werkgever) 
      * provincie (werkgever) 
      * naamBedrijf (werkgever) 
      */ 
      foreach($results as $row) 
      { 
          //Clean record 
          cleanRecord($row);
          echo generateDiv($row); 
      }
  
      function cleanRecord($row) 
      { 
          if (strlen($row['functie']) &gt; 20) { 
  
              // truncate string 
              $stringCut = substr($row['functie'], 0, 20); 
  
              // make sure it ends in a word so assassinate doesn't become ass... 
              $row['functie'] = substr($stringCut, 0, strrpos($stringCut, ' ')).'...'; 
          } 
          if (strlen($row['naamBedrijf']) &gt; 25) { 
  
              // truncate string 
              $stringCut2 = substr($row['naamBedrijf'], 0, 14); 
  
              // make sure it ends in a word so assassinate doesn't become ass... 
              $row['naamBedrijf'] = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
          } 
          if (strlen($row['omschrijvingKort']) &gt; 63) { 
  
              // truncate string 
              $stringCut2 = substr($row['omschrijvingKort'], 0, 63); 
  
              // make sure it ends in a word so assassinate doesn't become ass... 
              $row['omschrijvingKort'] = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
          } 
  
          return $row; 
      } 
      function generateDiv($row) 
      { 
          $date = date("d-m-Y", strtotime($row['datum'])); 
          return " 
              &lt;div class='vacatureinfo2'&gt; 
                  &lt;img class='userimg2' src='../../uploads/userimage/". $row['image'] ."' id='imge'/&gt; 
                  &lt;p class='func-br-pl pull-left'&gt;". $row['functie'] ." - ". $row['naamBedrijf'] ." - ". $row['plaats'] ."&lt;/p&gt;&lt;p class='dtm pull-right'&gt;". $date ."&lt;/p&gt; 
                  &lt;div class='omschrijvingkort2'&gt; 
                      ". $row['omschrijvingKort'] ." 
                  &lt;/div&gt; 
                  &lt;p class='pull-right'&gt;&lt;a href='reactie?id=" . $row['vacatureID'] . "'&gt;Reageer&lt;/a&gt; | &lt;a href='informatie-vacature?id=" . $row['vacatureID'] . "'&gt;Meer informatie&lt;/a&gt;&lt;/p&gt; 
              &lt;/div&gt; 
              &lt;hr&gt; 
          "; 
      }
  
      // pagination
      echo $paginate; 
  ?&gt;
  </code></pre>
  
  <p>UPDATE 2:</p>
  
  <p>Maybe there when something wrong with changing the code below to PDO?</p>
  
  <pre><code>&lt;?php
  include('connect.php'); 
  
  $tableName="vacature";      
  $targetpage = "testpaging2.php";    
  $limit = 10; 
  
  $query = "SELECT COUNT(vacatureID) as num FROM $tableName";
  $total_pages = mysql_fetch_array(mysql_query($query));
  $total_pages = $total_pages[num];
  
  $stages = 3;
  $page = mysql_escape_string($_GET['page']);
  if($page){
      $start = ($page - 1) * $limit; 
  }else{
      $start = 0; 
  }   
  
  // Get page data
  $query1 = "SELECT vacature.*, werkgever.image, werkgever.plaats, werkgever.provincie, werkgever.naamBedrijf FROM vacature LEFT JOIN werkgever ON (vacature.werkgeverID = werkgever.werkgeverID) ORDER BY vacature.datum DESC LIMIT $start, $limit";
  $result = mysql_query($query1);
  
  // Initial page num setup
  if ($page == 0){$page = 1;}
  $prev = $page - 1;  
  $next = $page + 1;                          
  $lastpage = ceil($total_pages/$limit);      
  $LastPagem1 = $lastpage - 1;                    
  
  
  $paginate = '';
  if($lastpage &gt; 1)
  {   
      $paginate .= "&lt;div class='paginate'&gt;";
      // Previous
      if ($page &gt; 1){
          $paginate.= "&lt;a href='$targetpage?page=$prev'&gt;previous&lt;/a&gt;";
      }else{
          $paginate.= "&lt;span class='disabled'&gt;previous&lt;/span&gt;";   }
  
      // Pages    
      if ($lastpage &lt; 7 + ($stages * 2))  // Not enough pages to breaking it up
      {   
          for ($counter = 1; $counter &lt;= $lastpage; $counter++)
          {
              if ($counter == $page){
                  $paginate.= "&lt;span class='current'&gt;$counter&lt;/span&gt;";
              }else{
                  $paginate.= "&lt;a href='$targetpage?page=$counter'&gt;$counter&lt;/a&gt;";}                    
          }
      }
      elseif($lastpage &gt; 5 + ($stages * 2))   // Enough pages to hide a few?
      {
          // Beginning only hide later pages
          if($page &lt; 1 + ($stages * 2))       
          {
              for ($counter = 1; $counter &lt; 4 + ($stages * 2); $counter++)
              {
                  if ($counter == $page){
                      $paginate.= "&lt;span class='current'&gt;$counter&lt;/span&gt;";
                  }else{
                      $paginate.= "&lt;a href='$targetpage?page=$counter'&gt;$counter&lt;/a&gt;";}                    
              }
              $paginate.= "...";
              $paginate.= "&lt;a href='$targetpage?page=$LastPagem1'&gt;$LastPagem1&lt;/a&gt;";
              $paginate.= "&lt;a href='$targetpage?page=$lastpage'&gt;$lastpage&lt;/a&gt;";       
          }
          // Middle hide some front and some back
          elseif($lastpage - ($stages * 2) &gt; $page &amp;&amp; $page &gt; ($stages * 2))
          {
              $paginate.= "&lt;a href='$targetpage?page=1'&gt;1&lt;/a&gt;";
              $paginate.= "&lt;a href='$targetpage?page=2'&gt;2&lt;/a&gt;";
              $paginate.= "...";
              for ($counter = $page - $stages; $counter &lt;= $page + $stages; $counter++)
              {
                  if ($counter == $page){
                      $paginate.= "&lt;span class='current'&gt;$counter&lt;/span&gt;";
                  }else{
                      $paginate.= "&lt;a href='$targetpage?page=$counter'&gt;$counter&lt;/a&gt;";}                    
              }
              $paginate.= "...";
              $paginate.= "&lt;a href='$targetpage?page=$LastPagem1'&gt;$LastPagem1&lt;/a&gt;";
              $paginate.= "&lt;a href='$targetpage?page=$lastpage'&gt;$lastpage&lt;/a&gt;";       
          }
          // End only hide early pages
          else
          {
              $paginate.= "&lt;a href='$targetpage?page=1'&gt;1&lt;/a&gt;";
              $paginate.= "&lt;a href='$targetpage?page=2'&gt;2&lt;/a&gt;";
              $paginate.= "...";
              for ($counter = $lastpage - (2 + ($stages * 2)); $counter &lt;= $lastpage; $counter++)
              {
                  if ($counter == $page){
                      $paginate.= "&lt;span class='current'&gt;$counter&lt;/span&gt;";
                  }else{
                      $paginate.= "&lt;a href='$targetpage?page=$counter'&gt;$counter&lt;/a&gt;";}                    
              }
          }
      }
  
              // Next
      if ($page &lt; $counter - 1){ 
          $paginate.= "&lt;a href='$targetpage?page=$next'&gt;next&lt;/a&gt;";
      }else{
          $paginate.= "&lt;span class='disabled'&gt;next&lt;/span&gt;";
          }
  
      $paginate.= "&lt;/div&gt;";       
  
  
  }
   echo $total_pages.' Results';
  
  
  
      while($row = mysql_fetch_array($result))
      {
  
      echo "
          &lt;div class='vacatureinfo2'&gt; 
              &lt;img class='userimg2' src='../../uploads/userimage/". $row['image'] ."' id='imge'/&gt; 
              &lt;p class='func-br-pl pull-left'&gt;". $row['functie'] ." - ". $row['naamBedrijf'] ." - ". $row['plaats'] ."&lt;/p&gt;&lt;p class='dtm pull-right'&gt;". $date ."&lt;/p&gt; 
              &lt;div class='omschrijvingkort2'&gt; 
                  ". $row['omschrijvingKort'] ." 
              &lt;/div&gt; 
              &lt;p class='pull-right'&gt;&lt;a href='reactie?id=" . $row['vacatureID'] . "'&gt;Reageer&lt;/a&gt; | &lt;a href='informatie-vacature?id=" . $row['vacatureID'] . "'&gt;Meer informatie&lt;/a&gt;&lt;/p&gt; 
          &lt;/div&gt; 
          &lt;hr&gt;";
  
      }
   // pagination
   echo $paginate;
  </code></pre>
  
  <p>Because when I run this page it works! But it is not PDO... See the code before this update to see how I changed the code to PDO please note that I also use functions in that code! If I know what I am doing wrong with changing it to PDO maybe that will fix this problem? But I do not know what I am doing wrong!!</p>