Page 1 of 1

adding tables

Posted: Sat Feb 20, 2016 9:54 am
by karyoker
How can I add tables to database from array? db connect code not given.

Code: Select all

 
$arr = array("baker", "benton", "clackamas", "clatsop", "columbia", "coos", "crook", "curry", "deschutes", "douglas", "gilliam", "grant", "harney", "hoodriver", "jackson", "jeffeson", "joesphine", "klamath", "lake", "lincoln", "linn", "malheur", "marion", "morrow", "multnomah", "polk", "sherman", "tillamook", "umatilla", "union", "wallowa", "wasco", "washington", "wheeler", "yamhill");
reset($arr);



foreach ($arr as $value) {
    $sql = "CREATE TABLE $value (
id INT(1) UNSIGNED AUTO_INCREMENT PRIMARY KEY,

reg_date TIMESTAMP
)";
}

Re: adding tables

Posted: Sat Feb 20, 2016 12:54 pm
by Christopher
There are examples in the manual. Take look at PDO: http://php.net/manual/en/pdo.connections.php. Database code is two steps: 1) create a database connection which you would do before your loop, and 2) execute a query which in your case you would do inside the loop.

PS - As a general rule, it is considered a bad idea to execute queries inside a loop. That is because each query has connection overhead to talk to the database server. If you find a query in a loop then look for ways of doing the same thing with one query. In this case, it may be ok as this operation is only done at setup.

PPS - Why are you creating all those database instead of a single table with a key, an ID for the county and the timestamp? It looks like a bad design.