How to use "WHERE IN" in MYSQLi Prepared Statements in PHP?



<?php
$ids = [1,2,3,4,5]; // any array of ids
$count = count($id);
$placeholders = implode(',', array_fill(0, $count, '?'));
$bindStr = str_repeat('i', $count);

$stmt = $mysqli -> prepare("SELECT * FROM table WHERE id IN ($placeholders)");
$stmt -> bind_param($bindStr, ...$ids);
$stmt -> execute();


Tagged: PHP MYSQLi
You can connect with me on Twitter or Linkedin.
Latest on My Blog
PHP Beginner's Tutorial
Beginner's PHP Tutorial
Image for Laravel High CPU Usage Because of File-based Session Storage
Laravel High CPU Usage Because of File-based Session Storage
Image for Resizing Droplets: A Personal Experience
Resizing Droplets: A Personal Experience
Image for Moving our CDN (10+ GB images) to a new server
Moving our CDN (10+ GB images) to a new server
Image for Disqus, the dark commenting system
Disqus, the dark commenting system
Image for Creating a Real-Time Chat App with PHP and Node.js
Creating a Real-Time Chat App with PHP and Node.js
Related Articles
650