1

私はこのコンセプトに 5 時間ほどこだわっていますが、本当にイライラしています。

$result = $mysqli->query("SELECT col FROM table");
while($row = $result->fetch_assoc()){
    echo $row['data'];
}

関数が一度に 1 行しかフェッチしないことは理解していますが、ループがリセットされたときにどのように呼び出されるのかわかりません。でどのように呼び出され$row = $result->fetch_assocますか? $rowまた、 nullの場合、この条件はどのように true と評価されますか?

4

5 に答える 5

0

あなたのコードを考えると:

while($row = $result->fetch_assoc()){
    echo $row['data'];
}

ループ条件は、次の同等のものに書き換えることができます。

while (($row = $result->fetch_assoc()) != false) {
    echo $row['data'];
}

言い換えれば、 while$rowは falsy ではなく、ループを続行します。nullも虚偽と見なされます。

于 2013-09-03T06:36:07.000 に答える
0

$row が null の場合、この条件はどのように true と評価されますか?

そうではありません。それがポイントです。

于 2013-09-03T06:28:27.803 に答える
-1

コードに対する次の説明が役立つ場合があります

// run the query. this returns a mysqli_result object on success.
// on failure query function returns false and you have to handle this condition.
$result = $mysqli->query("SELECT col FROM table");

// fetch_assoc() here you fetch one row at a time from the mysqli_result
// object into $row. this also moves the pointer to the next row
// so the next call returns the next row.
// This returns false when there is no more rows and exit your while loop
while($row = $result->fetch_assoc()){
    echo $row['data'];
}

参照したいリンク
http://www.php.net/manual/en/class.mysqli-result.php
http://php.net/manual/en/mysqli-result.fetch-assoc.php

于 2013-09-03T05:48:48.077 に答える