Having problem with binding parameters to PDO statement. "orderBy" seems not to be passed to query and results are not ordered as suppose to. When I use "price" in query itself all is good but cannot bind paremeter via bindParam. The code is:
class Products
{
const ORDER_BY_NAME="name";
const ORDER_BY_PRICE_PER_UNIT="price_per_unit";
const ORDER_BY_PRICE="price";
const ORDER_BY_MINIMUM_QUANTITY="minimum_quantity";
// function returns array of all products
public function getAllProducts($orderBy)
{
$db=Registry::getVariable("db");
$pdoStatement=$db->prepare("SELECT name, minimum_quantity, price_per_unit, price, id FROM products ORDER BY :orderBy;");
$pdoStatement->bindParam(":orderBy",$orderBy,PDO::PARAM_STR);
$pdoStatement->execute();
return $pdoStatement->fetchAll(PDO::FETCH_ASSOC);
}
}
and later on calling
$products=new Products();
echo $products->getAllProducts(Products::ORDER_BY_PRICE);
why my bindParam(":orderBy",$orderBy,PDO::PARAM_STR);
dosen't seem to be used in query? any help would be much appreciated :)