# `mysqli` Example: Use `foreach` to Iterate Result Set
```
  <?php
  echo "mysqli Example" . "\n";

  mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

  try {
    $mysqli = new mysqli("127.0.0.1", "root", "", "universe", 4000);
    if ($mysqli->connect_errno) {
      die("Error connecting: " . $mysqli->connect_error);
    }
    $res = $mysqli->query("SELECT id, name, mass FROM planets");
    if (!$res) {
      echo "Query failed: " . $mysqli->error;
    } else {
      ~!@foreach ($res as $row)@!~ {
        echo $row['id'] . ' ' . $row['name'] . ' ' . $row['mass'] . "\n";
      }
    }
  } catch (Exception $e) {
    die($e->getMessage());
  }
  ?>
```
-----------------------------------------------------------------------
The program iterates through every rows in the result set using a `foreach` loop.

Example:
```
<html>

<body>
  <h1>201.4-PHP Connection 10</h1>
  <?php
  echo "mysqli Example - foreach" . "<p>";

  mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

  try {
    $mysqli = new mysqli("127.0.0.1", "root", "", "universe", 4000);
    if ($mysqli->connect_errno) {
      die("Error connecting: " . $mysqli->connect_error);
    }
    $res = $mysqli->query("SELECT id, name, mass FROM planets");
    if (!$res) {
      echo "Query failed: " . $mysqli->error;
    } else {
      foreach ($res as $row) {
        echo $row['id'] . ' ' . $row['name'] . ' ' . $row['mass'] . "<p>";
      }
    }
    print "End.";
  } catch (Exception $e) {
    die($e->getMessage());
  }
  ?>

</body>

</html>
```
