dongqieli4164 2015-07-15 19:30
浏览 49
已采纳

为什么“while”循环总是用于迭代mysql_fecth_array()函数的结果?

Why don't people use for ,foreach or do..while() and why omit increment counter in while?

`<?php  while ( $fa = mysql_fetch_array($sel1) )//uesd mysql_fetch_array() function in while loop
    { 
          echo  $fa['cid'];//display client id 
    }

// while Syntax in w3school give to used this

 $x = 1; 

 while($x <= 5) {
     echo "The number is: $x";
     $x++;
 }  
?>//show why we don't used mysql_fecth_array() like this 
  • 写回答

2条回答 默认 最新

  • doupang9080 2015-07-15 20:33
    关注

    This is kind of dated information. In one way you're asking a question that is just accepted along the community. It is understood better that while I have information to show ... do something. You generally wouldn't say foreach of these items ... do something though you could. However, the other problem is mysql_fetch_array returns FALSE if there are no more rows. This would not work in a foreach because it is not an array. A for would also fail because to check for the finishing of a for you have to go to some point and end.. FALSE is not a valid point of check (that I have ever tried or used).


    @Michael Berkowski Adds:

    As of PHP 5.4+, the mysqli_result class does have Iterator support, meaning you can do $result = mysqli_query(...); and subsequently foreach ($result as $row) {...} and it will fetch associative arrays

    Though this isn't to be said the most commonly used form which is why we have the question.


    You could do...while but why would you. You don't have most the information that you're going to need from the fetch array.

    While is accepted and generally better. Doesn't fail and has a fall back if the mysql fails any way.

    Lastly... don't use mysql_* anymore. Switch to mysqli_*. Safer.. smarter.. better.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装