mysql_fetch_array while循环.它是如何工作的? [英] mysql_fetch_array while loop. How does it work?

查看:80
本文介绍了mysql_fetch_array while循环.它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关php.net的功能,但仍未回答我的问题.我知道C的初学者数量,而且我才刚刚开始使用php.通常,在C中,如果要进行while循环,则需要满足一些条件才能将循环推进到不再有效的位置,如下所示:

I have read about the function on php.net and that has still not answered my question. I know a beginners amount of C and I've just started using php. Normally in C if you were to do a while loop there needs to be some condition to advance the loop to a point where it will no longer be valid like so:

while (x >= 10)  
{ 
    printf("..."; 
    printf("x \n"; 
    x++; 
}

但是,在我用于pm消息系统的php脚本中,我有一个while循环,如下所示:

However in my php script that I'm using for a pm message system I have a while loop like this:

while($row2 = mysql_fetch_array($query))

其后:

{ 
  echo "<table border=1>";
  echo "<tr><td>";
  echo "Message #: ";
  echo $row['id'];
  echo "</td></tr>";
  echo "<tr><td>";
  echo "To: ";
  echo $row['to'];
  echo "</td></tr>";
  echo "<tr><td>";
  echo "From: ";
  echo $row['from'];
  echo " ";
  echo "</td></tr>";
  echo "<tr><td>";
  echo "Message: ";
  echo $row['message'];
  echo "</td></tr>";
  echo "</br>";
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr><td colspan=2></td></tr>
<tr><td></td><td>
<input type="hidden" name="id" maxlength="32" value = "<?php echo $row['id']; ?>">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="delete" value="Delete PM # <?php echo $row['id']; ?>">
</td>
<td colspan="2" align="right">
<input type="submit" name="reply" value="Reply to <?php echo $row['from']; ?>">
</td></tr>
</table>
<?php } ?>

这是如何工作的,从C背景看来,这几乎像是停留在同一位置,每次我们通过相同的"$ query"从相同的"$ query"打印出相同的"array fetch""row"循环....

How exactly does this work, from a C background it would seem almost like this would stay in the same spot, printing out the same "array fetch" 'row' from the same "$query" every time we go through the loop....

有没有一种方法可以编写此代码,以使我对发生的事情有更好的逻辑理解?就像说:

Is there a way I can write this to give me a better logical understanding of whats going on? like saying:

$i=0;
while ($row = ($i+mysql_fetch_array($query)) {
...
...
$i++;}

我知道这可能行不通,但是此功能如何自我增加?并有一种方法可以编写它在代码中实际上可以看到某种增量的地方?

I know that probably wont work but how does this function increment itself? And is there a way to write it where it would actually have some sort of incrementation visible in the code?

谢谢

推荐答案

每次调用mysql_fetch_array都会从查询中拉出下一行. while循环一直返回true,而mysql_fetch_array仍有剩余空间可分配给变量$row2.一旦它超出了行数,它就没有东西可提供该变量,并返回false.

Every time you call mysql_fetch_array it pulls the next row from your query. That while loop keeps returning true while the mysql_fetch_array still has something left to assign to the variable $row2. Once it's out of rows, it has nothing left to give the variable, and false is returned.

ETA:关于您提到的最后一点,您可以像示例中那样在循环的每次迭代中增加变量的增量,但这并不是完全必要的.您还可以通过在while循环之前执行类似$var = mysql_num_rows($data)的操作来查看返回了多少行.

ETA: Regarding the last bit you mentioned, you can have a variable increment in each iteration of the loop like in your example, but it's not entirely necessary. You can also just see how many rows have been returned by doing something like $var = mysql_num_rows($data) before your while loop.

这篇关于mysql_fetch_array while循环.它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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