while($ row = mysql_fetch_array($ result))-正在执行多少个循环? [英] while ($row = mysql_fetch_array($result)) - how many loops are being performed?

查看:240
本文介绍了while($ row = mysql_fetch_array($ result))-正在执行多少个循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果...

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

此操作:

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

这是做1次循环(重复x次)吗?

对于这个:

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

这是做2个循环(重复x次)吗?

其中x是结果数

好的,谢谢大家,好的,我基本上非常,非常糟糕地回答了这个问题.

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

回想起来应该是

'mysql_fetch_array()只返回一个 每次被称为'

'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()执行一个循环,所以我对是否将while()与它结合使用(如果它保存了嵌套循环)感兴趣.

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仅返回结果的下一行并前进内部指针.它不会循环. (在内部它可能会或可能不会在某处使用某些循环,但这无关紧要.)

No. mysql_fetch_array just 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.)

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

这将执行以下操作:

  1. mysql_fetch_array检索并返回下一行
  2. 该行已分配给$row
  3. 对表达式进行求值,如果其求值为true,则执行循环的内容
  4. 程序重新开始
  1. mysql_fetch_array retrieves 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

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

这将执行以下操作:

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

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

In both cases mysql_fetch_array does 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.

这篇关于while($ row = mysql_fetch_array($ result))-正在执行多少个循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆