MySQL fetch_assoc()显示少1个结果

时间:2016-10-09 11:53:32

标签: mysql select while-loop fetch

我有一张如下表:

hub dep
A B
A C
B D
B E
B F
E G

我使用mysql select来获取查询,我的代码如下所示:

$sql         = "SELECT dep FROM handd WHERE hub='B'";
        $result      = $conn->query( $sql );
        $row         = $result->fetch_assoc();

        while($row = $result->fetch_assoc()) {
    echo "id: " . $row["dep"]."<br>";
}

但它只给出了如下结果:

id: E
id: F

我想知道D在哪里?

1 个答案:

答案 0 :(得分:1)

 $row         = $result->fetch_assoc();

此行存储&#39; B D&#39;的结果。它实际应该是:

    $sql         = "SELECT dep FROM handd WHERE hub='B'";
    $result      = $conn->query( $sql );

    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["dep"]."<br>";
    }