I'm running a script that returns me 250 values that will be the columns of a database. I need to create the database if it does not exist or simply truncate it and recreate all tables to update columns that will be added in the future. I tried to use this code but without success.
try {
$pdo = new PDO('mysql:host=host;dbname=banco', $userdB, $passdB);
foreach($res as $item){
$sql = 'CREATE TABLE imovel ( '.$item["field"].' VARCHAR(300) );';
$pdo->exec($sql);
}
$pdo = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
Just to ensure understanding, this foreach
returns me 250 values that will become columns of the database. Rodo, no error appears but also does not create the table with the columns that come from foreach
.