An error stops my script

While the posted code does produce an error at the foreach() loop being shown, the OP is asking about a foreach() loop later in the code, that’s looping over the result from the query, when it matches zero rows.

It only works. By putting the external data directly into the sql query statement (the imploded string in the $days variable), it is not secure and will allow sql injection that will let a hacker include the contents of any of your database tables in the output from this query. The main point of using a prepared query is to prevent sql special characters in data from breaking the sql query syntax, which is how sql injection is accomplished. You need to properly build and execute a prepared query.

This is the correct answer to address the problem of the query matching no data (except that the OP is apparently returning a zero, not a boolean or an empty array.)

Conditional logic is fundamental to what programming is about, and lets the program test input values to control what to do next.

The following is list of the OPs threads dealing with the weekday part of the query -

How to filter results by weekday / weekdays? - #5 by r937 .

How do I filter results when a variable is an array? (This one shows how the OP is/was fetching the data. You should just return the fetchAll() result. It is an empty array if the query didn’t match any data. Using count() on the value will both tell you how many rows, in case you want to display that value, and let you test if you should loop over the data to produce the output.)

https://www.sitepoint.com/community/t/how-to-select-results-when-variable-is-an-array/345479 .

https://www.sitepoint.com/community/t/it-seems-i-miss-a-few-crucial-issues-when-it-comes-to-arrays/345644/3 .