I believe that you confuse the questions (inadvertently) due to the use of the word "string". Judging by your example, you mean a field / column. It looks like you want to specify the fields to select using a variable that can be executed by any of these methods ...
$fields = "name, age"; $sql = "SELECT $fields FROM table"; $sql = "SELECT {$fields} FROM table"; $sql = "SELECT ".$fields." FROM table";
NB it is important that you have a safe date in the $ fields element, I would suggest using a whitelist of valid values i.e.
// assuming $_POST['fields'] looks something like array('name','age','hack'); $allowed = array('name', 'age'); $fields = array(); foreach ($_POST['fields'] as $field) { if (in_array($field, $allowed)) { $fields[] = $field; } $fields = implode(', ', $fields);
source share