【问题标题】:Mysqli prepared statement : several WHERE clauses and WHERE IN (Array) [duplicate]Mysqli 准备好的语句:几个 WHERE 子句和 WHERE IN (Array)
【发布时间】:2015-03-16 17:27:17
【问题描述】:

我需要运行下面的查询。它给我带来了很多麻烦。事实上,我有几个“WHERE”条件,其中一个需要分解一个数组。

This issue 帮助了我,但它没有几个条件“WHERE”。

$array = (1,2,3,4,5,6,7,8,9,10);

$clause = implode(',', array_fill(0, count($array), '?'));

if($request = $this->getConnexion()->prepare('SELECT col1, col2 FROM table WHERE col1 IN ('.$clause.') AND col2>=?') or die(mysqli_error($this->getConnexion()))) {

    // The problem starts here
    call_user_func_array(array($request, 'bind_param'), $array);

    $request->bind_param('i', $this->getTime());
    // Until here

    $request->execute();
    $request->bind_result($col1, $col2);
    $request->store_result();

    // Following the code

}

【问题讨论】:

  • 您可以在使用call_user_func_array 之前将$this->getTime() 添加到数组的末尾,或者使用i 构建另一个array_fill,然后在bind_param 中内爆
  • 您的问题是什么?看起来您应该在$clause 数组中再添加一个?,而不是对bind_param() 进行额外调用,并将$this->getTime() 的值添加到$array 值数组中,所以您只是在处理一个参数数组涵盖IN () 子句和另一个条件。
  • @Michael Berkowski,请发表你的答案,我不明白你在想什么:)
  • 好的,等等——我把上面关于$clause的部分说错了......

标签: php mysql mysqli


【解决方案1】:

这里重要的是你只调用一次bind_param(),数组包含所有你需要绑定的参数,所以你的解决方案是添加额外的WHERE 子句参数到要绑定的值的 $array 上。 IN() 子句不是需要将call_user_func_array() 与其他参数分开的特殊情况。你在他们所有上调用它。

虽然缺少一些东西 - bind_param() 的第一个参数是一个数据类型的字符串。你所有的类型都是i,所以你需要使用str_repeat()来创建它。

// Eventually, this array will contain the other params too
$array = (1,2,3,4,5,6,7,8,9,10);

// This creates a string of ?,?,?,?... for the IN () clause    
$clause = implode(',', array_fill(0, count($array), '?'));

// Add the additional value onto the $array array
// so the last param is bound with the others.
$array[] = $this->getTime();

$types = str_repeat('i', count($array));

// The params passed to call_user_func_array() must have as references, each subsequent value. Add those in a loop, but start with the $types string
$params = array($types);
foreach ($array as $key => $value) {
   $params[] = &$array[$key];
}

if($request = $this->getConnexion()->prepare('SELECT col1, col2 FROM table WHERE col1 IN ('.$clause.') AND col2>=?') or die(mysqli_error($this->getConnexion()))) {

    // Then bind_param() is called on all parameters
    // using the $params array which includes types and param references
    call_user_func_array(array($request, 'bind_param'), $params);

    // Execute & fetch.
    $request->execute();
    $request->bind_result($col1, $col2);
    $request->store_result();

    // Following the code
}

【讨论】:

  • 多么愚蠢,我根本没想到!谢谢你。我测试并验证了你的答案
  • @zlen 不傻。 MySQLi 与call_user_func_array() 结合起来确实很复杂。如果不去查的话,我永远都记不住该怎么做,而且示例通常没有很好的评论来理解实际在做什么。
  • 我现在有这个错误:“mysqli_stmt::bind_param() 的参数 2 应该是一个参考,”我发现了上面的问题,但这不回答我:stackoverflow.com/questions/2045875/…跨度>
  • @zlen 好的,应该够了。我忽略了很大一部分,实际上是创建引用数组
  • 确实,在您编辑帖子之前,我明白问题出在参考文献上,所以是“i”号。你的解释很好。我测试一下
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-06
  • 2018-03-08
  • 1970-01-01
  • 1970-01-01
  • 2013-04-21
  • 1970-01-01
  • 2020-12-08
相关资源
最近更新 更多