1

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

1
  • 3
    WHERE nomInteretUser IN('".implode("','",$array)."')" Commented May 5, 2016 at 12:03

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

It doosn't work , there is an error with the cote ' " :
SELECT idUser,COUNT(nomInteretUser) taux FROM userInteret WHERE nomInteretUser IN (' " .$string. " '), it dosn't work
always no resutat , even there is one
please paste here mysql error or if no error then paste table data and real query so we can justify where is problem. @user3070123
2

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)."')";

Working Demo

1 Comment

there an error in this ('".implode("','",$array)."')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.