Jordy
Jordy

Reputation: 4809

MySQL Select where two fields are in multidimensional array

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!

Upvotes: 0

Views: 680

Answers (1)

ital0
ital0

Reputation: 334

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.

Upvotes: 1

Related Questions