What is the best practice for comparing values from a table with values from an array in MySQL using PHP?

When comparing values from a table with values from an array in MySQL using PHP, the best practice is to use a loop to iterate through the array and execute individual queries to fetch and compare the values from the table. This ensures that each value in the array is compared with the corresponding value in the table.

<?php
// Assuming $array contains the values to compare
$array = [1, 2, 3];

// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Loop through the array and compare values with table
foreach ($array as $value) {
    $query = "SELECT * FROM table_name WHERE column_name = $value";
    $result = $mysqli->query($query);

    if ($result->num_rows > 0) {
        // Value exists in table
        echo "Value $value exists in the table";
    } else {
        // Value does not exist in table
        echo "Value $value does not exist in the table";
    }
}

// Close database connection
$mysqli->close();
?>