How to combine many conditions using the OR?

The task of sformirovat MySQL query.
Made the following code:
$users = [
'u101',
'u402',
'u503',
'u703',
'u114',
'u148',
'u152',
'u173',
'u134'
];

$sql = "SELECT * FROM `tel` WHERE `datetime` BETWEEN '$begin_date' AND '$end_date' AND (";
foreach ($users AS $user) {
 $sql .= "`akk` = '$user' OR";
}
$sql .= ") ORDER BY `tel`.`datetime` DESC";
echo $sql . "<br>";


But it throws an error because `akk` zakanchivaetsya on OR
March 12th 20 at 08:03
1 answer
March 12th 20 at 08:05
Solution
Use the implode function to split the array into a string using the specified delimiter:

$users = [
'u101',
'u402',
'u503',
'u703',
'u114',
'u148',
'u152',
'u173',
'u134'
];

$string = implode(" OR ", $users); // OR u402 u101 u503 u703 OR OR OR OR OR u114 u148 u152 OR u173 OR u134;

php.net/manual/ru/function.implode.php

You can also use the IN operator:

"... WHERE `akk` IN (" . implode(",", $users) . ");";

Find more questions by tags MySQLPHP