Home > Backend Development > PHP Tutorial > foreach 的怪有关问题

foreach 的怪有关问题

WBOY
Release: 2016-06-13 13:07:19
Original
810 people have browsed it

foreach 的怪问题
$result = mysql_query("SELECT * FROM TB");
while($row = mysql_fetch_array($result))
{
/*这种方法能成功显示
for ($i=1; $i {
  echo $row[$i]." | ";
}

   
  echo "
";
  */

  foreach ($row as $x)
  {
  echo $x." | ";

}

}
搞不明白为什么弟二种方法每个字段的数据显示了2次

------解决方案--------------------
mysql_fetch_array 返回的是下标数组和关联数组的混合数组
查询结果的每一列都要在下标数组和关联数组个出现一次

for 循环的是下标数组的部分
而 foreach 循环的是全部

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template