的參數挑選我想不通爲什麼排序,因爲我沒有使用$排序作爲參數傳遞將只要工作。下面的例子將分揀工作:PHP SQLSRV與預處理語句
$sort = "quantity desc";
$sql = " with items as (
SELECT i.[item_id]
,i.[name]
,i.[value]
,i.[quantity]
,i.[available]
,isnull(r.awarded, 0) as awarded
, ROW_NUMBER() OVER(
ORDER BY $sort
) rowNumber
FROM [Intranet].[dbo].[Goodwell_Item] i
LEFT JOIN (
SELECT r.item_id
, COUNT(1) awarded
from [Intranet].[dbo].[Goodwell_Reward] r
group by r.item_id
) as r
ON i.item_id = r.item_id
)
SELECT *
FROM items
WHERE rowNumber BETWEEN (?) and (?)
and ((?) = '' OR (available = (?)))
";
$params = array($pagify['startFrom'], $end, $available, $available);
$stmt = sqlsrv_query($conn, $sql, $params);
但是,如果我改變符合ORDER BY到:
ORDER BY (?)
,並把它添加到我的$ PARAMS像這樣:
$params = array($sort, $pagify['startFrom'], $end, $available, $available);
然後由於某種原因的排序被忽略。
請告訴我如何得到排序的方式,不允許SQL注入工作。