0

I have a simple comma separated string like so:

$thisusersfavorites = "12,13,34,65,11";

I have a simple query like so:

"SELECT * FROM recipes WHERE id = '$thisusersfavorites' ORDER BY name ASC";

But the result is only yielding the first ID result, not the rest.

GMB
  • 216,147
  • 25
  • 84
  • 135
Dustin
  • 25
  • 3

1 Answers1

0

In MySQL, you can use handy string function find_in_set() to search for a value within a comma-separated string:

select * from recipes where find_in_set(id, ?) order by name

Note that I rewrote your statement as a parameterized query rather than munging values inside the query string. You do want to use these to make your code more efficient and safer.

Also, please keep in mind that this is rather inefficient way to proceed. It would be far beter to split your list into individual values, and to use in:

where id in (12, 13, 34, 65, 11)

As a parameterized query:

where id in (?, ?, ?, ?, ?)
GMB
  • 216,147
  • 25
  • 84
  • 135