一段时间内获取MYSQLI新查询

时间:2013-01-22 23:51:25

标签: php mysqli while-loop sync

我现在正在制作一些有趣的网站,但我已经陷入了困境。

我想执行一个组任务,该组的任何成员都可以启动它。 该集团的所有人都将获得相同的黄金,经验 并分享同样的冷却时间。 我的数据库中有3个表(我只会显示重要信息)

Members:
username, level, experience, playergold

Levels:
level, required_experience

Groups:
leader, member_1, member_2, member_3, last_quest_started, quest_cooldown

Quests:
success_message, failed_message, chance, minimum_experience, maximum_experience, minimum_gold, maximum_gold, cooldown

我想在群组中更新last_quest_started和quest_cooldown,我想更新每个成员他/她的等级,经验,玩家金币

因此,在获得组成员的每个用户名后,任务数据,计算经验和金币。我用它来更新:

if($select_members_info_stmt = $mysqli->prepare("SELECT members.username, members.level, members.experience, members.playergold, levels.required_experience FROM members INNER JOIN levels ON members.level = levels.level WHERE ((members.username = ?) OR (members.username = ?) OR (members.username = ?) OR(members.username = ?))"))
{
    $select_members_info_stmt->bind_param('ssss', $leader, $member_1, $member_2, $member_3);
    $select_members_info_stmt->execute();
    $select_members_info_stmt->bind_result($selected_username, $level, $experience, $playergold, $required_experience);
    while($select_members_info_stmt->fetch())
    {
        $now = time();

        if($update_user_stats_stmt = $mysqli->prepare("UPDATE members SET level = ?, experience = ?, playergold = ? WHERE username = ?"))
        {
            $update_user_stats_stmt->bind_param('iiiiis', $new_level, $new_experience, $new_gold, $now, $cooldown, $selected_username);
            $update_user_stats_stmt->execute();
            if($update_user_stats_stmt->affected_rows == 0)
            {
                    echo '<div>Because of a system error it is impossible to perform a task, we apologize for this inconvience. Try again later.</div>';
            }
            $update_user_stats_stmt->close();
        }
        else                                
        {
            printf("Update user stats error: %s<br />", $mysqli->error);
        }
    }
    $select_members_info_stmt->close();
    echo '<div>'.$success_message.'</div><br />';
}
else
{
    printf("Select members info error: %s<br />", $mysqli_error);
}

但我一直在接受:

更新用户统计信息错误:命令不同步;你现在不能运行这个命令(4次,这是我的团队满员时的大小。)

我无法找到解决不同步错误的解决方案,因为我无法关闭$ select_members_info_stmt,因为它会停止提取。

请帮帮我,因为我真的不知道该怎么做。

2 个答案:

答案 0 :(得分:6)

您无法像这样嵌套execute()

最好的解决方案是将成员列表折腾成array()一次,关闭连接,然后迭代该数组并更新每条记录。

它应该是这样的:

$select_members_info_stmt->bind_param('ssss', $leader, $member_1, $member_2, $member_3);
$select_members_info_stmt->execute();
$select_members_info_stmt->bind_result($selected_username, $level, $experience, $playergold, $required_experience);

$members = array();
while($select_members_info_stmt->fetch())
{
    // tossing into the array
    $members[] = array(
        'selected_username' =>$selected_username, 
        'level' => $level, 
        'experience' => $experience, 
        'playergold' => $playergold, 
        'required_experience' => $required_experience
    );
}
$select_members_info_stmt->close();

// Now iterate through the array and update the user stats
foreach ($members as $m) {
    if($update_user_stats_stmt = $mysqli->prepare("UPDATE members SET level = ?, experience = ?, playergold = ? WHERE username = ?"))
    {
        // Note that you need to use $m['selected_username'] here. 
        $update_user_stats_stmt->bind_param('iiiiis', $new_level, $new_experience, $new_gold, $now, $cooldown, $m['selected_username']);
        $update_user_stats_stmt->execute();
        if($update_user_stats_stmt->affected_rows == 0)
        {
                echo '<div>Because of a system error it is impossible to perform a task, we apologize for this inconvience. Try again later.</div>';
        }
        $update_user_stats_stmt->close();
    }
    else                                
    {
        printf("Update user stats error: %s<br />", $mysqli->error);
    }

}

答案 1 :(得分:3)

您无法在与mysql相同的连接上嵌套主动运行预准备语句。一旦在任何语句上调用execute(),就不能在同一连接上运行另一个语句,直到该预准备语句关闭为止。一旦你开始在第二个准备语句上执行,第一个预准备语句上的任何提取都将失败。

每个连接只能在mysql服务器上准备并运行一个'live'语句

如果你真的需要嵌套准备好的语句,你可以建立两个独立的mysqli连接。