0

How to apply PDO method in ORDER BY and LIMIT clauses? DO NOT use bindValue.

How to get Select to Order and Limit via $_Get(s)?

I don't want to use bindValue command, what's the best way to do this?

//-- Exemple return $_GET -- //
  $getOrder = 'ASC';
  $getLimit = 5;
  $orderColun = 'id';
//---------------------- //

   
    $order =  $getOrder == 'ASC' ? 'ORDER BY ? ASC' : 'ORDER BY ? DESC' ;
    $limit = $getLimit != ''? intval($getLimit) : 0;


    $sql  = "SELECT * FROM test $order LIMIT ?";
    $stmt  = $PDO->prepare($sql);
    $stmt->execute([$orderColun, $limit]);
    $return = $stmt->fetchAll(PDO::FETCH_ASSOC);
  • There is NO PDO method for ORDER BY clause. – Your Common Sense May 16 '22 at 16:08
  • After a lot of research I found a solution, it may be that some people are looking for this answer: Not really possible with plain PDO unless you use ordinal column sorting. That is, pass 1 to sort by the first column, 2 to sort by the second, etc... you get the same result. – Kleuton Novais May 16 '22 at 17:09

0 Answers0