How can PHP be used to efficiently retrieve and store country-specific click data in a normalized database structure?

To efficiently retrieve and store country-specific click data in a normalized database structure, we can use PHP to create a database table with columns for country, clicks, and any other relevant data. We can then use PHP to query the database for specific country data and update the click count accordingly.

<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "click_data";

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

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

// Retrieve click data for a specific country
$country = "USA";
$sql = "SELECT clicks FROM click_data WHERE country = '$country'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Update click count for the specific country
    $row = $result->fetch_assoc();
    $clicks = $row['clicks'] + 1;
    $sql = "UPDATE click_data SET clicks = $clicks WHERE country = '$country'";
    $conn->query($sql);
} else {
    // Insert new record if country data doesn't exist
    $sql = "INSERT INTO click_data (country, clicks) VALUES ('$country', 1)";
    $conn->query($sql);
}

$conn->close();
?>