My query:
SELECT MAX(num)
FROM (SELECT nomInteretUser,idUser,COUNT(nomInteretUser) as num
FROM userInteret
WHERE nomInteretUser IN ('piano','flute','chien') GROUP BY idUser
)
The goal is to change the IN ('piano','flute','chien')
to a dynamique array like
SELECT nomInteretUser, idUser, COUNT(nomInteretUser) as num
FROM userInteret
WHERE nomInteretUser IN ($array)
GROUP BY idUser
NB: $array is an array houw contain the list of interet
Use implode()
. Try below code. You need to convert your array to string. So here we will implode it with comma separated with quotes like ','
string and use it in query.
$array =array('piano','flute','chien');
$string = implode("','", $array);
$query = "SELECT nomInteretUser, idUser, COUNT(nomInteretUser) as num
FROM userInteret
WHERE nomInteretUser IN ('".$string."')
GROUP BY idUser";
Check online Demo: Click Here
You can use implode() function.
implode()
function will join arrays like: piano','flute','chien
.
We need a single quote and the beginning and end.
So, add it before and after implode()
like: "'".implode("','",$array)."'"
$array = array('piano','flute','chien');
WHERE nomInteretUser IN('".implode("','",$array)."')";