What is the correct way to store database IDs in an array in PHP?

When storing database IDs in an array in PHP, it is important to ensure that the IDs are properly sanitized to prevent SQL injection attacks. One way to do this is by using prepared statements with parameterized queries when retrieving the IDs from the database. This helps to protect against malicious input and ensures that the IDs are securely stored in the array.

// Assuming $db is your database connection

// Prepare a statement to retrieve IDs from the database
$stmt = $db->prepare("SELECT id FROM table_name");
$stmt->execute();

// Fetch IDs and store them in an array
$ids = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $ids[] = $row['id'];
}

// Use the $ids array as needed