How can PHP be used to securely store and retrieve user-specific website URLs from a MySQL database?

To securely store and retrieve user-specific website URLs from a MySQL database, you can use PHP prepared statements to prevent SQL injection attacks. Make sure to sanitize user input and validate URLs before storing them in the database. When retrieving URLs, use prepared statements to prevent SQL injection attacks and validate the URLs before displaying them to the user.

<?php
// Establish database connection
$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);
}

// Store URL in database
$stmt = $conn->prepare("INSERT INTO user_urls (user_id, url) VALUES (?, ?)");
$stmt->bind_param("is", $user_id, $url);

$user_id = 1; // User ID of the current user
$url = "https://www.example.com";
$stmt->execute();

// Retrieve URLs from database
$stmt = $conn->prepare("SELECT url FROM user_urls WHERE user_id = ?");
$stmt->bind_param("i", $user_id);

$user_id = 1; // User ID of the current user
$stmt->execute();
$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
    echo $row['url'] . "<br>";
}

// Close connection
$stmt->close();
$conn->close();
?>