0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.