How can PHP scripts be utilized to compare a URL with database records for link validity?
To compare a URL with database records for link validity, you can create a PHP script that queries the database for existing URLs and then compares the input URL with the records. If a match is found, the link is considered valid.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Input URL to check
$input_url = "https://www.example.com";
// Query the database for existing URLs
$sql = "SELECT * FROM links WHERE url = '$input_url'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "Link is valid";
} else {
echo "Link is not valid";
}
// Close the connection
$conn->close();
?>