What is the purpose of using $stmt->fetch() in PHP prepared statements?

When using PHP prepared statements, $stmt->fetch() is used to retrieve the results of a query executed with the prepared statement. This method fetches the next row from the result set represented by the prepared statement object. It allows you to access the data returned by the query in a structured manner.

// Assuming $stmt is a prepared statement object
$stmt->execute();

// Bind variables to the result set
$stmt->bind_result($col1, $col2);

// Fetch the results
while ($stmt->fetch()) {
    // Access the data retrieved from the query
    echo $col1 . ' - ' . $col2 . '<br>';
}