title: PHP PDO Check for multiple columns in mySQL table and create the ones that don't exist
body: <p>Looking for a way to check whether a table in a database has been assembled with the columns it required.</p>
  
  <p>I build this function:</p>
  
  <pre><code>function verifydbtable($table,$fields){
      global $fsdbh;
      foreach($fields as $field =&gt; $type){
          $verify = $fsdbh-&gt;prepare("ALTER TABLE `$table` ADD `$field` $type;");
          $verify-&gt;execute();
      }
      return 1;
  }
  </code></pre>
  
  <p>Where <code>$table</code> is the table to update and <code>$fields</code> contains an array of all the fields to check. <code>$type</code> contains the type of field eg. <code>VACHAR(20)</code>.</p>
  
  <p>The function works for each field until it comes across a field that already exists. Then it returns false.</p>
  
  <p><strong>The problem is that it then won't check the remaining columns from the array. How do we make it continue checking the remaining fields.</strong> </p>
  
  <p>Caused by the fact that if a column already exists it will return an error <code>#1060 - Duplicate column name 'subtitle'</code> which is what it should do. We then need to skip to the next item in the array if this error is returned.</p>
  
  <p>In layman's terms we need this but of course this is not a mySQL statement. </p>
  
  <pre><code>prepare("ALTER TABLE `$table` ADD IF NOT EXIST `$field` $type");
  </code></pre>