正常使用MsSQL
:
$ppage = 15;
$poset = 0;
$stmt = "SELECT * FROM tbl ORDER BY ID OFFSET {:$poset } ROWS FETCH NEXT {:ppage } ROWS ONLY";
$stmt = $this->conn->prepare($stmt);
$stmt->execute();
return $row = $stmt->fetchAll();
MsSQL
无法正常工作:
$ppage = 15;
$poset = 0;
$stmt = "SELECT * FROM tbl ORDER BY ID OFFSET :poffset ROWS FETCH NEXT :perpage ROWS ONLY";
$stmt = $this->conn->prepare($stmt);
$stmt->bindParam(':poffset', $poset);
$stmt->bindParam(':perpage', $ppage);
$stmt->execute();
return $row = $stmt->fetchAll();
查询很好,我用变量运行变量实际数据但是当我将变量设置为bindParam
时它不起作用,我错过了。
答案 0 :(得分:3)
请尝试使用$stmt = $this->conn->prepare($stmt);
$stmt->bindValue(':poffset', $poset, PDO::PARAM_INT);
$stmt->bindValue(':perpage', $ppage, PDO::PARAM_INT);
$stmt->execute();
:
{{1}}
答案 1 :(得分:3)
不是在bindParam()
函数的参数内使用execute()
函数,而是添加包含值的数组。
这样的事情:
$stmt = $this->conn->prepare($stmt);
$stmt->execute(array(':poffset' => $poset, ':perpage' => $ppage)); // using an array rather than the bindValue function.
正常情况下使用bindParam
功能,但请用逗号代替=>
。
这种做法可以让您不必为每个值调用bindParam()
函数& 仍然可以防止 SQL注入。