【问题标题】:when in clause with mysql pdomysql pdo 的 when in 子句
【发布时间】:2014-09-20 09:11:45
【问题描述】:

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

$start = 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.

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

【问题讨论】:

  • 试试这个,我想它已经问过了Where in With PDO
  • 谢谢,我现在就试试。
  • 获取无效参数号:混合,因为我也有 qstep
  • 您要插入的是什么,向我们展示一个示例,说明是什么为您的IN 生成了值
  • 另外,在打开连接后立即添加setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);,看看它是否产生任何东西。

标签: php mysql sql pdo


【解决方案1】:

$limitin 是一个多维数组,不是在$getans-&gt;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();

【讨论】:

  • 那么,如果不使用bindParam(),这将如何工作,或者我错过了什么?
  • @Fred-ii-PDO::execute() 可以选择将查询参数作为数组直接添加到execute() 中,无需bindParam() -> public bool PDOStatement::execute ([ array $input_parameters ] )::input_parameters An array of values with as many elements as there are bound parameters in the SQL statement being executed.
  • @Fred-ii- 它仅适用于 PDO,不适用于 mysqli。您对bindParam() 的建议可能更好,例如将数组放入execute() -> All values are treated as PDO::PARAM_STR。但由于 mysql 比较松散地比较值,('12','13','14') 在返回行时将被视为(12,13,14)
  • 由于 OP 使用整数,那么这将起作用。将等待得到 OP 的回复,看看给出了什么; 干杯
  • @Fred-ii- true,这就是为什么当我看到 OP 将$qstep 作为每个$limitin[] 的第一个值时,我在原始示例中做了if($key==0) {$inarray[]=$val[0];},并且if($key==0) {$getans-&gt;bindParam(1,$val[0]);} 在我编辑的示例中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-07
相关资源
最近更新 更多