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

San*_*nya 0 php mysql database

PHP 5.4和MySQL

建立在此基础之上:

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

我将如何显示表格中哪些不在阵列中?

Bar*_*mar 5

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

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

然后,您可以比较两个数组:

$not_found = array_diff($arr, $found);
Run Code Online (Sandbox Code Playgroud)