I have a table where one of the columns I use to know which users are subscribed to a thread of a forum. This column filled with the user names. For example, the content may be as follows:
Paco; Pepe; Laura;
If for example I want to find all the threads to which Paco is subscribed, I make the following query:
$statement = $conexion->prepare("SELECT * FROM comentarios WHERE suscritos LIKE :suscritos");
$statement->execute(array(":suscritos" => "%Paco%"));
$todos_mis_mensajes = $statement->fetchAll();
So far so good, but if for example there were another user with the name PacoPepe, the results of the query would be incorrect.
With PHP I can clean the array easily, the problem is that I want to load 10 records per page, which would do something like this:
$statement = $conexion->prepare("SELECT * FROM comentarios WHERE suscritos LIKE :suscritos LIMIT 0,10");
$statement->execute(array(":suscritos" => "%Paco%"));
$todos_mis_mensajes = $statement->fetchAll();
I need to filter when making the query, because if I have to do it after doing it, it gets very complicated to load 10 records per page. I had thought about saving the user ID but it would be in them with ID 1 and 11 for example.
Is there a way to do a explode()
at the same time I make the query? That way I would not use %
when making the query and would return only Paco's records.