我创建了一些与几个WHMCS挂钩的函数。我的一些函数需要在数据库中为我的功能创建一个新表。
我喜欢做的是通过编程方式将表创建到Database中,但是只运行一次生成表的代码,并且永远不会再运行该代码。
有什么办法或者好主意吗?
在我创建的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
}
然后使用以下命令创建数据库:
$createTable[] = 'CREATE TABLE IF NOT EXISTS `modulename_table` ( `id` int(11) NOT NULL auto_increment, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;';
我有很多这样的东西,然后通过一个循环来运行:
$createTableCount = 0;
for($s=0,$e=count($createTable); $s<$e; $s++){
$query = mysql_query($createTable[$s]);
if($query)
$createTableCount++;
}
然后将每一列添加到表中:
$alterTable[] = 'ALTER TABLE `modulename_table` ADD `something` int(11) NOT NULL;';
,我也通过循环运行-这允许我添加新的列/表和重新运行安装脚本,并添加任何不存在的列/表。