WHERE IN
を使用してステートメントを作成する方が効率的です。 一致するメールを取得し、結果の配列を比較します。array_diff()
を使用できます。 配列の違いを取得します。
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect($servername, $username, $password, $db);
echo "Connected successfully\n";
$tags = preg_split("/\,/", $_POST['tags']);
$invalidEmails = array();
$count = 0;
// modify your array how you want it
foreach ($tags as $i => $tag) {
$trim_brackets = trim($tag, '[]');
$trim_quotes = trim($trim_brackets, '"');
$tags[$i] = $trim_quotes;
}
// build placeholders for WHERE IN
$in = str_repeat('?,', count($tags) - 1) . '?';
// prepare query
$stmt = $conn->prepare("SELECT mail FROM dej_colleagues WHERE mail IN ($in)");
// bind an array of values. First params is a type list.
$stmt->bind_param(str_repeat('s', count($tags)), ...$tags);
$stmt->execute();
$results = $stmt->get_result();
// fetch the matching mails into an array
$mails = [];
foreach ($results as $row) {
$mails[] = $row['mail'];
}
// get the difference between the two arrays
$invalidEmails = array_diff($tags, $mails);
if (count($tags) === count($mails)) {
echo "good";
}
var_dump($invalidEmails);