title: Invalid argument supplied for foreach; PDO
body: <p>I have seen <a href="https://stackoverflow.com/questions/13926359/pdo-invalid-argument-supplied-for-foreach">this</a> and <a href="https://stackoverflow.com/questions/12988109/pdo-invalid-argument-supplied-for-foreach">this</a>, but neither of these help my situation, this is why I am knowledgeably opening a question that has been asked before.</p>
  
  <hr>
  
  <p>My code is:</p>
  
  <pre><code>&lt;?
      $getGame = $database-&gt;prepare('SELECT * FROM mainSite_games WHERE id=:gameID');
  ?&gt;
      &lt;div class='container_12'&gt;
          &lt;div id='contentcont'&gt;
              &lt;div id='content'&gt;
              &lt;?
              if (isset($_GET['gameID'])) {
                  $getGame-&gt;bindValue(':gameID',$GET['gameID'],PDO::PARAM_INT);
                  $getGame-&gt;execute();
                  $rows = $getGame-&gt;fetch(PDO::FETCH_ASSOC);
  
                  foreach ($rows as $game) {
  
                      $gameName = str_replace(' ','_',strtolower(stripslashes($game['gameName'])));
              ?&gt;
                      &lt;section class='&lt;? echo $gameName; ?&gt;'&gt;
  
  
                          &lt;h1&gt;&lt;? echo stripslashes($game['gameName']); ?&gt;&lt;/h1&gt;
  
                          &lt;p&gt;&lt;? echo stripslashes($game['gameDesc']); ?&gt;&lt;/p&gt;
  
  
                          &lt;article class='grid_5 alpha' style='float:left;'&gt;
  
                              &lt;h3&gt;&lt;a href='viewQuests.php?gameID=&lt;? echo $game['id'] ?&gt;'&gt;View &lt;? echo stripslashes($game['gameName']); ?&gt; Quests&lt;/a&gt;&lt;/h3&gt;
                              &lt;p&gt;Offers the ability to see the available quests in &lt;? echo stripslashes($game['gameName']); ?&gt; along with information about them and a simple guide to go along with each.&lt;/p&gt;
  
                          &lt;/article&gt;
  
  
                          &lt;article class='grid_5 omega' style='float:right;'&gt;
  
                              &lt;h3&gt;&lt;a href='viewDB.php?gameID=&lt;? echo $game['id'] ?&gt;'&gt;View &lt;? echo stripslashes($game['gameName']); ?&gt; Database&lt;/a&gt;&lt;/h3&gt;
                              &lt;p&gt;Offers information about &lt;? echo stripslashes($game['gameName']); ?&gt; items, places, and characters. We try to be extensive with our information.&lt;/p&gt;
  
                          &lt;/article&gt;
  
  
                      &lt;/section&gt;
  
              &lt;?
                  }
              } else {
                  echo '&lt;h3&gt;Game ID is not set!&lt;/h3&gt;;'
                  exit();
              }
  
              ?&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  &lt;?
      $database = null;
      unset($database);
  ?&gt;
  </code></pre>
  
  <p>The database is initialized and connected to in an include provided by my globhead file:</p>
  
  <pre><code>$function = new srFunc();
  try {
      $database = new PDO('mysql:host=localhost;dbname=expunged;charset=utf8','expunged','expunged',array(
          //database attributes
          PDO::ATTR_EMULATE_PREPARES=&gt;false, //use real prepared statements
          PDO::ATTR_ERRMODE=&gt;PDO::ERRMODE_EXCEPTION, //set errors to kill application
          PDO::ATTR_PERSISTENT=&gt;true //keep the connection open indefinitely
          ));
  } catch (PDOException $e) {
      $function-&gt;logPDO($e-&gt;getMessage());
      exit();
  }
  </code></pre>
  
  <p>logPDO is provided by my functions class:</p>
  
  <pre><code>public function logPDO($err) {
      try {
          $filename = 'logPDO';
          $filehandle = fopen($filename, 'a');
          fwrite($filehandle,'[ '.date('Y-m-d Ga e').' ]  '.$err."\n\n");
          fclose($filehandle);
      } catch (exception $e) {
          return $e;
      }
  }
  </code></pre>
  
  <p>OK..so now we know how my code works -- in the uppermost code box at my foreach, it reports in my error log:</p>
  
  <pre><code>Invalid argument supplied for foreach
  </code></pre>
  
  <p>Now, changing the line just above (<code>$rows = $getGame-&gt;fetch(PDO::FETCH_ASSOC)</code>) to <code>$rows = $getGame-&gt;fetchAll(PDO::FETCH_ASSOC)</code> (adding All), <strong>nothing</strong> is reported. It just becomes a blank page with my styling and structure intact.</p>
  
  <p>I'm not sure what's going on, and I am very new to PDO, so I'm not sure how to troubleshoot this. </p>
  
  <p>I have tried wrapping this in a try,catch block but nothing was reported (nor logged to file using my log function), so I'm at a loss. Does anyone with more experience with PDO see anything that's wrong with my code?</p>