How can you handle storing and retrieving hyperlinks in a database using PHP?
Storing and retrieving hyperlinks in a database using PHP involves properly escaping the URLs before inserting them into the database to prevent SQL injection attacks. When retrieving the URLs, they should be properly decoded to ensure they display correctly. One way to handle this is by using PHP's built-in functions like mysqli_real_escape_string() for storing and urldecode() for retrieving hyperlinks.
// Storing a hyperlink in the database
$link = "http://www.example.com";
$escapedLink = mysqli_real_escape_string($connection, $link);
$query = "INSERT INTO links_table (url) VALUES ('$escapedLink')";
mysqli_query($connection, $query);
// Retrieving a hyperlink from the database
$query = "SELECT url FROM links_table WHERE id = 1";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$decodedLink = urldecode($row['url']);
echo "<a href='$decodedLink'>Click here</a>";