How can PHP be used to store web addresses in a database and display them as links?
To store web addresses in a database using PHP, you can create a table with a column to store the URLs. To display them as links, you can fetch the URLs from the database and use the anchor tag in HTML to create clickable links.
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
// Fetch URLs from the database
$sql = "SELECT url FROM urls";
$result = $conn->query($sql);
// Display URLs as links
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<a href='" . $row["url"] . "'>" . $row["url"] . "</a><br>";
}
} else {
echo "0 results";
}
// Close the database connection
$conn->close();