Example 1 — Free After Reading Rows
Fetch product names, then free the result before closing the connection. Output is a fixed sample.
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT name FROM products LIMIT 2");
while ($row = mysqli_fetch_assoc($result)) {
echo $row["name"], PHP_EOL;
}
mysqli_free_result($result);
mysqli_close($mysqli); How It Works
- The query returns a mysqli_result.
- Rows are printed with fetch_assoc().
- mysqli_free_result() releases the result memory.
- mysqli_close() then closes the connection.

