PHP - 将 PDO 与 IN 子句数组一起使用 [英] PHP - Using PDO with IN clause array

查看:34
本文介绍了PHP - 将 PDO 与 IN 子句数组一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PDO 执行带有 IN 子句的语句,该子句使用数组作为其值:

I'm using PDO to execute a statement with an IN clause that uses an array for its values:

$in_array = array(1, 2, 3);
$in_values = implode(',', $in_array);
$my_result = $wbdb->prepare("SELECT * FROM my_table WHERE my_value IN (".$in_values.")");
$my_result->execute();
$my_results = $my_result->fetchAll();


上面的代码工作得很好,但我的问题是为什么这不:


The above code works perfectly fine, but my question is why this doesn't:

 $in_array = array(1, 2, 3);
    $in_values = implode(',', $in_array);
    $my_result = $wbdb->prepare("SELECT * FROM my_table WHERE my_value IN (:in_values)");
    $my_result->execute(array(':in_values' => $in_values));
    $my_results = $my_result->fetchAll();

此代码将返回 my_value 等于 $in_array (1) 中第一项的项,但不返回数组中的其余项(2 和 3).

This code will return the item whose my_value equals the first item in the $in_array (1), but not the remaining items in the array (2, and 3).

推荐答案

PDO 不擅长处理这些事情.您需要动态创建一个带有占位符的字符串并将其插入到查询中,同时以通常的方式绑定数组值.使用位置占位符,它会是这样的:

PDO is not good with such things. You need to create a string with placeholders dynamically and insert it into the query, while binding array values the usual way. With positional placeholders it would be like this:

$in  = str_repeat('?,', count($in_array) - 1) . '?';
$sql = "SELECT * FROM my_table WHERE my_value IN ($in)";
$stm = $db->prepare($sql);
$stm->execute($in_array);
$data = $stm->fetchAll();

如果查询中有其他占位符,您可以使用以下方法(代码取自我的 PDO教程):

In case there are other placeholders in the query, you could use the following approach (the code is taken from my PDO tutorial):

您可以使用 array_merge() 函数将所有变量连接到一个数组中,以数组的形式添加其他变量,按照它们在查询中出现的顺序:

You could use array_merge() function to join all the variables into a single array, adding your other variables in the form of arrays, in the order they appear in your query:

$arr = [1,2,3];
$in  = str_repeat('?,', count($arr) - 1) . '?';
$sql = "SELECT * FROM table WHERE foo=? AND column IN ($in) AND bar=? AND baz=?";
$stm = $db->prepare($sql);
$params = array_merge([$foo], $arr, [$bar, $baz]);
$stm->execute($params);
$data = $stm->fetchAll();

如果您使用命名占位符,代码会稍微复杂一些,因为您必须创建命名占位符的序列,例如:id0,:id1,:id2.所以代码将是:

In case you are using named placeholders, the code would be a little more complex, as you have to create a sequence of the named placeholders, e.g. :id0,:id1,:id2. So the code would be:

// other parameters that are going into query
$params = ["foo" => "foo", "bar" => "bar"];

$ids = [1,2,3];
$in = "";
$i = 0; // we are using an external counter 
        // because the actual array keys could be dangerous
foreach ($ids as $item)
{
    $key = ":id".$i++;
    $in .= ($in ? "," : "") . $key; // :id0,:id1,:id2
    $in_params[$key] = $item; // collecting values into a key-value array
}

$sql = "SELECT * FROM table WHERE foo=:foo AND id IN ($in) AND bar=:bar";
$stm = $db->prepare($sql);
$stm->execute(array_merge($params,$in_params)); // just merge two arrays
$data = $stm->fetchAll();

幸运的是,对于命名占位符,我们不必遵循严格的顺序,因此我们可以按任意顺序合并数组.

Luckily, for the named placeholders we don't have to follow the strict order, so we can merge our arrays in any order.

这篇关于PHP - 将 PDO 与 IN 子句数组一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆