How can one efficiently store search keywords in a database using PHP?
To efficiently store search keywords in a database using PHP, you can create a table in your database to store the keywords along with a timestamp. When a user performs a search, you can insert the search keyword along with the current timestamp into the database. This will allow you to track the most popular search terms and analyze search trends over time.
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "search_keywords";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get search keyword from user input
$search_keyword = $_GET['keyword'];
// Insert search keyword into database with current timestamp
$sql = "INSERT INTO keywords (keyword, timestamp) VALUES ('$search_keyword', NOW())";
if ($conn->query($sql) === TRUE) {
echo "Search keyword stored successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// Close database connection
$conn->close();