What are the best practices for generating navigation menus from a MySQL database in PHP?

When generating navigation menus from a MySQL database in PHP, it is important to retrieve the necessary data from the database, loop through the results, and dynamically create the menu items. It is also important to properly structure the database table to store the menu items and their relationships.

<?php
// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Query to retrieve menu items from database
$query = "SELECT * FROM menu_items";
$result = mysqli_query($connection, $query);

// Check if query was successful
if ($result) {
    // Loop through results and create navigation menu
    echo '<ul>';
    while ($row = mysqli_fetch_assoc($result)) {
        echo '<li><a href="' . $row['url'] . '">' . $row['title'] . '</a></li>';
    }
    echo '</ul>';
} else {
    echo "Error: Unable to retrieve menu items.";
}

// Close database connection
mysqli_close($connection);
?>