How can I check which IDs exist in a table in PHP?

To check which IDs exist in a table in PHP, you can execute a SELECT query to retrieve the IDs from the table and then compare them with the IDs you are looking for. You can use a loop to iterate through the retrieved IDs and check if they match the IDs you are checking for.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Array of IDs you want to check
$ids_to_check = [1, 2, 3, 4, 5];

// Retrieve existing IDs from the table
$sql = "SELECT id FROM your_table";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        $existing_id = $row["id"];
        if (in_array($existing_id, $ids_to_check)) {
            echo "ID $existing_id exists in the table.<br>";
        }
    }
} else {
    echo "0 results";
}

$conn->close();