title: Generate Create Table Script MySQL Dynamically with PHP
body: <p>I do not think that this has been posted before - as this is a very specific problem.</p>
  
  <p>I have a script that generates a "create table" script with a custom number of columns with custom types and names.</p>
  
  <p>Here is a sample that should give you enough to work from -</p>
  
  <pre><code>$cols = array();
  $count = 1;
  $numcols = $_POST['cols'];
  while ($numcols &gt; 0) {
  
      $cols[] = mysql_real_escape_string($_POST[$count."_name"])." ".mysql_real_escape_string($_POST[$count."_type"]);
      $count ++;
      $numcols --;
  }
  $allcols = null;
  $newcounter = $_POST['cols'];
  foreach ($cols as $col) { 
      if ($newcounter &gt; 1)
          $allcols = $allcols.$col.",\n";
      else
          $allcols = $allcols.$col."\n";
      $newcounter --;
  };
  $fullname = $_SESSION['user_id']."_".mysql_real_escape_string($_POST['name']);
  $dbname = mysql_real_escape_string($_POST['name']);
  $query = "CREATE TABLE ".$fullname." (\n".$allcols." )";
  mysql_query($query);
  echo create_table($query, $fullname, $dbname, $actualcols);
  </code></pre>
  
  <p>But for some reason, when I run this query, it returns a syntax error in MySQL. This is probably to do with line breaks, but I can't figure it out. HELP!</p>