php while循环和foreach循环 mysql_fetch_array()

while($rows=mysql_fetch_array()){
代码块。。

}
while循环后,可以把表里面的数据都循环输出

$rows=mysql_fetch_array()
foreach($rows as $values){
代码块。。

}
foreach循环只能循环一条数据出来!

我认为$rows这时候不管在while还是foreach都是属于数组,为什么两种情况不一样。后来问了下朋友,说可能是mysql_fetch_array()的原因,是因为这个是个结果集的问题?请大神能帮我理一下,说一下具体的区别

while循环和foreach循环在mysql_fetch_array()的用法:
while举例如下:
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf ("ID: %s Name: %s", $row[0], $row[1]);
}

mysql_free_result($result);
?>
foreach循环举例如下:
$rows = mysql_fetch_all($result);
foreach($rows as $row){
printf ("ID: %s Name: %s", $rows[0], $rows[1]);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-05-19
既然你知道是mysql_fetch_arry()的原因,为什么不翻翻手册?
mysql_fetch_array:Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.
关键字" moves the internal data pointer ahead"

mysql_fetch_array是从结果集中取出一行,然后指针向前移动

每次调用的时候除了做取出的动作还会对指针位置向前移动,如果结果集没有任何数据了则反回false。本回答被提问者和网友采纳
第2个回答  2015-05-19
mysql_fetch_array()每循环一次, 数据指针就下移一行!
如果mysql_fetch_array()不参与循环 , 那取回的数据自然就只有一行!

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网