What are the potential pitfalls of using the explode function in PHP to split input strings for database queries?

Using the explode function in PHP to split input strings for database queries can lead to SQL injection vulnerabilities if the input is not properly sanitized. To prevent this, it is essential to use prepared statements with parameterized queries to safely interact with the database.

// Example of using prepared statements with parameterized queries to prevent SQL injection
$input = "1,2,3,4";
$ids = explode(",", $input);

$db = new PDO("mysql:host=localhost;dbname=myDB", "username", "password");

$stmt = $db->prepare("SELECT * FROM myTable WHERE id IN (:ids)");
$stmt->bindParam(':ids', $ids, PDO::PARAM_STR);
$stmt->execute();

$results = $stmt->fetchAll();