How can SQLite be utilized as an alternative to storing IP address data in a CSV file using PHP?

Storing IP address data in a CSV file can be inefficient and cumbersome to manage. Using SQLite as an alternative allows for easier querying and manipulation of the data. To implement this, you can create a SQLite database table to store the IP address data and use PHP to insert, update, or retrieve the data as needed.

// Connect to SQLite database
$db = new SQLite3('ip_addresses.db');

// Create table to store IP addresses if it doesn't exist
$db->exec('CREATE TABLE IF NOT EXISTS ip_addresses (id INTEGER PRIMARY KEY, ip_address TEXT)');

// Insert IP address into the database
$ip = $_SERVER['REMOTE_ADDR'];
$db->exec("INSERT INTO ip_addresses (ip_address) VALUES ('$ip')");

// Retrieve IP addresses from the database
$results = $db->query('SELECT * FROM ip_addresses');
while ($row = $results->fetchArray()) {
    echo $row['ip_address'] . "\n";
}

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