gpt4 book ai didi

PHP MySQL 检查数组中的项目是否存在于表中,然后显示

转载 作者:行者123 更新时间:2023-12-01 00:21:07 26 4
gpt4 key购买 nike

PHP 5.4 与 MySQL

以此为基础:

<?php
$arr = ...
$sql = 'SELECT COUNT(*) FROM table WHERE field IN ( ' . implode( ',', $arr ) . ' );';
$result = $db->query( $sql );
?>

我如何显示数组中的哪些项目不在表格中?

最佳答案

不返回计数,而是返回字段本身:

$sql = 'SELECT field WHERE field IN ' . implode( ',', $arr ) . ' );';
$result = $db->query($sql);
$found = array();
while ($row = $result->fetch_assoc()) {
$found[] = $row['field'];
}

然后你可以比较两个数组:

$not_found = array_diff($arr, $found);

关于PHP MySQL 检查数组中的项目是否存在于表中,然后显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26187478/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com