How do I set ORDER BY params using prepared PDO statement?

后端 未结 8 2108
心在旅途
心在旅途 2020-11-22 15:37

I\'m having problems using params in the ORDER BY section of my SQL. It doesn\'t issue any warnings, but prints out nothing.

$order = \'column         


        
8条回答
  •  花落未央
    2020-11-22 16:06

    If I'm not entirely mistaken, Pascal is right.
    The only binding possible in PDO is the binding of values, as you did with the ':my_param' parameter.
    However, there's no harm done in:

    $stmt = $db->prepare("SELECT field from table WHERE column = :my_param ORDER BY ".$order ." ".$direction);
    $stmt->bindParam(':my_param', $is_live, PDO::PARAM_STR);
    $stmt->execute();
    

    The only thing to take notice of would be the correct escaping of $order and $direction, but since you set them manually and didn't set them via user input, I think you're all set.

提交回复
热议问题