-2

I'm getting Syntax error for this code... is it possible to have foreach loop inside SQL query?

try {
    $sql .= "CREATE TABLE IF NOT EXISTS `".$tbl."`(";

    foreach($columns as $column){
        $sql .= "`".$column."` VARCHAR( 250 ) NOT NULL,";
}

$sql .= "ID INT( 11 ) AUTO_INCREMENT PRIMARY KEY,";

$DB->exec($sql);
print("Created $tbl Table.\n");

} catch(PDOException $e) {
    echo $e->getMessage();//Remove or change message in production code
}
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Dev
  • 17
  • 5
  • Possible duplicate of [PHP Parse/Syntax Errors; and How to solve them?](http://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – u_mulder Dec 09 '16 at 17:41
  • People __can't__ guess error descriptions here. – u_mulder Dec 09 '16 at 17:41

1 Answers1

0

On the first line after try, declare the variable:

 $sql = "CREATE TABLE IF NOT EXISTS `".$tbl."`(";

and the error probably it's:

$sql .= "ID INT( 11 ) AUTO_INCREMENT PRIMARY KEY,";

You need finish the statemant:

$sql .= "ID INT( 11 ) AUTO_INCREMENT PRIMARY KEY )";

I hope I have helped.

Andre Rodrigues
  • 253
  • 3
  • 6