Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于两个数组数据选择查询时如何避免循环_Php_Mysql - Fatal编程技术网

Php 基于两个数组数据选择查询时如何避免循环

Php 基于两个数组数据选择查询时如何避免循环,php,mysql,Php,Mysql,我有一张像下面这样的桌子 id supplier_id item_id minimum_order_qty 1 6 1 500 2 4 1 300 3 2 2 200 4 3 2 100 5 4 3 250 6 5 5

我有一张像下面这样的桌子

id  supplier_id item_id minimum_order_qty   
1      6           1         500 
2      4           1         300 
3      2           2         200 
4      3           2         100 
5      4           3         250 
6      5           5         100 
7      7           6         1000    
8      8           6         600 
9      9           7         700 
10     1           7         500 
11     7           8         1000    
12     9           9         700 
13     2           10        500 
14     9           10        600 
每个
项目\u id
可以有多个供应商(
供应商\u id

我有两个数组
项目id
数组和
供应商id
数组

item\u id
数组:

Array
    (
        [0] => 9
        [1] => 10
        [2] => 11
        [3] => 12
        [4] => 13
        [5] => 14
        [6] => 15
        [7] => 16
        [8] => 17
        [9] => 18
        [10] => 19
        [11] => 20
        [12] => 21
        [13] => 22
        [14] => 23
        [15] => 24
    )
Array
        (
            [0] => 9
            [1] => 2
            [2] => 5
            [3] => 1
            [4] => 1
            [5] => 9
            [6] => 6
            [7] => 4
            [8] => 6
            [9] => 9
            [10] => 1
            [11] => 9
            [12] => 9
            [13] => 4
            [14] => 5
            [15] => 9
        )
supplier\u id
数组:

Array
    (
        [0] => 9
        [1] => 10
        [2] => 11
        [3] => 12
        [4] => 13
        [5] => 14
        [6] => 15
        [7] => 16
        [8] => 17
        [9] => 18
        [10] => 19
        [11] => 20
        [12] => 21
        [13] => 22
        [14] => 23
        [15] => 24
    )
Array
        (
            [0] => 9
            [1] => 2
            [2] => 5
            [3] => 1
            [4] => 1
            [5] => 9
            [6] => 6
            [7] => 4
            [8] => 6
            [9] => 9
            [10] => 1
            [11] => 9
            [12] => 9
            [13] => 4
            [14] => 5
            [15] => 9
        )
两个数组的长度相同。我想基于这两个数组从
supplier\u item
中选择
minimum\u order\u qty
。通常,我必须像这样选择内部循环:

$item_count = count($item);
$sup_count = count($supp_id);
for($i=0; $i<$item_count; $i++) {
    $itm_id = $item[$i];
    $s_id = $supp_id[$i];
    $sql = "select * from supplier_item where item_id=$itm_id and supplier_id=$s_id";
    $result[] = $this->query($sql);
}
$item\u count=计数($item);
$sup\u count=计数($supp\u id);
对于($i=0;$iquery($sql);
}

但是,上面的代码将多次运行查询。我不想这样做。那么,有没有其他方法可以通过单个查询选择数据?

您可以在一个查询中完成此操作

$item_count = count($item);
$sup_count = count($supp_id);
$predicates = [];
for($i=0; $i<$item_count; $i++) {
    $itm_id = $item[$i];
    $s_id = $supp_id[$i];
    $predicates[] = "(item_id=$itm_id and supplier_id=$s_id)";
}
$sql = "select * from supplier_item where " . implode(' OR ', $predicates);
$result = $this->query($sql);
$item\u count=计数($item);
$sup\u count=计数($supp\u id);
$predicates=[];
对于($i=0;$iquery($sql);

我们可以利用MySQL的扩展
WHERE IN
语法,并使用以下形式的查询:

SELECT *
FROM supplier_item
WHERE (item_id, supplier_id) IN ((9, 9), (10, 2), ...);
PHP代码:

$item_id = array(9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24);
$supplier_id = array(9,2,5,1,1,9,6,4,6,9,1,9,9,4,5,9);

$where = "(";
for ($i=0; $i < count($item_id); $i++) {
    if ($i > 0) $where .= ", ";
    $where .= "(" . $item_id[$i] . ", " . $supplier_id[$i] . ")";
}
$where .= ")";

$sql = "SELECT * FROM supplier_item WHERE (item_id, supplier_id) IN " . $where . ';';
$item_id=数组(9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24);
$supplier_id=数组(9,2,5,1,1,9,6,4,6,9,1,9,9,4,5,9);
$where=“(”;
对于($i=0;$i0)$where.=“,”;
$where.=“(“$item\u id[$i]”,“$supplier\u id[$i]”);
}
$where.=”;
$sql=“从“$WHERE.”中的供应商项目中选择*(项目id,供应商id);

重要注意事项:原始字符串连接可以为SQL注入打开大门。如果在生产中使用此答案,则仅当您已经对item和supplier ID数组进行了消毒,并且您确定它们只包含整数时,才应使用此答案。

此CakePHP与SQL注入有什么关系?ps,切勿在q中插入值直接像那样的质疑,那只是等待被利用的SQL注入漏洞-使用绑定!@ndm我只显示示例代码。因此,我不想添加细节。是示例代码造成了安全漏洞,IMHO应该避免发布这些漏洞,因为阅读它的人不一定知道这一点,并且被告知广告实践!