adding tables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
karyoker
Forum Newbie
Posts: 3
Joined: Thu Feb 18, 2016 6:42 pm

adding tables

Post 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
)";
}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: adding tables

Post 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.
(#10850)
Post Reply