我做错了什么或者php不支持这个吗?

时间:2013-09-17 16:00:17

标签: php mysql sql

我的问题是,当我尝试通过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;

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

1 个答案:

答案 0 :(得分: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即可调用它(同样,谨防弃用)。你是怎么做到的?

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);

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

同样适用于评论和多个查询:由于各种原因(许多人认为一次只允许一个查询“更安全”,以避免某些类型的SQL攻击),一些后端不允许这样做。 / p>

由于这些原因,您可能希望将大型查询拆分为单个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...
}