简体   繁体   English

PHP 当 mysqli_fetch_assoc 表达式仅用一个变量替换时提供无限 While 循环

[英]PHP Gives Infinite While Loop when mysqli_fetch_assoc Expression is Replaced with just a Variable

I'm a complete newbie in PHP and I just don't understand how while loop works with mysqli_fetch_assoc .我是 PHP 的新手,我只是不明白while循环如何与mysqli_fetch_assoc一起工作。 The former execute statements based upon expressions, the latter retrieves a row from the database.前者基于表达式执行语句,后者从数据库中检索一行。 So if you don't end the loop, it will continue to iterate the field in the first row infinitely;所以如果不结束循环,它将继续无限迭代第一行中的字段;

<?php
$connect_db=mysqli_connect('localhost','root','root','db');
$fetch_data="SELECT * FROM tabel";
$query_db=mysqli_query($connect_db,$fetch_data);
$fetch_row=mysqli_fetch_assoc($query_db);
while($fetch_row){
    $column_2_array=$fetch_row['column_2'];
    echo($column_2_array);
    break;
}
?>

What I'm scratching my head is if the whole expression is put inside the parentheses instead of just the variable, then the loop will iterates the whole content of the column.我正在摸不着头脑的是,如果整个表达式被放在括号内而不仅仅是变量,那么循环将迭代列的全部内容。

<?php
$connect_db=mysqli_connect('localhost','root','root','db');
$fetch_data="SELECT * FROM tabel";
$query_db=mysqli_query($connect_db,$fetch_data);
while($fetch_row=mysqli_fetch_assoc($query_db)){
    $column_2_array=$fetch_row['column_2'];
    echo($column_2_array);
}
?>

Why is it that on the second example above, after the first loop, while is iterating the subsequent rows on the selected column?为什么在上面的第二个示例中,在第一个循环之后, while正在迭代所选列上的后续行?

Clarifying my question further, if I take away break from the first example, then what is the difference between example 1 and 2?进一步澄清我的问题,如果我从第一个示例中删除break ,那么示例 1 和示例 2 之间有什么区别? This is what confuses me since I thought they're identical.这让我感到困惑,因为我认为它们是相同的。

To understand why this approach of looping mysqli_result works with a while loop you need to understand two things:要了解为什么这种循环 mysqli_result 的方法适用于while循环,您需要了解两件事:

  • PHP uses type juggling . PHP 使用类型杂耍 Each row returned by mysqli_fetch_assoc() will be an array. mysqli_fetch_assoc()返回的每一行都是一个数组。 It is safe to assume that this array will never be empty, so the value will always be cast to true when used in boolean context.可以安全地假设此数组永远不会为空,因此在 boolean 上下文中使用时,该值将始终强制转换为true
  • mysqli_fetch_assoc() returns a single row from the result and moves an internal pointer to the next row. mysqli_fetch_assoc()从结果中返回一行并将内部指针移动到下一行。 When pointer reaches the last row, each subsequent call will return NULL .当指针到达最后一行时,每个后续调用都将返回NULL

This while loop is equivalent to:此 while 循环等效于:

$fetch_row = mysqli_fetch_assoc($query_db); // save the first row in the variable. It could also be false
while($fetch_row) { // as long as $fetch_row is not false-ish
    $column_2_array = $fetch_row['column_2'];
    echo $column_2_array;

    // fetch false or the next row
    $fetch_row = mysqli_fetch_assoc($query_db)
}

Worth noting that such an approach to looping is not recommended.值得注意的是,不建议使用这种循环方法。 It is much easier to use a foreach loop.使用foreach循环要容易得多。 This will loop on the whole resultset one by one from the first row until the last.这将从第一行到最后一行一个接一个地循环整个结果集。 You can loop the same mysqli_result object multiple times.您可以多次循环相同的mysqli_result object。

foreach($query_db as $fetch_row) {
    $column_2_array = $fetch_row['column_2'];
    echo $column_2_array;
}

An even better alternative is to use fetch_all(MYSQLI_ASSOC) and store all rows in a multi-dimensional array.一个更好的选择是使用fetch_all(MYSQLI_ASSOC)并将所有行存储在一个多维数组中。 And as always, use PDO instead of mysqli whenever possible.和往常一样,尽可能使用 PDO 而不是 mysqli。

The simplest explanation is,最简单的解释是,

When you run mysqli_query() For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object which contains data from the database.当您运行mysqli_query()对于成功的 SELECT、SHOW、DESCRIBE 或 EXPLAIN 查询时, mysqli_query()将返回一个mysqli_result object,其中包含来自数据库的数据。


  • mysqli_fetch_assoc() - Fetch a result row(one row from the mysqli_result object) as an associative array at each time it called and moves the internal pointer to the next. mysqli_fetch_assoc() - 在每次调用时获取结果行(从mysqli_result对象中的一行)作为关联数组,并将内部指针移动到下一个。 $fetch_row is used to store that associative array containing data of single row getting from the mysqli_result object ' $fetch_row用于存储包含mysqli_result object 获取的单行数据的关联数组

you can retrieve data belongs to that raw by using $fetch_row['column_name']您可以使用$fetch_row['column_name']检索属于该原始数据的数据

  • The while loop does the calling of mysqli_fetch_assoc() over and over. while循环一遍又一遍地调用mysqli_fetch_assoc() After one row is fetched, it goes to the next one and so on till it reaches the end of the object and returns NULL , which means false and breaks the while loop.获取一行后,它会转到下一行,依此类推,直到到达 object 的末尾并返回NULL ,这意味着falsebreaks while循环。

So in your first code sample,所以在你的第一个代码示例中,

$fetch_row=mysqli_fetch_assoc($query_db); will return and assign only one data row to $fetch_row (data type- assoc. array).将只返回一个数据行并将其分配给$fetch_row (数据类型-关联数组)。 Which makes the condition of the while loop true .这使得while循环的条件为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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