更新SQL表的行

时间:2022-10-06 13:11:25

I have a table with a column called 'status'. The defaut value of 'status' is 0. I want to update the value to '1' after using it. I basically want to check if the status is 0, if it is, do an operation and then change the value to 1.

我有一个名为'status'的列表。 'status'的defaut值为0.我想在使用后将值更新为'1'。我基本上想检查状态是否为0,如果是,则执行操作然后将值更改为1。

Here is the code. All works perfectly except that the value of 0 is not changed to 1. I am a novice so maybe is a very basic mistake :(

这是代码。所有的工作都完美,除了0的值不改为1.我是新手,所以也许是一个非常基本的错误:(

<?php

$sql = "SELECT number, status FROM summonerid";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

     // output data of each row
     while($row = $result->fetch_assoc()) {
        $SummonerID = $row["number"];
        $status = $row["status"];

        if($status=='0'){
             $recentgames=$lol->getRecentGames($SummonerID);
             $MatchID1=$recentgames->games[0]->gameId;
             $sql = "INSERT INTO matchid (number) SELECT * FROM (SELECT '$MatchID1') AS tmp WHERE NOT EXISTS (SELECT number FROM matchid WHERE number = '$MatchID1') LIMIT 1;";

             $sql = "UPDATE summonerid SET status='1' WHERE status='0';"; // THIS IS THE PART THAT DOES NOT WORK WELL
        }
    }
}

?>

Any help would be highly appreciated

任何帮助将受到高度赞赏

1 个解决方案

#1


Try this.. you are not executing the sql statements

试试这个..你没有执行sql语句

$sql = "SELECT number, status FROM summonerid";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

 // output data of each row
 while($row = $result->fetch_assoc()) {
    $SummonerID = $row["number"];
    $status = $row["status"];

    if($status=='0'){
         $recentgames=$lol->getRecentGames($SummonerID);
         $MatchID1=$recentgames->games[0]->gameId;
         $sql = "INSERT INTO matchid (number) SELECT * FROM (SELECT '$MatchID1') AS tmp WHERE NOT EXISTS (SELECT number FROM matchid WHERE number = '$MatchID1') LIMIT 1;";

          $result1 = $conn->query($sql);
         $sql = "UPDATE summonerid SET status='1' WHERE status='0';"; 
          $result1 = $conn->query($sql); 
    }
}
}

?>

#1


Try this.. you are not executing the sql statements

试试这个..你没有执行sql语句

$sql = "SELECT number, status FROM summonerid";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

 // output data of each row
 while($row = $result->fetch_assoc()) {
    $SummonerID = $row["number"];
    $status = $row["status"];

    if($status=='0'){
         $recentgames=$lol->getRecentGames($SummonerID);
         $MatchID1=$recentgames->games[0]->gameId;
         $sql = "INSERT INTO matchid (number) SELECT * FROM (SELECT '$MatchID1') AS tmp WHERE NOT EXISTS (SELECT number FROM matchid WHERE number = '$MatchID1') LIMIT 1;";

          $result1 = $conn->query($sql);
         $sql = "UPDATE summonerid SET status='1' WHERE status='0';"; 
          $result1 = $conn->query($sql); 
    }
}
}

?>