Error Invalid argument in foreach

0
<?php


if (isset($_POST['sendRecover'])) {
    $recover = strip_tags(trim($_POST['email']));
    $readRec = read($link, 'up_users', "WHERE email = '$recover'");

    if (!$readRec) {
        $rc = mysqli_fetch_array($readRec) >= 1;
        echo '<span class="ms no">Erro: Email Inválido!</span>';
    } else {
        foreach ($readRec as $rec) {
            if ($rec['nivel'] == 1 || $rec['nivel'] == 2) {
              }
?>

I do not know why this error occurred:

  

Warning: Invalid argument supplied for foreach () in D: \ wamp64 \ www \ COURSES \ project_pro_php \ admin \ reminder_password.php on line 15

There is a mysqli query before it comes from a function but the select is right, I already tested it, my syntax is wrong and I did not get it.

    
asked by anonymous 09.08.2017 / 02:12

2 answers

2

Update
First, keep in mind that a warning is not an error.
What might have generated this warning is that foreach has in its syntax the format:

foreach (array_expression as $value)

Ensure that the read() function returns an array for $readRec .

Read more here in the PHP manual.

    
09.08.2017 / 02:26
0

It is always valid to check the variable that goes to the foreach, we always have to maintain the integrity of the data, ensuring that even if it does not receive anything, always be in agreement with the structure of the function, in your case, simply check if it is a array valid:

if ( !is_array($readRec) || count($readRec) < 1 ) {
    // Não é um array válido ou não possui conteúdo.
}
    
09.08.2017 / 02:40