Alex Chen Alex Chen - 1 year ago 61
MySQL Question

How to cast string to list in PHP or MySQL

I have a list of ids I want to fetch data from a Database.

$ids = "1,2,3,4";

Here is my query

$this->Execute("SELECT * FROM table WHERE id in (?)",array($ids));

This only gives my result where id=1. How do I fetch data where id is from 1 to 4?

Answer Source

Use FIND_IN_SET since $ids is a string

$this->Execute("SELECT * FROM table WHERE FIND_IN_SET(id, ?)", array($ids));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download