Problem in PHP + MySQL query using LIKE

1

Good afternoon guys, recently in one of my projects a problem has arisen and I hope you can help me.

To better understand the site is a site of consultations in the health area, such as clinics, cardiology, urology, etc. Some clinics can belong to two categories at the same time, such as "Clinic" and "Cardiology" I made the query as follows:

$url.= ' AND t.areadasaude LIKE "%'.$especialidadesaude.'%"';
The problem is, the category Cardiology belongs to id = 1, cardiology belongs to id = 19, LIKE queries everything inside the table all results that are within category 19 are appearing in category 1 that because LIKE takes number 1 from id 19. How do I get LIKE to get 100% of what is marked?

Note: If I mark two categories, the database is registered as follows: ["1","19"] , we should consider in the tbm query when you have more than one category checked. Could someone help me?

    
asked by anonymous 05.01.2016 / 19:54

2 answers

2

How about trying to use t.areadasaude IN ('1', '19') instead of LIKE

    
05.01.2016 / 20:03
0

What is the value of this $ specialties variable and how is it stored in this 'areasaude' field? If it's ids separated by space (or another character) you can use slipt:

$ids = split(' ', $especialidadesaude);

could return the values of the field in the database, if they are stored separated by commas tmb and split it and compare the vectors;

If the field 'areasaude' in the database has a single value, the query would look like this:

$url.= ' AND t.areadasaude IN($ids)';

But if the ids of related categories are being stored in a field I think this is not ideal but rather have a relationship n: m where there would be a table with the ids of the two tables. In this case the query would look like this (I did not test the query):

SELECT c.* FROM categorias c 
INNER JOIN clinicas_x_categorias cxc ON c.idCat = tbs.idCat
WHERE cxc.idCat IN($ids)
GROUP BY c.idCat 
    
05.05.2016 / 15:49