-1

Need to run a mysql SELECT through PHP where two arrays equal one another.

 $sql = "SELECT * FROM around WHERE 'array_intersect($ar1, $ar2)'";
 $resultsd = $conn->query($sql);
 foreach($resultsd as $rowd) {

 $sword[] = $rowd['TIM'];
 }

After the match is found, need to pull from that particular row in mysql. This needs to be in PHP.

Arrays are from:

  $ar1 = array();
  $sql = "SELECT * FROM blc WHERE ffv='$safe_username'";
  $results = $conn->query($sql);
  foreach($results as $row) {  
  $ar1[] = $row['vvvs'];
  }

1 Answers1

2

Given two arrays and wanting to perform a query for the intersection of those arrays:

$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);

$sql = "SELECT * FROM `around` WHERE `foo` IN ('" . implode("','", $result) . "')";

This results in the query:

SELECT * FROM `around` WHERE `foo` IN ('green','red')

The intersection happens outside of the query because there still is stuff to do with the data before it is usable in a query. Actually I would be neater and do this:

$results = "'" . implode("','", $result) . "'";
$sql = "SELECT * FROM `around` WHERE `foo` IN ({$results})";
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119