So this is a problem that I have had for quite some time, the ability to pass in an array of user ids, emails, or any other array of values, and perform a database operation on all records that match.
For Example:
Let's say an admin user of your website wants to do a bulk-update on the users of of the software, disabling their accounts. Perhaps they were displayed a list of users, and checked the "disable" box next to some of them. Now they click the "Disable Users" button. The slow way to do this is to (on the server-side) iterate through each userId and call some function, such as User.disable(userId).
The more efficient way is to pass in the array of userIds into the database and have it do the operation at once on all desired records. Here is how that is done.
Your stored procedure should look like the following:
-- userIds is a json string of an array of integers, e.g. "[1, 2, 3]"
CREATE PROCEDURE `user.disable`(userIds VARCHAR(4000))
BEGIN
UPDATE user SET enabled = 0 WHERE JSON_CONTAINS(userIds, id);
END
Your code which calls this might look like the following (PHP):
class User {
public static function disableMany(array $userIds): bool
{
$strUserIds = json_encode($userIds);
$query = "CALL `user.disable`($strUserIds)";
$result = mysqli_query($con, $query);
return $result !== false;
}
}
This example is using MariDb, see link JSON_CONTAINS.
The MySQL documentation is Here (JSON_CONTAINS).
I hope this helps and good luck.