How can PHP be used to retrieve navigation data from a database for a website?

To retrieve navigation data from a database for a website using PHP, you can create a PHP function that connects to the database, queries the navigation data, and returns the results. This function can be called in your website template to dynamically generate the navigation menu based on the data retrieved from the database.

<?php
// Function to retrieve navigation data from the database
function getNavigationData() {
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "database";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Query to retrieve navigation data
    $sql = "SELECT * FROM navigation_menu";
    $result = $conn->query($sql);

    // Check if there are results
    if ($result->num_rows > 0) {
        // Output data of each row
        while($row = $result->fetch_assoc()) {
            echo '<a href="' . $row["url"] . '">' . $row["title"] . '</a>';
        }
    } else {
        echo "0 results";
    }

    $conn->close();
}
?>