Reputation: 1561
I'll tried creating a little installation script for my modules.
When I call $_GET["install"]
the script is running. I looks like this:
public function install()
{
$this->sql->query("
INSERT
INTO cms_modules (title)
VALUES ('Guestbook')
");
$this->sql->query("
CREATE TABLE IF NOT EXISTS `cms_guestbook` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
");
}
It works pretty good. The table is installed and a new row is added to cms_modules
, although I would like that the INSERT is only activated if the cms_guestbook
table doesn't exists.
I searched for a answer and found this topic. I'll tried using that WHERE NOT EXISTS
by doing the following:
$this->sql->query("
INSERT
INTO cms_modules (title)
VALUES ('Guestbook')
WHERE NOT EXISTS (
SELECT 1 FROM cms_guestbook
)
");
But this didn't worked for my. The table is installed, but the row not inserted. I also tried using SHOW TABLES LIKE
but that also didn't seem to work.
Anyone suggestions or a solution?
Upvotes: 0
Views: 352
Reputation: 4828
You could do something like this:
if(!$this->sql->query("SHOW TABLES LIKE 'cms_guestbook'")){
$this->sql->query("
INSERT
INTO cms_modules (title)
VALUES ('Guestbook')
");
}
You query to see if it finds a specific table and if it returns false (check to be sure it returns a boolean) you run your other query.
Upvotes: 1