How can PHP be used to display the number of times a specific link has been clicked on a webpage?

To display the number of times a specific link has been clicked on a webpage using PHP, you can store the click count in a database or a file. Each time the link is clicked, increment the click count in the database or file. Then, retrieve and display the click count on the webpage where the link is located.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "click_tracker";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Update click count in the database when link is clicked
if(isset($_GET['link_id'])) {
    $link_id = $_GET['link_id'];
    $sql = "UPDATE links SET click_count = click_count + 1 WHERE id = $link_id";
    $conn->query($sql);
}

// Retrieve click count from the database
if(isset($_GET['link_id'])) {
    $link_id = $_GET['link_id'];
    $sql = "SELECT click_count FROM links WHERE id = $link_id";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        echo "This link has been clicked " . $row['click_count'] . " times.";
    } else {
        echo "0 clicks";
    }
}

// Close database connection
$conn->close();