1

我的问题是,当我尝试通过 php 的 mysql_query 函数执行下面的查询时,它什么也没做(当然除了这个查询之外的所有工作)。如果我把 die(mysql_error()) 放在查询的最后,它只显示白页而不是一个错误。另一方面,如果我尝试直接从 mysql 客户端执行它,则 sql 代码可以工作。我不知道到底出了什么问题。这是sql代码。

-- Instructions:  
-- Set the NPC Entry and stats you want it to have below.  
SET  
@NPC_ENTRY := ".$entry.", -- This is your NPC's Entry  
@NPC_HEALTH := ".$health.", -- This is the health value you want your NPC to have.  
@NPC_MANA := ".$mana.", -- This is the mana value you want your NPC to have.  
@NPC_ARMOR := ".$armor."; -- This is the armor value you want your NPC to have.

-- DO NOT CHANGE ANYTHING BELOW, UNLESS YOU KNOW WHAT YOU ARE DOING.  
-- Getting NPC datas:  
SET  
@NPC_CLASS := (SELECT `unit_class` FROM creature_template WHERE Entry = @NPC_ENTRY),  
@NPC_LEVEL := ROUND(((SELECT `minlevel` FROM creature_template WHERE Entry = @NPC_ENTRY)+  (SELECT `maxlevel` FROM creature_template WHERE Entry = @NPC_ENTRY))/2, 0),  
@EXP := (SELECT `exp` FROM creature_template WHERE Entry = @NPC_ENTRY);

-- Getting base HP from a HP column defined by exp.  
SET  
@GET_HP_COL :=  
(SELECT CASE @EXP  
WHEN 0 THEN (SELECT basehp0 FROM creature_classlevelstats WHERE `level` = @NPC_LEVEL and `class` = @NPC_CLASS)  
WHEN 1 THEN (SELECT basehp1 FROM creature_classlevelstats WHERE `level` = @NPC_LEVEL and `class` = @NPC_CLASS)  
WHEN 2 THEN (SELECT basehp2 FROM creature_classlevelstats WHERE `level` = @NPC_LEVEL and `class` = @NPC_CLASS)  
END),  
-- Getting base mana  
@GET_MA_COL := (SELECT basemana FROM creature_classlevelstats WHERE `level` = @NPC_LEVEL and `class` = @NPC_CLASS),  
-- Getting base armor  
@GET_AR_COL := (SELECT basearmor FROM creature_classlevelstats WHERE `level` = @NPC_LEVEL and `class` = @NPC_CLASS);

-- Running the update with all the data collected:  
UPDATE creature_template SET Health_mod = (@NPC_HEALTH/@GET_HP_COL), Mana_mod = (@NPC_MANA/@GET_MA_COL), Armor_mod = (@NPC_ARMOR/@GET_AR_COL) WHERE Entry = @NPC_ENTRY;

如果我的帖子有错误,我深表歉意。英语不是我的母语。:(

4

1 回答 1

1

我怀疑问题可能在这里:

SET  
@NPC_ENTRY := ".$entry.", -- This is your NPC's Entry  
@NPC_HEALTH := ".$health.", -- This is the health value you want your NPC to have.  
@NPC_MANA := ".$mana.", -- This is the mana value you want your NPC to have.  
@NPC_ARMOR := ".$armor."; -- This is the armor value you want your NPC to have.

您发布了一个大型 MySQL 查询,我想您通过一次调用来调用它mysql_query(同样,当心 deprecation)。你是怎样做的?

SQL 应该类似于

@NPC_ENTRY := 11,  -- This is your NPC's Entry  
@NPC_HEALTH := 42, -- This is the health value you want your NPC to have.  
@NPC_MANA := 17, -- This is the mana value you want your NPC to have.  
@NPC_ARMOR := 33; -- This is the armor value you want your NPC to have.

如果是这样,你应该这样做:

$query1 = <<<SQL1
SET  
@NPC_ENTRY := {$entry}, -- This is your NPC's Entry  
@NPC_HEALTH := {$health}, -- This is the health value you want your NPC to have.  
@NPC_MANA := {$mana}, -- This is the mana value you want your NPC to have.  
@NPC_ARMOR := {$armor}; -- This is the armor value you want your NPC to have.
SQL1;

mysql_query($query1);

如果您只是将查询放在更多引号之间,您最终会得到像 0.42 这样的字段。而不是 42,这会破坏 SQL。

评论和多个查询也是如此:出于各种原因,一些后端不允许这样做(许多人认为一次只允许一个查询“更安全”,以避免某些类别的 SQL 攻击)。

由于这些原因,您可能希望将大型查询拆分为一组单个 SQL 语句并一个接一个地执行它们(可能在事务中)。您还可以将注释移至 PHP:

$sqls = array(
    'BEGIN WORK',
    // Set the data
    "SET @NPC_ENTRY := {$entry}," // This is your NPC's Entry
      ."@NPC_HEALTH := {$health},"  // This is the health value you want your NPC to have.  
      ."@NPC_MANA := {$mana},"      // This is the mana value you want your NPC to have.  
      ."@NPC_ARMOR := {$armor};",   // This is the armor value you want your NPC to have.
    // DO NOT TOUCH BELOW THIS
    "SET @NPC_CLASS := ...",
    ...
    "END WORK;"
);

/* Or also like this; it is clearer and almost as efficient.

$sqls = array(
    'BEGIN WORK',
    // Set the data
    "SET @NPC_ENTRY  := {$entry};",   // Your NPC's Entry
    "SET @NPC_HEALTH := {$health};",  // health value you want your NPC to have.  
    "SET @NPC_MANA := {$mana};",      // mana value   " "
    "SET @NPC_ARMOR := {$armor};",    // armor value  " "
    // DO NOT TOUCH BELOW THIS
    "SET @NPC_CLASS := ...;",
    ...
    "END WORK;"
);

*/


unset($error);
foreach($sqls as $sql) {
    if (!mysql_query($sql)) {
        $error = // get MySQL last error
        break;
    }
}
if (isset($error)) {
    ...do something...
}
于 2013-09-17T16:10:15.863 回答