문제

I tried to use mysqli_fetch_array twice in a code, and it seems not to work.

<?php 
$i=1; 
while ($i < $x)
{     
while($data=mysqli_fetch_array($result))   
  {     
   echo $i;
   echo $data['ID'];   
  }    
}    

while {$i > $x)
{     
 while($data=mysqli_fetch_array($result))   
 {     
  echo $i; 
  echo $data['ID2']    
 }    
}    
?>

This doesn't work for the second case. Why? Is there any way to get my idea to work? I want to use some of the array values in one place, and some in another

도움이 되었습니까?

해결책

After you fetch all rows the pointer is at the end of the result set and you cannot fetch more. You need to reset it to the first row before attempting to fetch from it again:

mysqli_data_seek($result, 0);

Alternately, the first time through assign rows to an array and use that later:

while($data=mysqli_fetch_array($result)) {     
   $rows[] = $data;   
}    

foreach($rows as $row) {
    //whatever
}

Depending on what you are doing not a great way to split up rows, but based on your comments (you have to increment your counter):

$i=1; 
while($data=mysqli_fetch_array($result) && $i < 11) {     
   echo $data['ID'];   
   $i++;
}    

while($data=mysqli_fetch_array($result) && $i < 21) {     
   echo $data['ID2'];
   $i++;    
}    

다른 팁

Where are you resetting the vars, also you should do the while loop like this:

while ( ($i < $x) || ($data=mysqli_fetch_array($result) ) )
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top