0

I have the following multidimensional array, generated after some database Selects:

<?php
$array = array (
    array("X"=>500, Y="400"),
    array("X"=>234, Y="347"),
    array("X"=>845, Y="345"),
    array("X"=>264, Y="916")
);
?>

Now I need to do a select in a table where BOTH field X and Y are in the array. How to do that? Like:

SELECT FROM table WHERE 
                        (X=500 AND Y=400) OR
                        (X=234 AND Y=347) OR
                        (X=845 AND Y=345) OR
                        (X=264 AND Y=916)
;

I can only find solutions here on StackOverflow for a single item in a query, but not for two values in a multidimensional array that needs to be exactly the same. Thank you!

1
  • 2
    Not sure I understand - what isn't working for you here? Commented Jul 15, 2015 at 14:54

1 Answer 1

1

If I understood, you need to read your array and pass it to your SQL.

Something like:

<?php

$where = null;
foreach ($array as $a)
{
    if (is_null($where))
    {
        $where = " WHERE (X = " . $a['X'] . " AND Y = " . $a['Y'] . ")";
    }else
    {
        $where .= " OR (X = " . $a['X'] . " AND Y = " . $a['Y'] . ")";
    }
}

$sql = "SELECT * FROM table " . $where;
?>

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

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.