I have a separate custom table in my database for each language I'm providing for my website. I need to select the correct table dynamically from a variable that is sent from a POST request.
First I was trying to to use $wpdb->prepare but then I read that it can't really handle the name of the table since it will force it into a string.
The other solutions that I saw looked something like this:
$foods = $wpdb->get_results("SELECT * FROM $sanitized_search_language WHERE dbID = $sanitized_search_text", ARRAY_A);
That will give me the following error:
"<div id=\"error\"><p class=\"wpdberror\"><strong>WordPress database error:</strong> [You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE dbID = 719' at line 1]<br /><code>SELECT * FROM WHERE dbID = 719</code></p></div>[]"
I also saw the variables wrapped in curly brackets in some solutions like this:
$foods = $wpdb->get_results("SELECT * FROM {$sanitized_search_language} WHERE dbID = {$sanitized_search_text}", ARRAY_A);
But that is also giving me an error.
How can I select the custom table I want to query based on a dynamic variable?