I'm trying to find records from a table based on a variable that is grabbed from user submission. This is my query:
$field = t('field_data_' . variable_get('tabbed_content_field'));
$fieldValue = t(variable_get('tabbed_content_field') . '_value');
$query = db_query('SELECT :fieldValue FROM {:field}', array(':field' => $field, ':fieldValue' => $fieldValue));
However, whenever I run this query I get the following error:
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''field_data_field_news_release_date'' at line 2: SELECT :fieldValue FROM {:field}; Array ( [:field] => field_data_field_news_release_date [:fieldValue] => field_news_release_date_value )
I can retrieve the records successfully with:
$query = db_query('SELECT ' . $fieldValue . ' FROM { ' . $field . '}');
However, as I understand it this would be insecure.
Does anyone know why the first query won't work?
t()
for strings that don't need to be translated. The name of a database table, or a database field is not something that needs to be translated; the "node" table is always the "node" table, independently from the language currently enabled for the site.