What are the best practices for handling language translations and search queries in a MySQL database using PHP?

When handling language translations and search queries in a MySQL database using PHP, it's important to properly sanitize user input to prevent SQL injection attacks. One way to achieve this is by using prepared statements with placeholders for user input. Additionally, storing translations in a separate table with language codes can help efficiently retrieve and display the correct translations based on the user's language preference.

// Establish a database connection
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');

// Prepare a SQL query with a placeholder for user input
$stmt = $pdo->prepare("SELECT translation FROM translations WHERE keyword = :keyword AND language_code = :language_code");

// Bind the user input to the placeholders
$stmt->bindParam(':keyword', $_POST['keyword']);
$stmt->bindParam(':language_code', $_POST['language_code']);

// Execute the query
$stmt->execute();

// Fetch the translation result
$translation = $stmt->fetchColumn();

// Display the translation
echo $translation;