MSSQL php pdo分页,bindParam上有些​​不对劲

时间:2016-07-25 04:26:17

标签: php sql-server pdo

正常使用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时它不起作用,我错过了。

提前谢谢。

2 个答案:

答案 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注入。