How can PHP be used to retrieve URLs from a database and insert them into HTML code?

To retrieve URLs from a database and insert them into HTML code using PHP, you can first query the database to fetch the URLs, store them in an array, and then loop through the array to dynamically insert the URLs into the HTML code using PHP echo statements.

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

// Check connection
if (!$connection) {
    die("Connection failed: " . mysqli_connect_error());
}

// Query to retrieve URLs from database
$query = "SELECT url FROM urls_table";
$result = mysqli_query($connection, $query);

// Check if query was successful
if (mysqli_num_rows($result) > 0) {
    // Store URLs in an array
    $urls = array();
    while ($row = mysqli_fetch_assoc($result)) {
        $urls[] = $row['url'];
    }

    // Insert URLs into HTML code
    foreach ($urls as $url) {
        echo "<a href='$url'>$url</a><br>";
    }
} else {
    echo "No URLs found in the database.";
}

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