I need to parameterize both the ORDER BY column name and DESC or ASC keyword as a variable. I have it working already but the variables are in the query, which is not safe.
$fruitColumn='bananas'
$sortbyORDER= 'DESC'
$fruitSearch = $db->prepare("
SELECT * FROM fruits
ORDER BY $fruitColumn $sortbyORDER");
$fruitSearch ->execute();
BUT - I need the variables bound in the execution for security.
$fruitSearch = $db->prepare("
SELECT * FROM fruits
ORDER BY ? ?");
$fruitSearch ->execute([$fruitType,$sortbyORDER]);
This doesn't seem to work as the two ?s give me an error.
I have also tried combining the two variables into one eg. $order = ".$fruitColumn." ".$sortbyORDER;
Is this possible? Thanks in advance