2

This is my array in php: $list = array('dog', 'tiger', 'lion', 'elephant')

Table name: watchlist Database: assignment enter image description here

After comparing the $list with the watchlist table it should print only the matched items. So, the output for this will be: dog tiger

Is there any mysql query to get this output. I am weak in mysql. Thank you in advance.

Shadow
  • 33,525
  • 10
  • 51
  • 64
RajB009
  • 417
  • 2
  • 7
  • 19
  • `SELECT * FROM watchlist WHERE name IN ('dog', 'tiger', 'lion', 'elephant')`. Of course you would want to bind the values and stuff like that, but this is the gist of it. – Jonathan Jul 31 '18 at 19:52
  • SQL has IN operator... `name IN('dog', 'tiger', 'lion', 'elephant')` will give the results you need.. if you also need/want to enforce the order you need to add `ORDER BY FIELD(name, 'dog', 'tiger', 'lion', 'elephant')` this is a MySQL only function. – Raymond Nijland Jul 31 '18 at 19:52

1 Answers1

0

You can do this:

$query = "SELECT * FROM `watchlist` WHERE `assignment` IN('".implode("','",$list)."')";

Note: Make sure that any external input is sanitized.

Ivaylo
  • 467
  • 6
  • 19