Spaceman Spaceman - 4 months ago 12
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...

<?php

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

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

?>


I get a blank page as is.

Answer

Look at this PDO prepared statement:

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

$stm->execute(array($usrid));