What are the potential advantages and disadvantages of using a database for storing language translations in PHP?
Issue: Storing language translations in a database in PHP can provide a centralized and easily manageable way to handle translations for multilingual websites. However, it can also introduce additional complexity and potential performance issues compared to other methods like using language files.
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "translations";
$conn = new mysqli($servername, $username, $password, $dbname);
// Query the database for translations
$sql = "SELECT translation FROM translations_table WHERE language = 'en' AND key = 'hello'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["translation"];
}
} else {
echo "Translation not found";
}
$conn->close();