首次使用后丢失mysql结果资源

时间:2010-08-10 13:30:24

标签: php mysql

我有以下代码:(添加查询)

$weight_table = mysql_query ("SELECT * FROM ".$prefix."weight ORDER BY wlimit DESC");    
foreach ($weight as $area => $weight) {
        echo $weight;

        while ($weight_row = mysql_fetch_array($weight_table)) {
            print_r($weight_row);
            $limit = $weight_row['wlimit'];
            if ($weight < $limit) {
        $weight_level[$count] = $weight_row;
            }
        }
    }
    print_r($weight_level);

其中$ weight_table是一个mysql查询结果,$ weight是一个数组。

我的问题是$ weight_table似乎在第一个while循环之后到期,因此$ weight数组被有效地视为有一个项目。这是一个问题!

有人可以建议为什么php会在第一次使用后忘记mysql结果吗?

4 个答案:

答案 0 :(得分:2)

通过for循环的第一次迭代,你从mysql结果集中获取所有记录。你的内部while循环将一直运行,直到所有行都消失。你希望它拉多少行?

此外,你重新使用$ weight这是一个坏主意,可能会造成副作用。

foreach ($weight as $area => $wt) { 
   echo $wt; 

   while ($weight_row = mysql_fetch_array($weight_table)) { 
      print_r($weight_row); 
      $limit = $weight_row['wlimit']; 
      if ($wt < $limit) { 
         $weight_level[$count] = $weight_row; 
      } 
   } 

   mysql_data_seek($weight_table,0);
} 
print_r($weight_level); 

答案 1 :(得分:1)

您在foreach声明中覆盖$weight数组。 请改用$area => $_weight

答案 2 :(得分:0)

while ($weight_row = mysql_fetch_array($weight_table)) {
    print_r($weight_row);
    foreach ($weights as $area => $weight) {
        echo $weight;
       $limit = $weight_row['wlimit'];
       if ($weight < $limit) {
          $weight_level[$count] = $weight_row;
       }
   }
}
print_r($weight_level);

使用$ weight数组作为$ weights

答案 3 :(得分:0)

foreach ($weight as $area => $weight) {

你正在覆盖$height数组中的内容,这就是它在第一次循环运行后到期的原因。尝试使用另一个变量名称。