gpt4 book ai didi

php - when in 从句与 mysql pdo

转载 作者:行者123 更新时间:2023-11-29 05:21:37 24 4
gpt4 key购买 nike

我想在 mysql pdo 中使用“where in”子句运行一个简单的选择查询。

$开始= 12;$end = 14;

for($i=$start; $i<=$end;$i++)
{
$limitin[] = array($qstep,$i);
}

数组是

Array
(
[0] => Array
(
[0] => 3
[1] => 12
)

[1] => Array
(
[0] => 3
[1] => 13
)

[2] => Array
(
[0] => 3
[1] => 14
)

)

然后

$questionmarks = str_repeat("?,", count($limitin)-1) . "?";

查询是

$getans = $this->db->prepare("SELECT * 
FROM answers
WHERE qstep = ?
and ansid in ($questionmarks) ");


$getans->execute($limitin); //$limitin is the array.

我得到一个空白的结果集和一个通知,它是“通知:数组到字符串的转换”

最佳答案

$limitin 是一个多维数组,不是在 $getans->execute($limitin); 中使用的有效数组。您需要创建一个数组。尝试类似 ->

$inarray=array();

foreach($limitin as $key=>$val){
if($key==0) {$inarray[]=$val[0];} // set the qstep as the 1st array value
$inarray[] = $val[1]; // add each value
}

$questionmarks = str_repeat("?,", count($limitin)-1) . "?";

$getans = $this->db->prepare("SELECT *
FROM answers
WHERE qstep = ?
and ansid in ($questionmarks) ");


$getans->execute($inarray);

编辑

正如@Fred-ii- 所指出的,bindParam() 是另一种方法,例如

$questionmarks = str_repeat("?,", count($limitin)-1) . "?";

$getans = $this->db->prepare("SELECT *
FROM answers
WHERE qstep = ?
and ansid in ($questionmarks) ");


foreach($limitin as $key=>$val){
if($key==0) {$getans->bindParam(1,$val[0]);} // set the qstep as the 1st array value
$getans->bindParam($key+2,$val[1]); // add each value
}

$getans->execute();

关于php - when in 从句与 mysql pdo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25007823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com