Spaceman Spaceman - 1 year ago 49
MySQL Question

Select rows searching with an id in a column that contains multiple ids separated by a comma

I have a column in mysql db that contain multiple ids separated by a comma like so... 100,112,324 . The column is defined as varchar.

I want to find those rows whose ids column contains the id 112 or some other id that I specify. Similar questions I have found suggest splitting up the values in the column using lengthy code. Maybe I'm wrong but I would think there is a cleaner approach. Here is what I'm attempting to do, any help is appreciated...


include "../connect.php";
$usrid = "%112%";

$stm = $conn->prepare("SELECT * FROM subjects WHERE ids LIKE ?");
while($row = $stm->fetch(PDO::FETCH_ASSOC)) {
echo $row['consultant']."<br>";


I get a blank page as is.

Answer Source

Look at this PDO prepared statement:

Try putting the "to be binded variable" in an array: