为选定的行提供结果

时间:2016-04-05 07:39:07

标签: php mysql

我正在尝试获取我在$sql = "";中选择的每一行的结果 但我没有成功。尝试了不同的代码,但没有一个工作。这是我的代码:

$sql2 = "SELECT player1, player2, player3, player4, player5, player6, player7, player8, player9, player10, player11, player12, player13, player14, player15, player16, player17, player18, player19, player20, player21, player22, player23, player24 FROM `savedpacks` WHERE `packid`=" . $_GET['id'] . "";

    $result2 = $conn->query($sql2);
    while( $row2 = mysqli_fetch_assoc( $result2)) {
    $sql = "SELECT * FROM `players_db` WHERE `id`=" . $row2['player1'] . "";
    $result = $conn->query($sql);   
    $row = mysqli_fetch_assoc( $result);    

    if ($row['Revision'] != 0) {`

我正在尝试获取每个选定行的结果,所以播放器1,播放器2等等。但是你可以看到我设法只获得了player1的行。我希望有人可以帮助我。谢谢!

3 个答案:

答案 0 :(得分:0)

您可以这样使用:

$sql2 = "You Query";

$players = array();
$result2 = $conn->query($sql2);
while($row2 = $result->fetch_assoc()) {
    $players[] = $row2;
}

foreach ($players as $key => $value) {
    $sql = "SELECT * FROM `players_db` WHERE `id`=" . $value . "";    
}

将值存储到数组中,然后使用foreach循环并为每次迭代获取$value

答案 1 :(得分:0)

在循环中使用循环,像这样

$sql2 = "SELECT player1, player2, player3, player4, player5, player6, player7, player8, player9, player10, player11, player12, player13, player14, player15, player16, player17, player18, player19, player20, player21, player22, player23, player24 FROM `savedpacks` WHERE `packid`=" . $_GET['id'] . "";

    $result2 = $conn->query($sql2);
    while( $row2 = mysqli_fetch_assoc( $result2)) {
    for($i=1;$i<=24;$i++)
    {
        $sql = "SELECT * FROM `players_db` WHERE `id`=" . $row2['player'.$i] . "";
        $result = $conn->query($sql);   
        $row = mysqli_fetch_assoc( $result);    

        if ($row['Revision'] != 0) {`...}
    }

答案 2 :(得分:0)

您可以将连接查询用于此

    $sql = "SELECT * FROM players_db as P join savedpacks as S on (S.id = P.player1 || S.id = P.player2 || S.id = P.player3 || S.id = P.player4 || S.id = P.player5 || S.id = P.player6 || S.id = P.player7 || S.id = P.player8 || S.id = P.player9 || S.id = P.player10 || S.id = P.player11 || S.id = P.player12 || S.id = P.player13 || S.id = P.player14 || S.id = P.player15 || S.id = P.player16 || S.id = P.player17 || S.id = P.player18 || S.id = P.player19 || S.id = P.player20 || S.id = P.player21 || S.id = P.player22 || S.id = P.player23 || S.id = P.player24) where S.packid=" . $_GET['id'] . "";

您可以在单个查询iteslf中获得结果,而不是使用2个查询