PHP connect to DB and check URLs : I try to build a PHP function to check if some URLs are still working of have expired. 

I managed to connect to my database, retrieve the list of URLs to check and use a check function I found on stackoverflow. However I have problems loop through all the URLs and write back the results to the database. 

Would be great if someone can help on that part in the end ("define URL" should loop through the list and "output" should write back the echo into the table). 

Here the php 

    <?php
    
    //DB Credentials
    $servername = "localhost";
    $username = "root";
    $password = "password";
    $dbname = "dbname";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
    //Retrieve list of URLs to check
    $sql = "SELECT product_id, link FROM link_check";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            echo "id: " . $row["product_id"]. " " . $row["link"]. "<br>";
        }
    } else {
        echo "0 results";
    }
    
    //Function to check URLs
    function check_url($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        $headers = curl_getinfo($ch);
        curl_close($ch);
    
        return $headers['http_code'];
    }
    
    //Define URLs
    $sql = "SELECT link FROM link_check";
    $url = $conn->query($sql);
    
    //Output
    $check_url_status = check_url($url);
    if ($check_url_status == '200')
       echo "Link Works";
    else
       echo "Broken Link";
    
    $conn->close();
    ?>

This question is related to this old post: [Check Link URL][1]


  [1]: http://stackoverflow.com/questions/15770903/check-if-links-are-broken-in-php