Jack Mercier Jack Mercier -4 years ago 64
SQL Question

PHP SQL statement using REGEXP to fetch multiple values

How do I use REGEXP to fetch multiple data from the array

$vall
.

I referred a sample code from:
PHP sql statement where clause to multiple array values

Some of the sample data: 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;
}

Answer Source

Though this may not be a good solution as it contains a huge security risk, I will still post it based on what is needed above.

REGEXP is not needed for data that are not complex in design.

$fsql = "select * from Error where RptDatime = 201706091000 and partnumber in ('" . implode("','", $vall) . "');"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download