我收到此代码的语法错误...是否可以在SQL查询中进行foreach
循环?
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
}
答案 0 :(得分:0)
在尝试后的第一行,声明变量:
$sql = "CREATE TABLE IF NOT EXISTS `".$tbl."`(";
错误可能就是:
$sql .= "ID INT( 11 ) AUTO_INCREMENT PRIMARY KEY,";
你需要完成statemant:
$sql .= "ID INT( 11 ) AUTO_INCREMENT PRIMARY KEY )";
我希望我有所帮助。