If I then switch out the values (88,90,123,98,91,92) and replace with parameter :P4, the result in testing the query reduces to a single result (because it is using only the first number from the list.)
I understand that the IN clause requires an array, but I can’t find the right combination.
I have tried:
{{$_GET.filter_tags}} which is an array that receives it’s values from an array variable on the front end.
I have also tried to take the GET variable and split it (because it seems to be a comma separated string, not an array) and convert it into an actual array, but I get an array to string conversion error.
At any rate…does anybody know how I can a) use an array variable on the page to b) pass that array through for use within an IN clause in a custom query?
Ya, I’ve done this in a regular query plenty of times…always works. In the custom query though, I can’t even get the proper results when using the Execute Query button within the action, but assumed that is because I didn’t format properly as an array.???
I think you should use this one indeed. No need for splitting here just give the comma separated list as single parameter.
If you are getting a single record on the test query that might be because you have entered a single static test value? Maybe add the test numbers list there.
Any way even if with single row - test for the real results in the browser.
Unfortunately, using {{$_GET.filter_tags}} doesn’t fix it (in the browser.) Only the first value from the array is used in the search. I’ve confirmed by doing the search directly in mysql.
My understanding (from this thread Query using IN clause) is that IN requires an array, but the GET doesn’t seem to return an array to “value” in the debug:
From the stackoverflow posted by @George I read that arrays are not supported by PDO and you have to rewrite the query and have a parameter for each value in the array. In the query builder the query is build dynamically and it creates multiple parameters for each value in the array for the IN statement.
I found you can use the mysql find_in_set function in lieu of the IN clause. It should be noted that this shouldn’t be used on large sets as the performance will degrade.
find_in_set(cast(distro_tags.tag as char), :P4)
In my example this adds a WHERE condition requiring the tag field from the distro_tags table to be one of the entries in the P4 parameter, which is a string representation of a numeric array.
The parameter can be passed directly from the $_GET variable into the sql statement since it natively has no white space. If creating your own, the format is value followed by a comma without any white space (or it will break.)