PHP SQL语句使用REGEXP获取多个值

时间:2017-06-12 10:22:24

标签: php sql arrays pdo

如何使用REGEXP从数组$vall中获取多个数据。

我引用了以下示例代码: PHP sql statement where clause to multiple array values

部分样本数据:CGCG-0025-0,CGCR-0003-0,CGRQ-0163-0

foreach ($list as $value) // this $list is from another array
{
    $part = $value[3];
    $vall[] = $value[3];  // $list1 is an empty array
}

$partnumber =[];

foreach($vall as $v)
{
    $partnumber[] = "*.".$v.".*";

    print_r(array_values($partnumber));   // these are some of the values Array ( [0] => *.CGCG-0025-0.* ) Array ( [0] => *.CGCG-0025-0.* [1] => *.CGCG-0025-0.* ) Array ( [0] => *.CGCG-0025-0.* [1] => *.CGCG-0025-0.* [2] => *.CGCR-0003-0.* )
}

foreach($partnumber as $x)
{
    echo '<br>'.$partnumber; // shows 'Array' on each lines 
}

$fsql="select * from Error where RptDatime = 201706091000 and partnumber REGEXP  '".implode("|",$partnumber)."'";

//example 1 $fsql="select * from MPdError where RptDatime = 201706091000 and partnumber = ('CGRQ-0057-0') ";
//example 1 shows the correct data but i need multiple partnumber
$getResults = $conn->prepare($fsql);
$getResults->execute();
$results = $getResults->fetchAll(PDO::FETCH_BOTH);

foreach($results as $row)
{
    $mac = $row['Machine'];
    $id = $row['Id'];
    echo '<br><br><br>ID:'.$id.'<br>Machine Number :'.$mac;
}

1 个答案:

答案 0 :(得分:0)

虽然这可能不是一个好的解决方案,因为它包含巨大的安全风险,但我仍然会根据上面的需要发布它。

设计中不复杂的数据不需要REGEXP。

$fsql = "select * from Error where RptDatime = 201706091000 and partnumber in ('" . implode("','", $vall) . "');"