MySQL “Where IN” value contained in list used with setParameter() requires value to be array if using placeholders

In normal SQL queries, you can use “WHERE IN” like this: “WHERE id IN (1,5,10)” as in:

If the list 1,5,10 “contains” the value represented by the value in the “id” field.

However, if you define a comma delimited string, you can’t simply do this:

$idListString=”1,5,10”

$dql=”SELECT d FROM Your/Entity d WHERE id IN (:idList)”

// and in your $dql query ->setParameter(”idList”=>$idListString)

This will only return the first result!   However, if $idList was an array $idListArray=array(1,5,10) then ->setParameter(“idList”=>$idListArray) works as expected.

This actually makes sense, the issue I found is that the Doctrine version of MySQL’s “WHERE… IN”  where a value is contained in a list is not well documented.  So web devs, especially Symfony developers beware!  PHP, Symfony, Magento, WordPress and MySQL experts will run into different web development issues — even as experienced experts, we’re always learning and improving.  This is just an obscure time saver, obscure probably  because it is very difficult to find search results and help related to the “MySQL IN” filter because the two-letter word “IN” is obviously omnipresent on the web in so many different contexts.  Searching “MySQL Contains IN filter” or something like that will help you find more information about the MySQL “IN” filter, function, operator — however it’s categorized.

About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

Your email address will not be published. Required fields are marked *