【发布时间】:2021-03-27 18:02:14
【问题描述】:
我正在尝试将 WHERE IN 语句合并到使用 PDO 的已经工作的查询中。我已经使用索引数组的计数遍历并动态创建了位置占位符,并循环遍历所述数组以绑定每个值。然而,当这一切都说完了,我只得到一个结果,特别是与我动态绑定的最后一个值匹配的结果。
此外,如果我简单地将 $arrayToCheck 内爆,用逗号作为分隔符代替 $placeholderSpots,我会得到我所期望的全部 24 个结果。
$arrayToCheck = json_decode($listFromDatabase); //This is a Python List converted to a string using Python's json.dumps, which was then passed into a database table.
$timePeriod = 0;
$rowLimit = 2500;
$placeholderSpots = implode(", ", array_fill(0, count($arrayToCheck), "?"));
$toPull = $GLOBALS['MainDatabase']->prepare("SELECT * FROM datatable WHERE timecolumn >= ? AND idcolumn IN ($placeholderSpots) ORDER BY timecolumn DESC LIMIT ?");
$toPull->bindParam(1, $timePeriod, PDO::PARAM_INT);
$tempCounter = 2;
foreach ($arrayToCheck as $eachID) {
$toPull->bindParam($tempCounter, $eachID, PDO::PARAM_INT);
$tempCounter++;
}
$toPull->bindParam($tempCounter, $rowLimit, PDO::PARAM_INT);
if ($toPull->execute()) {
while ($pulledData = $toPull->fetch(PDO::FETCH_ASSOC)) {
//Data Is Processed Here
}
}
【问题讨论】:
-
我的意思是,如果你已经是 bindParam,为什么不绑定
$placeHolderSpots呢?您是否回复了$placeHolderSpots以确保您得到了您的预期? -
@Jimithus 我可能会误解,但我认为您不能使用绑定生成位置占位符。是的,我已经确认准备语句具有正确数量的占位符。不幸的是,我看不到最终语句的样子,但没有错误确认正在传入正确数量的参数,并且 foreach 循环中的
$eachID回显表明它符合预期。