PDO Placeholders + SQL IN()

<?php // Making a PDO placeholder for an array and an `IN()` clause // Because I can never remember the syntax... $ingredients = array('cheese', 'pepperoni', 'sausage', 'anchovies', 'bubblegum', 'magic'); $where = 'pizza.toppings IN (' . implode(',', array_fill(0, count($ingredients), '?')) . ')'; // Which equals: 'pizza.toppings IN (?,?,?,?,?,?)' $stmt = $conn->prepare("SELECT * FROM [pizza] WHERE {$where}"); $stmt->execute($ingredients); while($pizza = $stmt->fetch()) { nomNomNom($pizza); }
How to bind an array of values to a prepared SQL IN() clause.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.