Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.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-将数组值传递给mysql查询并获取结果_Php_Mysql_Codeigniter - Fatal编程技术网

PHP-将数组值传递给mysql查询并获取结果

PHP-将数组值传递给mysql查询并获取结果,php,mysql,codeigniter,Php,Mysql,Codeigniter,我有上面的数组,我需要从中获取每个sr_no、batch_id,并需要检查表(part_列表)中是否存在组合(例如,第一行sr_no=>71&batch_id=>5)。我已经尝试了下面的代码,但它正在获取所有。如果有更好的方法,也可以实施 $dataSet = Array ( [0] => Array ( [quantity] => 22 [sr_no] => 71 [batch_id] =>5 [inq_id] => 91 ) [1] =>

我有上面的数组,我需要从中获取每个sr_no、batch_id,并需要检查表(part_列表)中是否存在组合(例如,第一行sr_no=>71&batch_id=>5)。我已经尝试了下面的代码,但它正在获取所有。如果有更好的方法,也可以实施

$dataSet = Array ( 
    [0] => Array ( [quantity] => 22 [sr_no] => 71 [batch_id] =>5 [inq_id] => 91 ) 
    [1] => Array ( [quantity] => 35 [sr_no] => 72 [batch_id] => [inq_id] => 92 ) 
    [2] => Array ( [quantity] => 20 [sr_no] => 69 [batch_id] => [inq_id] => 90 ) 
    [3] => Array ( [quantity] => 45 [sr_no] => 75 [batch_id] => 6 [inq_id] => 94 ) 
    [4] => Array ( [quantity] => 15 [sr_no] => 70 [batch_id] => 6 [inq_id] => 90 ) 
)

提前谢谢。

据我所知,您想检查
sr\u no
batch\u id
。 如果您愿意使用原始SQL查询,您可以首先将两列合并,然后为该合并列提供数组并找到所需的结果

请试试这个

$keyedExistingPartRows = array_column(
    $this->db
        ->where_in('batch_id', array_column($dataSet, 'batch_id'))
        ->where_in('sr_no', array_column($dataSet, 'sr_no'))
        ->get('part_list')
        ->result_array(),
    null,
    'sr_no'
);

两个单独的列值的完整数组,在这里没有什么意义-这根本不检查您正在寻找的“组合”。最后,您实际需要的WHERE子句类似于
WHERE(sr_no=71和batch_id=5)或(sr_no=75和batch_id=6)或…
@CBroe感谢您的回复。你能解释一下如何实现它吗?你还必须说明没有批次id的条目。这是你想要寻找的合法组合吗?在这些情况下,批次id是数据库中的空字符串,而不是
null
?我不能测试它,因为我没有设置Codeigniter项目,我不喜欢发布答案使用我没有测试过的代码。试着写,这是一个简单的故事。循环您的数组,将一个
$this->db->or\u where
调用放在循环内,其余(
get
result\u数组
)放在循环外。
$temp_Arr = [];

foreach($dataSet as $val){
    $temp_Arr[] = $val['sr_no']."-".$val['batch_id'];    
}
$sr_no_and_batch_id = "('".implode("', '", $temp_Arr)."')";
//echo $sr_no_and_batch_id;

$sql="SELECT * FROM `part_list` WHERE CONCAT(sr_no, '-', batch_id) IN $sr_no_and_batch_id";    
$query = $this->db->query($SQL);
return $query->result_array();