0
I’m running a script that returns 250 values that will be the columns of a database. I need to create the database if it doesn’t exist or simply truncate it and recreate all the 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
me returns 250 values that will become columns of the database. I run, no error appears but also does not create the table with the columns that comes from the foreach
.
Variables are not interpreted in single quotes. This will create 250 tables
imovel
, would not be to execute aALTER TABLE .... ADD COLUMN ...
– rray
I made the correction by concatenating the
field
. He creates the bank and puts the first column. And then to !!! I think theCREATE
within theforeach
doesn’t happen, but then how?– Marcos Vinicius
Run the first create, and then run
ALTER TABLE
in the items after this.– mutlei