How to select results when variable is an array

Hello,
Here is an array of weekdays
$days = [1, 3, 4]

Here is my query:

 $sql = "INSERT INTO `filtered`
			            (id, username, ticket, o_time, type, size, item, o_price, s_l, t_p, c_time, c_price, profit)
			SELECT * 
		    FROM `data`
			WHERE c_time BETWEEN :start_date AND :end_date AND DAYOFWEEK(c_time) IN ($days)";
			
	 $stmt = $db->prepare($sql);
	 $stmt->bindParam(':start_date', $start_date, PDO::PARAM_STR);
	 $stmt->bindParam(':end_date', $end_date, PDO::PARAM_STR);
	 $stmt->execute();

It wirked fine until I added
AND DAYOFWEEK(c_time) IN ($days)
How do I do bindParam in case of an array?

Do I change $stmt->execute(); to: $stmt->execute($days); ?

Binding parameters to a IN clause is not as straightforward as many would like it to be, but it is possible when you know how.
Take a look at this example:-

Where is this array coming from? Is it dynamically being built from external/unknown data or is it hard-coded?

If it is a list of hard-coded values, just build the sql query with the appropriate IN(1,3,4) expression.

If it is from external/unknown data, you can use FIND_IN_SET() and use a single prepared query place-holder to supply the value to the query.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.