为什么我的变量没有在php mysqli for-loop中更新?

时间:2016-05-13 00:22:20

标签: php mysql loops mysqli

我正在尝试将一个表(10k +行)排序到两个单独的表中,但似乎遇到了一个我无法弄清楚的无意义错误。当我注释掉mysqli插入语句时,变量正确打印出来并按原样更新。即使循环遍历表中的所有值,我也可以使用正确的键和值查看所有数据。

当我添加insert语句并将其放在mysqli_query中时,我突然收到大量的mysqli错误,说突然出现重复的主键,但是按照之前的测试,没有重复。其他时候,似乎只循环一次然后停止(或循环正确的次数,但每次都不做任何事情。我打印出了mysql语句,并且在尝试查询之前能够看到它们都是正确的,并且然而它在实际运行时仍会引起问题。整个代码包含在下面,但我无法弄清楚问题对我来说是什么。

$sql = "SELECT * FROM originalTable ORDER BY port LIMIT 20";
$result = mysqli_query($conn, $sql);
$num_Rows = mysqli_num_rows($result);
echo $num_Rows."<br/>";

// gets data for each row in the table

for($i=0; $i<$num_Rows; $i++){
    $row = mysqli_fetch_assoc($result);
    $ID = $row["ID"];
    echo $ID."<br/>";
    $IP = $row["IP"];
    echo $IP."<br/>";
    $port = $row["port"];
    echo $port."<br/>";
    $running = $row["running"];
    echo $running."<br/>";
    $afk = $row["afk"];
    echo $afk."<br/>";
    $gamemode = $row["gamemode"];
    echo $gamemode."<br/>";
    $maxplayers = $row["maxplayers"];
    echo $maxplayers."<br/>";
    $spawnprotection = $row["spawnprotection"];
    echo $spawnprotection."<br/>";
    $whitelist = $row["whitelist"];
    echo $whitelist."<br/>";
    $enablequery = $row["enablequery"];
    echo $enablequery."<br/>";
    $enablercon = $row["enablercon"];
    echo $enablercon."<br/>";
    $rconpassword = $row["rconpassword"];
    echo $rconpassword."<br/>";
    $motd = $row["motd"];
    echo $motd."<br/>";
    $announceachieve = $row["announceplayerachievements"];
    $allowflight = $row["allowflight"];
    $spawnanimals = $row["spawnanimals"];
    $spawnmobs = $row["spawnmobs"];
    $forcegamemode = $row["forcegamemode"];
    $hardcore = $row["hardcore"];
    $pvp = $row["pvp"];
    $difficulty = $row["difficulty"];
    $generatorsettings = $row["generatorsettings"];
    $levelname = $row["levelname"];
    $levelseed = $row["levelseed"];
    $leveltype = $row["leveltype"];
    $autosave = $row["autosave"];

    if($IP == $server1){
        $server = "server1table";
        $sql1 = "INSERT INTO server1table (id, ip, port, running, afk, gamemode, maxplayers, spawnprotection,
              whitelist, enablequery, enablercon, rconpassword, motd, announceplayerachievements,
              allowflight, spawnanimals, spawnmobs, forcegamemode, hardcore, pvp, difficulty,
              generatorsettings, levelname, levelseed, leveltype, autosave) VALUES ('$ID', '$IP',
              '$port', '$running', '$afk', '$gamemode', '$maxplayers', '$spawnprotection', '$whitelist',
              '$enablequery', '$enablercon', '$rconpassword', '$motd', '$announceachieve', '$allowflight', '$spawnanimals',
              '$spawnmobs', '$forcegamemode', '$hardcore', '$pvp', '$difficulty', '$generatorsettings', '$levelname',
              '$levelseed', '$leveltype', '$autosave')";
        echo $sql1;
        $result = mysqli_query($conn, $sql1);
        echo "Server 1<br/>";
        if($result){
            echo "Success?";
        }
        else{
            echo "Failure = " . $conn->error;
        }
    } else if ($IP == $Server2){
        $server = "server2table";
        $sql2 = "INSERT INTO server2table (id, ip, port, running, afk, gamemode, maxplayers, spawnprotection,
              whitelist, enablequery, enablercon, rconpassword, motd, announceplayerachievements,
              allowflight, spawnanimals, spawnmobs, forcegamemode, hardcore, pvp, difficulty,
              generatorsettings, levelname, levelseed, leveltype, autosave) VALUES ('$ID', '$IP',
              '$port', '$running', '$afk', '$gamemode', '$maxplayers', '$spawnprotection', '$whitelist',
              '$enablequery', '$enablercon', '$rconpassword', '$motd', '$announceachieve', '$allowflight', '$spawnanimals',
              '$spawnmobs', '$forcegamemode', '$hardcore', '$pvp', '$difficulty', '$generatorsettings', '$levelname',
              '$levelseed', '$leveltype', '$autosave')";
        echo $sql2;
        $result = mysqli_query($conn, $sql2);
        echo "Server 2<br/>";
        if($result){
            echo "Success?";
        }
        else{
            echo "Failure = " . $conn->error;
        }
    }

编辑:上表中的ID不是表ID字段或主键。它代表播放器的ID。端口是表的主键

澄清:在这段代码的一次迭代中,它将所有内容发送到服务器1没有任何问题,但它完全忽略了服务器2.我已经测试并确保split(if语句)按预期工作,并打印出更正服务器名称并将$ sql设置为预期值(打印出来时)。它只会移动表的第一列,然后将其他所有内容设置为空白,并将ID设置为0并发送一堆重复的主要错误。

1 个答案:

答案 0 :(得分:0)

我认为您不应该插入ID,这可能是您可能与表主键和auto_increment进行斗争的地方。只需从插入中删除id,它应该没问题。取决于您如何设置表结构。一般来说,您不应该保留ID,只需要保存唯一性和索引管家等。