我在我的SQL的ORDER BY
部分使用params時遇到了問題。它不會發出任何警告,但不會打印任何內容。如何使用準備好的PDO語句設置ORDER BY參數?
$order = 'columnName';
$direction = 'ASC';
$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->bindParam(':order', $order, PDO::PARAM_STR);
$stmt->bindParam(':direction', $direction, PDO::PARAM_STR);
$stmt->execute();
的:my_param
作品,但不:order
或:direction
。它是否沒有正確內部轉義?我堅持直接在SQL中插入它?像這樣:
$order = 'columnName';
$direction = 'ASC';
$stmt = $db->prepare("SELECT * from table WHERE column = :my_param ORDER BY $order $direction");
是否有PDO::PARAM_COLUMN_NAME
常數或一些等價物?
謝謝!
另請參見[可以PHP PDO語句接受表名作爲參數?](http://stackoverflow.com/q/182287/157957) – IMSoP 2013-09-01 22:51:25