简体   繁体   English

WHMCS,我怎么只能运行一次代码

[英]WHMCS, how can I run code only once

I have create some functions that are hooked on several WHMCS hooks. 我创建了一些挂在多个WHMCS挂钩上的函数。 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 ? 有什么办法或一个好主意如何做到这一点?

In my WHMCS module that I have created, I use the following logic: 在创建的WHMCS模块中,使用以下逻辑:

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. 我还通过一个循环运行-这使我可以添加新的列/表,然后重新运行安装脚本并添加任何尚不存在的列/表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM