php while ($row = mysql_fetch_array($result)) - 执行了多少个循环?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2974011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:18:57  来源:igfitidea点击:

while ($row = mysql_fetch_array($result)) - how many loops are being performed?

phpmysql

提问by Haroldo

if...

如果...

$query = "SELECT col1,col2,col3 FROM table WHERE id > 100"
$result = mysql_query($query);

for this action:

对于此操作:

while ($row = mysql_fetch_array($result)){
   ....
}

is this doing 1 loop (iterated x times)?

这是在做 1 个循环(迭代 x 次)吗?

and for this one:

对于这个:

$row = mysql_fetch_array($result)
foreach($row as $r){
   ...
}

is this doing 2 loops (iterated x times)?

这是在做 2 个循环(迭代 x 次)吗?

where x is the number of results

其中 x 是结果数



EDIT:

编辑:

ok thanks guys, ok I basically phrased this question really, really badly.

好的,谢谢大家,好的,我基本上真的非常非常糟糕地表达了这个问题。

in retrospect it should have been

回想起来应该是

'does mysql_fetch_array() only return one row each time it is called'

'mysql_fetch_array() 是否每次调用只返回一行'

I an now happy that my understanding of mysql_fetch_array() was v. incorrect!

我现在很高兴我对 mysql_fetch_array() 的理解是错误的!

thanks for your time!

谢谢你的时间!

回答by deceze

I'm assuming mysql_fetch_array() perfroms a loop, so I'm interested in if using a while() in conjunction with it, if it saves a nested loop.

我假设 mysql_fetch_array() 执行一个循环,所以我对是否将 while() 与它结合使用很感兴趣,如果它保存了一个嵌套循环。

No. mysql_fetch_arrayjust returns the next row of the result and advances the internal pointer. It doesn't loop. (Internally it may or may not use some loop somewhere, but that's irrelevant.)

不,mysql_fetch_array只返回结果的下一行并推进内部指针。它不会循环。(在内部它可能会或可能不会在某处使用某个循环,但这无关紧要。)

while ($row = mysql_fetch_array($result)) {
   ...
}

This does the following:

这将执行以下操作:

  1. mysql_fetch_arrayretrieves and returns the next row
  2. the row is assigned to $row
  3. the expression is evaluated and if it evaluates to true, the contents of the loop are executed
  4. the procedure begins anew
  1. mysql_fetch_array检索并返回下一行
  2. 该行被分配给 $row
  3. 计算表达式,如果计算结果为true,则执行循环的内容
  4. 程序重新开始
$row = mysql_fetch_array($result);
foreach($row as $r) {
    ...
}
$row = mysql_fetch_array($result);
foreach($row as $r) {
    ...
}

This does the following:

这将执行以下操作:

  1. mysql_fetch_arrayretrieves and returns the next row
  2. the row is assigned to $row
  3. foreachloops over the contents of the array and executes the contents of the loop as many times as there are items in the array
  1. mysql_fetch_array检索并返回下一行
  2. 该行被分配给 $row
  3. foreach循环遍历数组的内容,并根据数组中的项执行循环内容的次数

In both cases mysql_fetch_arraydoes exactly the same thing. You have only as many loops as you write. Both constructs do not do the same thing though. The second will only act on one row of the result, while the first will loop over all rows.

在这两种情况下mysql_fetch_array都做完全相同的事情。您只有与您编写的一样多的循环。但是,这两种构造都不会做同样的事情。第二个将只作用于结果的一行,而第一个将遍历所有行。

回答by TheDeadMedic

It depends how many rows are returned in $results, and how many columns there are in $row?

这取决于有多少行返回$results,有多少列$row

回答by Moox

Considering only one row is returned, only one loop will be done in both cases. Though it will check for the loop entering condition twice on each.

考虑到只返回一行,两种情况都只进行一次循环。虽然它会在每个循环中检查两次进入条件。

回答by Hammerite

For the first one: your program will go through the loop once for every row in the result set returned by the query. You can know in advance how many results there are by using mysql_num_rows().

对于第一个:您的程序将对查询返回的结果集中的每一行进行一次循环。您可以通过使用 提前知道有多少结果mysql_num_rows()

For the second one: this time you are only using one rowof the result set and you are doing something for each of the columns. That's what the foreachlanguage construct does: it goes through the body of the loop for each entry in the array $row. The number of times the program will go through the loop is knowable in advance: it will go through once for every column in the result set (which presumably you know, but if you need to determine it you can use count($row)).

对于第二个:这次您只使用结果集的一行,并且您正在为每一做一些事情。这就是foreach语言结构所做的:它遍历数组中每个条目的循环体$row。程序通过循环的次数是预先知道的:它会为结果集中的每一列循环一次(这可能你知道,但如果你需要确定它,你可以使用count($row))。

回答by darpet

The first row:

第一行:

$result = mysql_query($query);  

return php db resource.

返回 php db 资源。

The second row

第二行

while ($row = mysql_fetch_array($result))  

Loops all the records returned by the query.

循环查询返回的所有记录。

Use mysql_fetch_assoc instead
In that case the row has key=>value pairs.
In the while just put this:
print_r($row)and you will understand
If you use mysql_fetch_assoc the format of the row will be:

改用 mysql_fetch_assoc
在这种情况下,该行具有 key=>value 对。
在 while 中只需输入:
print_r($row)你就会明白
如果你使用 mysql_fetch_assoc 行的格式将是:

$row["column1_name"] = column1_value;  
$row["column2_name"] = column2_value;  

For this one:

对于这个:

$row = mysql_fetch_assoc($result)  
foreach ($row as $columnName => $columnValue) {  
    ...  
}  

You will get the firs row from the query and you will iterate all columns in the first result of the query.

您将从查询中获得第一行,并且您将迭代查询的第一个结果中的所有列。

回答by Mike Moore

Yes, mysql_fetch_array()only returns one result. If you want to retrieve more than one row, you need to put the function call in a whileloop.

是的,mysql_fetch_array()只返回一个结果。如果要检索多行,则需要将函数调用放入while循环中。

Two examples:

两个例子:

This will only return the first row

这只会返回第一行

$row = mysql_fetch_array($result);

This will return one row on each loop, until no more rows are available from the result set

这将在每个循环中返回一行,直到结果集中没有更多行可用

while($row = mysql_fetch_array($result))
{
    //Do stuff with contents of $row
}

回答by GOsha

$query = "SELECT col1,col2,col3 FROM table WHERE id > 100"
$result = mysql_query($query);
if(mysql_num_rows($result)>0)
{
 while($row = mysql_fetch_array()) //here you can use many functions such as mysql_fetch_assoc() and other
 {
//It returns 1 row to your variable that becomes array and automatically go to the next result string
  Echo $row['col1']."|".Echo $row['col2']."|".Echo $row['col2'];
 }
}