title: PHP PDO, excluding row with null field value
body: <p>So I am using this sample code <code>output6.php</code></p>
  
  <p>What I would like to do is if <code>empire_name</code> returns null, skip the associated row
  Any ideas or suggestions would be appreciated
  Thanks!</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body&gt;
  
  &lt;?php
  echo "&lt;table style='border: solid 1px black;'&gt;";
    echo "&lt;tr&gt;
      &lt;th&gt;Empire Name&lt;/th&gt;
      &lt;th&gt;Win?&lt;/th&gt;
      &lt;th&gt;Building 1&lt;/th&gt;
      &lt;th&gt;Building 2&lt;/th&gt;
      &lt;th&gt;Building 3&lt;/th&gt;
      &lt;th&gt;Building 4&lt;/th&gt;
      &lt;th&gt;Building 5&lt;/th&gt;
      &lt;th&gt;Building 6&lt;/th&gt;
      &lt;th&gt;Building 7&lt;/th&gt;
      &lt;th&gt;Building 8&lt;/th&gt;
      &lt;th&gt;Building 9&lt;/th&gt;
      &lt;/tr&gt;";
  
  class TableRows extends RecursiveIteratorIterator { 
       function __construct($it) { 
           parent::__construct($it, self::LEAVES_ONLY); 
       }
  
       function current() {
           return "&lt;td style='width: 150px; border: 1px solid black;'&gt;" . parent::current(). "&lt;/td&gt;";
       }
  
       function beginChildren() { 
           echo "&lt;tr&gt;"; 
       } 
  
       function endChildren() { 
           echo "&lt;/tr&gt;" . "\n";
       } 
  } 
  
  $servername = "xxx";
  $username = "xxx";
  $password = "xxx";
  $dbname = "xxx";
  
  try {
       $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
       $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
       $stmt = $conn-&gt;prepare("SELECT empire_name, win, building_1, building_2, building_3, building_4, building_5, building_6, building_7, building_8, building_9 FROM ft_form_2 "); 
       $stmt-&gt;execute();
  
       // set the resulting array to associative
       $result = $stmt-&gt;setFetchMode(PDO::FETCH_ASSOC); 
  
       foreach(new TableRows(new RecursiveArrayIterator($stmt-&gt;fetchAll())) as $k=&gt;$v) { 
           echo $v;
       }
  }
  catch(PDOException $e) {
       echo "Error: " . $e-&gt;getMessage();
  }
  $conn = null;
  echo "&lt;/table&gt;";
  ?&gt;  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I tried looking this up but I don't know enough to know what to look for</p>
  
  <p>thanks!</p>
  
  <p>my current results <a href="https://i.stack.imgur.com/byVxx.png" rel="nofollow noreferrer">null = blank rows, image link</a></p>
  
  <p>I think this is close...
  <a href="https://stackoverflow.com/questions/12462291/pdo-and-is-not-null-function">PDO and IS NOT NULL Function</a>
  But I am not sure how to implement in the current code (I tried a few variations)</p>
  
  <p>A different variation - same results (output3.php) closer to the desired end result</p>
  
  <pre><code>&lt;?php
  define("DB_HOST", "xxx");    // Using Constants
  define("DB_USER", "xxx");
  define("DB_PASS", "xxx");
  define("DB_NAME", "xxx");
  
  try {       // &lt;&lt; using Try/Catch() to catch errors!
  
  $dbc = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset-utf8",DB_USER,DB_PASS);
  }catch(PDOException $e){ echo $e-&gt;getMessage();}
  
  if($dbc &lt;&gt; true){
      die("&lt;p&gt;There was an error&lt;/p&gt;");
  }
  
  $print = ""; // assign an empty string 
  
  $stmt = $dbc-&gt;query("SELECT * FROM ft_form_2"); // fetch data
  $stmt-&gt;setFetchMode(PDO::FETCH_OBJ);
  
  if($stmt-&gt;execute() &lt;&gt; 0)
  {
  
      $print .= '&lt;table border="1px"&gt;';
      $print .= '&lt;tr&gt;&lt;th&gt;Empire_Name&lt;/th&gt;';
      $print .= '&lt;th&gt;Win&lt;/th&gt;';
      $print .= '&lt;th&gt;Building_1&lt;/th&gt;';
      $print .= '&lt;th&gt;Building_2&lt;/th&gt;';
      $print .= '&lt;th&gt;Building_3&lt;/th&gt;';
      $print .= '&lt;th&gt;Building_4&lt;/th&gt;';
      $print .= '&lt;th&gt;Building_5&lt;/th&gt;';
      $print .= '&lt;th&gt;Building_6&lt;/th&gt;';
      $print .= '&lt;th&gt;Building_7&lt;/th&gt;';
      $print .= '&lt;th&gt;Building_8&lt;/th&gt;';
      $print .= '&lt;th&gt;Building_9&lt;/th&gt; &lt;/tr&gt;';
  
      while($ft_form_2 = $stmt-&gt;fetch()) // loop and display data
      {
  
          $print .= '&lt;tr&gt;';
          $print .= "&lt;td&gt;{$ft_form_2-&gt;empire_name}&lt;/td&gt;";
          $print .= "&lt;td&gt;{$ft_form_2-&gt;win}&lt;/td&gt;";
          $print .= "&lt;td&gt;{$ft_form_2-&gt;building_1} &lt;br&gt; {$ft_form_2-&gt;building_1_notes} &lt;/td&gt;";
          $print .= "&lt;td&gt;{$ft_form_2-&gt;building_2} &lt;br&gt; {$ft_form_2-&gt;building_2_notes} &lt;/td&gt;";
          $print .= "&lt;td&gt;{$ft_form_2-&gt;building_3} &lt;br&gt; {$ft_form_2-&gt;building_3_notes} &lt;/td&gt;";
          $print .= "&lt;td&gt;{$ft_form_2-&gt;building_4} &lt;br&gt; {$ft_form_2-&gt;building_4_notes} &lt;/td&gt;";
          $print .= "&lt;td&gt;{$ft_form_2-&gt;building_5} &lt;br&gt; {$ft_form_2-&gt;building_5_notes} &lt;/td&gt;";
          $print .= "&lt;td&gt;{$ft_form_2-&gt;building_6} &lt;br&gt; {$ft_form_2-&gt;building_6_notes} &lt;/td&gt;";
          $print .= "&lt;td&gt;{$ft_form_2-&gt;building_7} &lt;br&gt; {$ft_form_2-&gt;building_7_notes} &lt;/td&gt;";
          $print .= "&lt;td&gt;{$ft_form_2-&gt;building_8} &lt;br&gt; {$ft_form_2-&gt;building_8_notes} &lt;/td&gt;";
          $print .= "&lt;td&gt;{$ft_form_2-&gt;building_9} &lt;br&gt; {$ft_form_2-&gt;building_9_notes} &lt;/td&gt;";
          $print .= '&lt;/tr&gt;';
      }
  
      $print .= "&lt;/table&gt;";
      echo $print;
  }
  ?&gt;  
  </code></pre>