Question

I am new to writing server code, anyway I have a SQL DB that contains a list of numbers, I am wanting to check to see if an array that contains a list of numbers has any overlap with the DB.

Database:
ID  Number
1    3
2    5
3    7
4    11
5    13
6    19

For example, in PHP/psuedocode:

$numbers = $_REQUEST['NUMBERS']  // array of numbers i.e. [3, 7, 20, 54]

This is what I'm looking for:

echo json_encode($result) // returns [3, 7]
Was it helpful?

Solution

Just do a query selecting the rows containing the numbers from the request:

$numbers = implode(',', $_REQUEST['NUMBERS']);
$query = "SELECT Number FROM TableName WHERE Number IN ($numbers)";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top