PHP While循环只回显一次

PHP While循环只回显一次,php,loops,Php,Loops,这是我的PHP代码: while($row1 = mysql_fetch_object($query1)) { echo "*".$row1->id."*"; while ($row2 = mysql_fetch_object($query2)) { echo "%".$row2->id."%"; } } 我希望它输出例如:*1*%1%%2%%3%%4%%*2*%1%%2%%3%%4%%*3*%1%%2%%3%%4% 但是这个循环输出的是:*1*%1%

这是我的PHP代码:

while($row1 = mysql_fetch_object($query1)) {
   echo "*".$row1->id."*";
   while ($row2 = mysql_fetch_object($query2)) {
      echo "%".$row2->id."%";
   }
}
我希望它输出例如:*1*%1%%2%%3%%4%%*2*%1%%2%%3%%4%%*3*%1%%2%%3%%4%

但是这个循环输出的是:*1*%1%%2%%3%%4%%*2**3*

它只输出$row1的第一个循环中的$row2值


我怎样才能解决这个问题?谢谢。

在第一次循环之后,query2将不再返回任何结果。因此,while在每个附加循环中都是false。您可能希望使用mysqli_data_seek重置查询,或者将所有数据存储在一个单独的数组中,并在其中循环

另请注意: 根据文件

从PHP5.5.0开始,mysql扩展已被弃用,并将被删除 未来相反,MySQLi或PDO_MySQL扩展应该是 使用


在第一个循环之后,query2将不再返回任何结果。因此,while在每个附加循环中都是false。您可能希望使用mysqli_data_seek重置查询,或者将所有数据存储在一个单独的数组中,并在其中循环

另请注意: 根据文件

从PHP5.5.0开始,mysql扩展已被弃用,并将被删除 未来相反,MySQLi或PDO_MySQL扩展应该是 使用


问题是,一旦您完全遍历$query2,结果就到此为止了。下次通过$row1循环时,您仍然处于$query2的末尾,并且没有结果。尝试使用返回结果的开头:

while($row1 = mysql_fetch_object($query1)) {
   echo "*".$row1->id."*";
   mysql_data_seek($query2, 0);
   while ($row2 = mysql_fetch_object($query2)) {
      echo "%".$row2->id."%";
   }
}

问题是,一旦您完全遍历$query2,结果就到此为止了。下次通过$row1循环时,您仍然处于$query2的末尾,并且没有结果。尝试使用返回结果的开头:

while($row1 = mysql_fetch_object($query1)) {
   echo "*".$row1->id."*";
   mysql_data_seek($query2, 0);
   while ($row2 = mysql_fetch_object($query2)) {
      echo "%".$row2->id."%";
   }
}

当调用$row2=mysql\u fetch\u object$query2时,您可能没有得到任何东西,这将为您提供所得到的输出

当调用$row2=mysql\u fetch\u object$query2时,您可能没有得到任何东西,这将为您提供所得到的输出

如果确实需要多次重复第二个查询数据,请先将其放入数组,然后根据需要在该数组上循环多次。

如果确实需要多次重复第二个查询数据,请先将其放入数组,并根据需要多次循环此数组。

首先:在PHP5.5中,mysql_*函数已经并将被删除。考虑使用或替代。

话虽如此,;回到你的问题:

每个结果集都包含一个指向结果集中某个记录的内部指针。最初,该指针指向第一条记录,每次调用mysql\u fetch\u对象时,该指针都会前进

在第一个内部循环之后,$query2结果集的内部指针将已经位于列表的末尾,因此对mysql\u fetch\u对象的后续调用将只返回FALSE

如果内部查询依赖于$row1中的值,则需要在外部循环中重新执行第二个查询。否则,您可以使用重置结果指针。

首先:在PHP5.5中,mysql_*函数将被删除。考虑使用或替代。

话虽如此,;回到你的问题:

每个结果集都包含一个指向结果集中某个记录的内部指针。最初,该指针指向第一条记录,每次调用mysql\u fetch\u对象时,该指针都会前进

在第一个内部循环之后,$query2结果集的内部指针将已经位于列表的末尾,因此对mysql\u fetch\u对象的后续调用将只返回FALSE


如果内部查询依赖于$row1中的值,则需要在外部循环中重新执行第二个查询。否则,您可以使用。

重置结果指针。关于mysql\u fetch\u对象,有几点需要注意。从PHP文档中:

警告:从PHP 5.5.0开始,此扩展已被弃用

请注意上面

返回一个对象,该对象的属性与获取的行相对应,并将内部数据指针向前移动

请注意我加粗的部分

代码中的$result对象(即$query2)是一个迭代对象,其指针指向当前项

Current Item
 |
 v
[1][2][3][4]
当第一个循环碰到第二个循环时,它会在整个循环中迭代,因此在最后,对象看起来如下所示:

         Current Item
          |
          v
[1][2][3][4]
mysql_fetch_object() ~
  1. Get the next time
  2. Uh, there are no more objects because we're at item [4].  Return done.
while($row1 = mysql_fetch_object($query1)) {
   echo "*".$row1->id."*";
   while ($row2 = mysql_fetch_object($query2)) {
      echo "%".$row2->id."%";
   }
   // put the result pointer back to the front
   mysql_data_seek($query2, 0)
}
while($row1 = $query1->fetch_object()) {
   echo "*".$row1->id."*";
   while ($row2 = $query1->fetch_object()) {
      echo "%".$row2->id."%";
   }
   // put the result pointer back to the front
   $query2->data_seek(0);
}
对于第一次循环的每次迭代,在第一次之后,mysql_fetch_对象函数基本上如下所示:

         Current Item
          |
          v
[1][2][3][4]
mysql_fetch_object() ~
  1. Get the next time
  2. Uh, there are no more objects because we're at item [4].  Return done.
while($row1 = mysql_fetch_object($query1)) {
   echo "*".$row1->id."*";
   while ($row2 = mysql_fetch_object($query2)) {
      echo "%".$row2->id."%";
   }
   // put the result pointer back to the front
   mysql_data_seek($query2, 0)
}
while($row1 = $query1->fetch_object()) {
   echo "*".$row1->id."*";
   while ($row2 = $query1->fetch_object()) {
      echo "%".$row2->id."%";
   }
   // put the result pointer back to the front
   $query2->data_seek(0);
}
那么,你是如何让它工作的呢?您可以简单地将结果保存到一个数组中,然后在该数组上迭代,或者您可以使用mysql_data_seek重置指针,mysql_data_seek在5.5中也被弃用

要重置数据指针,如下所示:

         Current Item
          |
          v
[1][2][3][4]
mysql_fetch_object() ~
  1. Get the next time
  2. Uh, there are no more objects because we're at item [4].  Return done.
while($row1 = mysql_fetch_object($query1)) {
   echo "*".$row1->id."*";
   while ($row2 = mysql_fetch_object($query2)) {
      echo "%".$row2->id."%";
   }
   // put the result pointer back to the front
   mysql_data_seek($query2, 0)
}
while($row1 = $query1->fetch_object()) {
   echo "*".$row1->id."*";
   while ($row2 = $query1->fetch_object()) {
      echo "%".$row2->id."%";
   }
   // put the result pointer back to the front
   $query2->data_seek(0);
}
注意,缺点是您调用的函数正在创建对象,这会在每次运行时产生处理开销

另一个选项是将结果保存到一个数组中,并且只需每隔一天循环一次数组 时间:

请注意,此方法将在数组中存储对象时创建额外的内存使用量,但它将节省CPU处理时间,因为您不需要反复创建对象,也不需要调用函数

<>如果你只打印输出,你可以考虑先保存结果。无论您现在多次循环$secondary_result,最终结果将始终与代码相同,第一个循环没有直接影响第二个结果的迹象

在这种情况下,这更有意义

$buffer = '';
while ($row2 = mysql_fetch_object($query2)) {
  $buffer .= "%".$row2->id."%";
}

while($row1 = mysql_fetch_object($query1)) {
    echo "*".$row1->id."*";
    echo $buffer;
}
但我真的不知道你为什么这么做。如果您正在执行嵌套循环,通常是因为第一个循环的结果会影响第二个循环

但我希望这有帮助

干杯

编辑

根据@Blazemonger关于展望未来的评论,PDO的等价物为:

当您使用PDO函数得到一个结果对象时,您会这样循环:

         Current Item
          |
          v
[1][2][3][4]
mysql_fetch_object() ~
  1. Get the next time
  2. Uh, there are no more objects because we're at item [4].  Return done.
while($row1 = mysql_fetch_object($query1)) {
   echo "*".$row1->id."*";
   while ($row2 = mysql_fetch_object($query2)) {
      echo "%".$row2->id."%";
   }
   // put the result pointer back to the front
   mysql_data_seek($query2, 0)
}
while($row1 = $query1->fetch_object()) {
   echo "*".$row1->id."*";
   while ($row2 = $query1->fetch_object()) {
      echo "%".$row2->id."%";
   }
   // put the result pointer back to the front
   $query2->data_seek(0);
}

上面的示例显示了MySqli的fetch对象和指针重置版本。

关于mysql\u fetch\u对象需要注意的几点。从PHP文档中:

警告:从PHP 5.5.0开始,此扩展已被弃用

请注意上面

返回一个对象,该对象的属性与获取的行相对应,并将内部数据指针向前移动

请注意我加粗的部分

代码中的$result对象(即$query2)是一个迭代对象,其指针指向当前项

Current Item
 |
 v
[1][2][3][4]
当第一个循环碰到第二个循环时,它会在整个循环中迭代,因此在最后,对象看起来如下所示:

         Current Item
          |
          v
[1][2][3][4]
mysql_fetch_object() ~
  1. Get the next time
  2. Uh, there are no more objects because we're at item [4].  Return done.
while($row1 = mysql_fetch_object($query1)) {
   echo "*".$row1->id."*";
   while ($row2 = mysql_fetch_object($query2)) {
      echo "%".$row2->id."%";
   }
   // put the result pointer back to the front
   mysql_data_seek($query2, 0)
}
while($row1 = $query1->fetch_object()) {
   echo "*".$row1->id."*";
   while ($row2 = $query1->fetch_object()) {
      echo "%".$row2->id."%";
   }
   // put the result pointer back to the front
   $query2->data_seek(0);
}
对于第一次循环的每次迭代,在第一次之后,mysql_fetch_对象函数基本上如下所示:

         Current Item
          |
          v
[1][2][3][4]
mysql_fetch_object() ~
  1. Get the next time
  2. Uh, there are no more objects because we're at item [4].  Return done.
while($row1 = mysql_fetch_object($query1)) {
   echo "*".$row1->id."*";
   while ($row2 = mysql_fetch_object($query2)) {
      echo "%".$row2->id."%";
   }
   // put the result pointer back to the front
   mysql_data_seek($query2, 0)
}
while($row1 = $query1->fetch_object()) {
   echo "*".$row1->id."*";
   while ($row2 = $query1->fetch_object()) {
      echo "%".$row2->id."%";
   }
   // put the result pointer back to the front
   $query2->data_seek(0);
}
那么,你是如何让它工作的呢?您可以简单地将结果保存到一个数组中,然后在该数组上迭代,或者您可以使用mysql_data_seek重置指针,mysql_data_seek在5.5中也被弃用

要重置数据指针,如下所示:

         Current Item
          |
          v
[1][2][3][4]
mysql_fetch_object() ~
  1. Get the next time
  2. Uh, there are no more objects because we're at item [4].  Return done.
while($row1 = mysql_fetch_object($query1)) {
   echo "*".$row1->id."*";
   while ($row2 = mysql_fetch_object($query2)) {
      echo "%".$row2->id."%";
   }
   // put the result pointer back to the front
   mysql_data_seek($query2, 0)
}
while($row1 = $query1->fetch_object()) {
   echo "*".$row1->id."*";
   while ($row2 = $query1->fetch_object()) {
      echo "%".$row2->id."%";
   }
   // put the result pointer back to the front
   $query2->data_seek(0);
}
注意,缺点是您调用的函数正在创建对象,这会在每次运行时产生处理开销

另一个选项是将结果保存到数组中,每次只需在数组中循环:

$secondary_result = array();
while ($row2 = mysql_fetch_object($query2)) {
  $secondary_result[] = $row2;
}

while($row1 = mysql_fetch_object($query1)) {
    echo "*".$row1->id."*";
    foreach($secondary_result as $row2) {
        echo "%".$row2->id."%";
    }
}
请注意,此方法将在数组中存储对象时创建额外的内存使用量,但它将节省CPU处理时间,因为您不需要反复创建对象,也不需要调用函数

<>如果你只打印输出,你可以考虑先保存结果。无论您现在多次循环$secondary_result,最终结果将始终与代码相同,第一个循环没有直接影响第二个结果的迹象

在这种情况下,这更有意义

$buffer = '';
while ($row2 = mysql_fetch_object($query2)) {
  $buffer .= "%".$row2->id."%";
}

while($row1 = mysql_fetch_object($query1)) {
    echo "*".$row1->id."*";
    echo $buffer;
}
但我真的不知道你为什么这么做。如果您正在执行嵌套循环,通常是因为第一个循环的结果会影响第二个循环

但我希望这有帮助

干杯

编辑

根据@Blazemonger关于展望未来的评论,PDO的等价物为:

当您使用PDO函数得到一个结果对象时,您会这样循环:

         Current Item
          |
          v
[1][2][3][4]
mysql_fetch_object() ~
  1. Get the next time
  2. Uh, there are no more objects because we're at item [4].  Return done.
while($row1 = mysql_fetch_object($query1)) {
   echo "*".$row1->id."*";
   while ($row2 = mysql_fetch_object($query2)) {
      echo "%".$row2->id."%";
   }
   // put the result pointer back to the front
   mysql_data_seek($query2, 0)
}
while($row1 = $query1->fetch_object()) {
   echo "*".$row1->id."*";
   while ($row2 = $query1->fetch_object()) {
      echo "%".$row2->id."%";
   }
   // put the result pointer back to the front
   $query2->data_seek(0);
}

上面的示例显示了MySqli的fetch对象和指针重置版本。

我知道要移动到PDO,这将尽快完成。我知道要移动到PDO,这将尽快完成。展望未来,PDO等价物是什么?展望未来,PDO等价物是什么?