简体   繁体   中英

Update rows of a SQL table

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.

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 :(

<?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

Try this.. you are not executing the sql statements

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

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM