Reputation: 972
I have a database schema that I generate from each modules like this $schema
:
Array
(
[database] => Array
(
[0] => CREATE TABLE IF NOT EXISTS `test` (
`testid` VARCHAR(255) NOT NULL ,
`name` VARCHAR(45) NOT NULL ,
`type` VARCHAR(45) NOT NULL ,
`version` INT NULL ,
`info` TEXT NULL
PRIMARY KEY (`dirname`) )
ENGINE = InnoDB
DEFAULT CHARSET = utf8
)
)
Array
(
[database] => Array
(
[0] => CREATE TABLE IF NOT EXISTS `menu_test` (
`mid` VARCHAR(150) NOT NULL ,
`menu_test` VARCHAR(45) NOT NULL ,
`name` VARCHAR(150) NOT NULL ,
PRIMARY KEY (`mid`, `language`) )
ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8
)
[insert] => Array
(
[0] => Array
(
[table] => menu_test
[fields] => Array
(
[mid] => menu_admin
[language] => EN
[name] => Admin
)
)
[1] => Array
(
[table] => menu_test
[fields] => Array
(
[mid] => menu_seo
[language] => EN
[name] => Menu SEO
)
)
And so on...I needed to generate an *.sql file with this schema so I tried this but it only add the last element!
$database = DOCUMENT_ROOT."sites/user/database.sql" ;
foreach ($schema as $type => $query)
{
switch ($type)
{
case "database" :
foreach ($query as $sql)
file_put_contents($database,$sql.";");
break ;
case "insert" :
foreach ($query as $sql)
file_put_contents($database,$sql.";");
break ;
}
}
Many Thanks!
Upvotes: 0
Views: 567
Reputation: 943
If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.
file_put_contents($database,$sql.";",FILE_APPEND); should work.
Upvotes: 2