php - WHMCS, how can I run code only once

606

I have create some functions that are hooked on several WHMCS hooks. Some of my functions are require to be a new table in database that I create for my functionality.

What I like to do is to programmatically create the table into Database, but run the code that generate the table only one time and never run that code again.

Is there any way or a good idea on how to do that ?

658

Answer

Solution:

In my WHMCS module that I have created, I use the following logic:

if(!mysql_num_rows(mysql_query("SHOW TABLES LIKE 'modulename_%'"))) {

    // there are no tables in the database for our module so we need to create them

}

I then use the following to create the database:

$createTable[] = 'CREATE TABLE IF NOT EXISTS `modulename_table` ( `id` int(11) NOT NULL auto_increment, PRIMARY KEY  (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;';

I have many of these which I then run through a loop:

$createTableCount = 0;
for($s=0,$e=count($createTable); $s<$e; $s++){
       $query = mysql_query($createTable[$s]);
       if($query)
            $createTableCount++;
}

I then add each column to the table:

$alterTable[] = 'ALTER TABLE `modulename_table` ADD `something` int(11) NOT NULL;';

which I also run through a loop - this allows me to add new columns/tables and re-run the installation script and add any columns/tables that don't already exist.

People are also looking for solutions to the problem: regex - PHP, regexp and parsing game logs

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.