Problems in narrowing MySQL query

1

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.

    
asked by JetLagFox 07.07.2017 в 13:50
source

1 answer

2

Finally the code that works for all cases is as follows:

$statement = $conexion->prepare("SELECT * FROM comentarios  WHERE (suscritos LIKE 'Paco;%' OR suscritos LIKE '% Paco;%') LIMIT 0,10");
$statement->execute();
$todos_mis_mensajes = $statement->fetchAll();
    
answered by 07.07.2017 в 16:12