-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'];
  }
17
  • possible duplicate of Imitate array_intersect in MySQL Commented Mar 24, 2015 at 18:10
  • The main question here is about PHP. What you have just linked has nothing to do with PHP. Please do not cause any confusion here. Commented Mar 24, 2015 at 18:12
  • I'm not @TebTheestatebook. You're asking if you can use a PHP function in a MySQL statement. The answer is no. But you can imitate it with the link provided. If your question is different than that you need to clear things up. Commented Mar 24, 2015 at 18:16
  • @JayBlanchard Actually, you can run a function inside a query; it's just very tricky Sam ;-) Commented Mar 24, 2015 at 18:21
  • How Fred? For some reason Jay wants to make incorrect generalizations. Commented Mar 24, 2015 at 18:21

1 Answer 1

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})";
Sign up to request clarification or add additional context in comments.

2 Comments

Hey @Fred -ii- cast your eye on this and see if you'd make improvements.
Hm... interesting Sam; good show 'ol chap! Seems all good to moi ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.