Loop through MySQLi results

suggest change

A while loop can walk a result. The fetch returns no row at the end, so the loop stops. Since PHP 8.1 that return value is null. Older versions returned false. Both are falsy, so the while condition is the same. These examples work with

Object oriented style

while($row = $result->fetch_assoc()) {
    var_dump($row);
}

Procedural style

while($row = mysqli_fetch_assoc($result)) {
    var_dump($row);
}

To get exact information from results, we can use:

while ($row = $result->fetch_assoc()) {
    echo 'Name and surname: '.$row['name'].' '.$row['surname'].'<br>';
    echo 'Age: '.$row['age'].'<br>'; // Prints info from 'age' column
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents