//lets get the auto name set
$accepted = 0;
$sql = mysql_query("SELECT * FROM ".TBL_FACTIONS." WHERE f_name='no_name'");
$numrows = mysql_num_rows($sql);
//default name 1
if($numrows == 0){ $auto_name = 'no_name'; $accepted = 1;}
//default name 2
if ($accepted == 0){ $sql = mysql_query("SELECT * FROM ".TBL_FACTIONS."
WHERE f_name='no_name[02]'"); $numrows = mysql_num_rows($sql);
if($numrows == 0){$auto_name = 'no_name[02]'; $accepted = 1;} }
//default name 3
if ($accepted == 0){ $sql = mysql_query("SELECT * FROM ".TBL_FACTIONS."
WHERE f_name='no_name[03]'"); $numrows = mysql_num_rows($sql);
if($numrows == 0){$auto_name = 'no_name[03]'; $accepted = 1;} }
//default name 4
if ($accepted == 0){ $sql = mysql_query("SELECT * FROM ".TBL_FACTIONS."
WHERE f_name='no_name[04]'"); $numrows = mysql_num_rows($sql);
if($numrows == 0){$auto_name = 'no_name[04]'; $accepted = 1;} }
//default name 5
if ($accepted == 0){ $sql = mysql_query("SELECT * FROM ".TBL_FACTIONS."
WHERE f_name='no_name[05]'"); $numrows = mysql_num_rows($sql);
if($numrows == 0){$auto_name = 'no_name[05]'; $accepted = 1;} }
//default name 6
if ($accepted == 0){ $sql = mysql_query("SELECT * FROM ".TBL_FACTIONS."
WHERE f_name='no_name[06]'"); $numrows = mysql_num_rows($sql);
if($numrows == 0){$auto_name = 'no_name[06]'; $accepted = 1;} }
//-->update faction table
mysql_query("INSERT INTO ".TBL_FACTIONS." (f_id, f_name, f_status,
f_boss, f_uboss, f_rhm, f_lhm, f_r1, f_r2, f_nat,
f_funds, f_fmj, f_score,f_footer)
VALUES ('','$auto_name','0','$username','','','','','','gb','0','0','0','')");
这会检查我的MySQL数据库,为表中创建的行创建一个自动名称。 它工作正常,但我认为必须有一种更简单,更有效的方法来实现相同的输出?
另外,如何添加常量循环以便始终创建no_name [XX]插入?
答案 0 :(得分:1)
嗯,你似乎是在迭代地添加它们......所以你不能这样做:
$sql = mysql_query("SELECT * FROM ".TBL_FACTIONS." WHERE f_name='no_name' OR f_name='no_name[02]' OR f_name='no_name[03]' OR f_name='no_name[04]' OR f_name='no_name[05]' OR f_name='no_name[06]'");
然后再做
$num_rows = mysql_num_rows(mysql_query($sql));
然后:
switch($num_rows){
case 0:
$auto_name = 'no_name';
break;
case 1:
$auto_name = 'no_name[02]';
break;
case 2:
$auto_name = 'no_name[03]';
break;
case 3:
$auto_name = 'no_name[04]';
break;
case 4:
$auto_name = 'no_name[05]';
break;
case 5:
$auto_name = 'no_name[06]';
break;
}
mysql_query("INSERT INTO ".TBL_FACTIONS." (f_id, f_name, f_status, f_boss, f_uboss, f_rhm,
f_lhm, f_r1, f_r2, f_nat, f_funds, f_fmj, f_score,f_footer)
VALUES ('','$auto_name','0','$username','','','','','','gb','0','0','0','')");
另外,对于常量循环的事情,你要问的是你只需要像上面那样做但没有开关循环...带有一个while循环......所以:
$result = mysql_query("SELECT * FROM ".TBL_FACTIONS." WHERE f_name='no_name'");
$i = 0;
while(mysql_num_rows($result)){
$i++;
$result = mysql_query("SELECT * FROM ".TBL_FACTIONS." WHERE f_name='no_name[".$i."]'");
}
// $i should be the next number to add now...
if($i > 0){
$sql = "INSERT INTO ".TBL_FACTIONS." (f_id, f_name, f_status, f_boss, f_uboss, f_rhm,
f_lhm, f_r1, f_r2, f_nat, f_funds, f_fmj, f_score,f_footer)
VALUES ('','no_name[$i]','0','$username','','','','','','gb','0','0','0','')";
}
else {
$sql = "INSERT INTO ".TBL_FACTIONS." (f_id, f_name, f_status, f_boss, f_uboss, f_rhm,
f_lhm, f_r1, f_r2, f_nat, f_funds, f_fmj, f_score,f_footer)
VALUES ('','no_name','0','$username','','','','','','gb','0','0','0','')";
}
答案 1 :(得分:1)
首先,您可以删除接受的检查,将其替换为您用于设置值的反向检查,并将sql语句中使用的值替换为if($numrows == 0)
之后设置的变量
请记住,再次设置变量将覆盖之前的值。
另外,我在第一个陈述中加上if(TRUE)
条件。
//lets get the auto name set
if (TRUE) {
$auto_name = 'no_name';
$sql = mysql_query("SELECT * FROM ".TBL_FACTIONS." WHERE f_name='".$auto_name."'");
$numrows = mysql_num_rows($sql);
}
//default name 2
if ($numrows != 0) {
$auto_name = 'no_name[02]';
$sql = mysql_query("SELECT * FROM ".TBL_FACTIONS." WHERE f_name='".$auto_name."'");
$numrows = mysql_num_rows($sql);
}
//default name 3
if ($numrows != 0) {
$auto_name = 'no_name[03]';
$sql = mysql_query("SELECT * FROM ".TBL_FACTIONS." WHERE f_name='".$auto_name."'");
$numrows = mysql_num_rows($sql);
}
//default name 4
if ($numrows != 0) {
$auto_name = 'no_name[04]';
$sql = mysql_query("SELECT * FROM ".TBL_FACTIONS." WHERE f_name='".$auto_name."'");
$numrows = mysql_num_rows($sql);
}
//default name 5
if ($numrows != 0) {
$auto_name = 'no_name[05]';
$sql = mysql_query("SELECT * FROM ".TBL_FACTIONS." WHERE f_name='".$auto_name."'");
$numrows = mysql_num_rows($sql);
}
//default name 6
if ($numrows != 0) {
$auto_name = 'no_name[06]';
$sql = mysql_query("SELECT * FROM ".TBL_FACTIONS." WHERE f_name='".$auto_name."'");
$numrows = mysql_num_rows($sql);
}
//-->update faction table
mysql_query("INSERT INTO ".TBL_FACTIONS." (f_id, f_name, f_status, f_boss, f_uboss, f_rhm, f_lhm, f_r1, f_r2, f_nat, f_funds, f_fmj, f_score,f_footer) VALUES ('','$auto_name','0','$username','','','','','','gb','0','0','0','')");
现在您可能会注意到每个块除了$auto_name = 'no_name';
行之外都有相同的代码,这些行仅在数字区域有所不同,并且第一个块始终运行,这使得它非常适合{{1} }循环。
do{}while()
(请注意,此代码存在细微差别,它使用$ noname [X]表示条目0-9,$ noname [XX]表示条目10-99和$ noname [XXX]表示条目100-999等等,如果您想要这个不同,则需要更改$numeric_part = ''; // numeric part is empty for the first time
$i = 1 // which name are we're checking
do {
$auto_name = 'no_name'.$numeric_part;
$sql = mysql_query("SELECT * FROM ".TBL_FACTIONS." WHERE f_name='".$auto_name."'");
$numrows = mysql_num_rows($sql);
//prepare for the next round trough the loop
$i++;
$numeric_part = '['.$i.']';
} while ($numrows != 0);
//-->update faction table
mysql_query("INSERT INTO ".TBL_FACTIONS." (f_id, f_name, f_status, f_boss, f_uboss, f_rhm, f_lhm, f_r1, f_r2, f_nat, f_funds, f_fmj, f_score,f_footer) VALUES ('','$auto_name','0','$username','','','','','','gb','0','0','0','')");
行
这仍然不是非常有效,因为这仍然会使您的数据库总共击中5000次,仅输入100个条目,并且总共输入500,000(50万)次输入1000(1K)个条目,最后一次查询命中数据库1000(1K)次。
(您的托管/计算机 不会喜欢 )
一种更有效的方法是将条目简单地添加到最后,并忽略它们之间的空条目(您现在正在重复使用它。)
通过允许$numeric_part = '['.$i.']';
字段的NULL条目,并使用基于f_name
字段的值替换输出中的空条目,可以完全忽略中间的空条目。
(我将把最后一部分作为练习编写。)
答案 2 :(得分:0)
我看到你想要找到可用的最大no_name,
如果是这样,可以在此字段上执行SUBSTRING以查找此最大值,并插入表max+1
类似的东西:
SELECT
MAX(SUBSTRING_INDEX(SUBSTRING(f_name,8) , ']',1 ) ) AS max_id
FROM ".TBL_FACTIONS."
答案 3 :(得分:-1)
使用循环。任何时候有许多不同的东西都只用不同的索引做同样的事情,使用一个循环。这样,您就不必添加更多代码,因为会添加更多选项。
$default_names = 5; // set this dynamically if needed
$accepted = 0;
$sql = mysql_query("SELECT * FROM ".TBL_FACTIONS." WHERE f_name='no_name'");
$numrows = mysql_num_rows($sql);
if($numrows == 0) {
$auto_name = 'no_name'; $accepted = 1;
}
else
{
for ($i = 0; $i < $default_names ; $i++)
{
$sql = mysql_query("SELECT * FROM ".TBL_FACTIONS." WHERE f_name='no_name['0'.$i + 2.']'");
$numrows = mysql_num_rows($sql);
if($numrows == 0) {
$auto_name = "no_name['0'.$i + 2.']";
$accepted = 1;
break;
}
}
}
mysql_query("INSERT INTO ".TBL_FACTIONS." (f_id, f_name, f_status, f_boss, f_uboss, f_rhm,
f_lhm, f_r1, f_r2, f_nat, f_funds, f_fmj, f_score,f_footer)
VALUES ('','$auto_name','0','$username','','','','','','gb','0','0','0','')");