-3

Iam trying to build a PHP function to check if some URLs are still working or have expired.

Would be great if someone can help me creating the loop functionality ("define URL" should loop through the list and "output" should write back the echo into the table).

Here is the php

$sql = "SELECT link FROM link_check";
$url = $conn->query($sql);

foreach ($sql as $url);
{
$check_url_status = check_url($url);
if ($check_url_status == '200')
   echo "Link Works";
else
   echo "Broken Link";
}
    ?>

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

Community
  • 1
  • 1
Karsten
  • 81
  • 1
  • 13
  • SO is a Q/A site, so it would be nice if asked a specific question not just list what you need help with. Also, you could share with us what you have tried. – Shadow Mar 05 '16 at 12:06
  • You need to place your `if/else` statement within your while loop iterating through the database results and then execute your `UPDATE` queries based on those conditions. Also, your second query under `//Define URLs` is unnecessary, and you're also using the result incorrectly. `$url` is the entire mysql result. – WheatBeak Mar 05 '16 at 12:07
  • 1
    Everything you need is in the examples in the documentation: http://php.net/manual/en/mysqli-stmt.fetch.php (or http://php.net/manual/en/pdostatement.fetch.php if you use PDO) – Gerald Schneider Mar 10 '16 at 09:00
  • I'm voting to close this question as off-topic because it merely asks for an example on using an undisclosed database library which can probably be found on the corresponding manual, thus is unlikely to help others. – Álvaro González Mar 10 '16 at 09:34

1 Answers1

1

You have to loop through the retreived recordset (query result):

   $SQL = "SELECT link FROM link_check";

   $rst = $conn->query($SQL);

   while($row = $rst->fetch_assoc())
   {
     $check_url_status = check_url($row['link']);
     echo ($check_url_status == '200') ? "Link Works" : "Broken Link";
   }
sbrbot
  • 6,169
  • 6
  • 43
  • 74
  • Thanks a lot. Exactly what I needed. Any idea how to write back the data into the database table lets say into column "URL_status"? – Karsten Mar 10 '16 at 09:21
  • Why `while()` and `fetch_assoc()` and not any other mechanism? We know nothing about the library used by the OP. – Álvaro González Mar 10 '16 at 09:35
  • Its obvious that OP is beginner in PHP programming, so that's the wanted and probably the simplest solution. The solution for database update would be to dynamically create UPDATE SQL (UPDATE link_check SET URL_status='OK' WHERE link='$link') and execute this query for each row (each pass through the loop). Again somebody would suggest using prepared statement (I would in other circumstances too, but not now). :-) – sbrbot Mar 10 '16 at 12:26