dtng5978 2014-10-16 16:46
浏览 25
已采纳

如果while循环中的语句没有更新数据库中的所有用户

Im trying to update all users in the database to get +5 turns every time this script is run.

But it updates only the first user in the database untill it has full turns and then it updates the next user.

What do i have to do to add 5 turns to all users but dont add any if they have 100/100 turns?

<?php

include ("functions/db_connect.php");

$get_users = mysqli_query($db, "SELECT * FROM `members`") or die(mysql_error());
while($user = mysqli_fetch_assoc($get_users)){

    if ($user['current_turns'] < $user['max_turns']) {
        $update = mysqli_query($db, "UPDATE `members` SET
                                `current_turns`=`current_turns`+'5' WHERE `id`='".$user['id']."'") or die(mysql_error());
        header("Location: users.php");
        exit();

    }
}

?>
  • 写回答

1条回答 默认 最新

  • du0923 2014-10-16 17:26
    关注

    Try using a query like this to update all the members at the same time instead of selecting them all and looping through updating them one by one.

    UPDATE members 
    SET current_turns = LEAST(max_turns, current_turns + 5)
    WHERE current_turns < max_turns
    

    Using the LEAST function will allow the current_turns to be incremented up to max_turns without going over, as I assume this would be undesirable, but it does depend on neither column being null.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错