Php 在执行中使用绑定时PDO限制查询不起作用
我正在尝试让一个限制查询工作。到目前为止,我已经验证了以下内容: 所有输入都有效且正确。 手动运行MySQL时,查询可以工作 这些值被强制转换为INT。我甚至用INT值来替换变量Php 在执行中使用绑定时PDO限制查询不起作用,php,mysql,pdo,Php,Mysql,Pdo,我正在尝试让一个限制查询工作。到目前为止,我已经验证了以下内容: 所有输入都有效且正确。 手动运行MySQL时,查询可以工作 这些值被强制转换为INT。我甚至用INT值来替换变量 try { $sql = "SELECT * FROM sensor_data WHERE sid=:sid ORDER BY id DESC LIMIT :starting_limit, :recordlimit"; $core = CoreDB::g
try {
$sql = "SELECT * FROM sensor_data WHERE sid=:sid ORDER BY id DESC LIMIT :starting_limit, :recordlimit";
$core = CoreDB::getInstance();
$sth = $core->dbh->prepare($sql);
$sth->execute(array(':sid' => $sensor->getID(), ':starting_limit' => (int)0, ':recordlimit' => (int)10));
// $sth->execute(array(':sid' => $sensor->getID()));
$results = $sth->fetchAll();
var_dump($sth->rowCount());
if ($sth->rowCount() > 0) {
foreach($results as $row){
$id = $row['id'];
var_dump($id);
}
}
}
catch(Exception $e){
}
任何建议都值得赞赏我找到的唯一解决方案是手动绑定参数,而不是将它们放在execute方法的数组中
try {
$sql = "SELECT * FROM sensor_data WHERE sid=:sid ORDER BY id DESC LIMIT :starting_limit, :recordlimit";
$core = CoreDB::getInstance();
$sth = $core->dbh->prepare($sql);
$sth->bindValue(':sid', $sensor->getID(), PDO::PARAM_INT);
$sth->bindValue(':starting_limit', 0, PDO::PARAM_INT);
$sth->bindValue(':recordlimit', 10, PDO::PARAM_INT);
$sth->execute();
$results = $sth->fetchAll();
var_dump($sth->rowCount());
if ($sth->rowCount() > 0) {
foreach($results as $row){
$id = $row['id'];
var_dump($id);
}
}
}
catch(Exception $e){
}
如果有人知道如何让它在execute方法中工作,请让我知道,我会更新答案 定义“不工作”。您的
var\u转储输出是什么?如果我手动完成该查询,将0替换为:standing_limit,将10替换为:recordlimit,则可以在catch()
@obsidiange()中同时输出$sth
和$results
,并将行数为8的变量转储。当我像上面所示那样做时,我得到了0。可能是@smith的副本,我正在强制转换为int,并在execute方法中绑定,所以我本以为这会解决这个问题。我将使用bindValue重写,看看它是否解决了问题。