本文介绍了PHP sql语句where子句到多个数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果value [3]存储了多个数据,我该如何使用where子句

How do i use where clause for array if value[3] has multiple data stored

$fsql="select * from Error where RptDatime = 201706091000 and partnumber like  ('$value[3]')";
$getResults = $conn->prepare($fsql);
$getResults->execute();
$results = $getResults->fetchAll(PDO::FETCH_BOTH);

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

推荐答案

您可以使用regex函数代替like.以下是适合您的示例代码.

You can use regex function instead of like. Below is the sample code for you.

$partnumner = [];

    foreach($value[3] as $v)
    {
        $partnumber[] = "*.".$v.".*";
    }

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

如果您仍然希望使用like,可以在此处

If you still wish to use like, you can follow the answer here

这篇关于PHP sql语句where子句到多个数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 06:55
查看更多