Invalid argument for foreach

there is no issue with this query, as when I use print_r it prints out three different arrays, Im just wondering what Im missing here to get this foreach working, as at the moment Im getting an error...

Warning: Invalid argument supplied for foreach() in C:xampphtdocsmyshopadminaccount_list.php on line 11

<?php 

$account_list = "SELECT * FROM accounts";
$query = $connect->query($account_list);
while  ($final_result = $query->fetch_array(MYSQLI_ASSOC)) {
// echo '<pre>';
// print_r($final_result);
// echo '</pre>';
}
echo '<table><tbody>';
foreach ($final_result as $result) {
echo '<tr><td>'.$result['id'].'</td>
<td>'.$result['firstname'].'</td>
<td>'.$result['lastname'].'</td>
<td>'.$result['email'].'</td>
<td>'.$result['address'].'</td>';
}
echo '</body></table>';
?>

As it is, you're not calling foreach until after the query has completed, at which time $final_result is FALSE . foreach (false as $result) is, indeed, invalid.

You'll want to combine the two loops:

$account_list = "SELECT * FROM accounts";
$query = $connect->query($account_list);

echo '<table><tbody>';

$final_result = false;

while  ($result = $query->fetch_array(MYSQLI_ASSOC)) {
  // echo '<pre>';
  // print_r($final_result);
  // echo '</pre>';

  $final_result = $result;

  echo '<tr><td>'.$result['id'].'</td>
  <td>'.$result['firstname'].'</td>
  <td>'.$result['lastname'].'</td>
  <td>'.$result['email'].'</td>
  <td>'.$result['address'].'</td>';
}

echo '</body></table>';

$final_result不是一个数组,而是数组的一个元素,而不是只写 -

$final_result = $query->fetch_all(MYSQLI_ASSOC);
链接地址: http://www.djcxy.com/p/53060.html

上一篇: 为foreach()警告提供的参数无效

下一篇: foreach的参数无效