PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE somewhere ( id INT AUTO_INCREMENT, name VARCHAR(100), PRIMARY KEY (id) ); INSERT INTO somewhere(id, name) VALUES ("1", 'Chad'), ("4", 'Ned'), ("5", 'Dave'), ("14", 'Newt'), ("18", 'Bill'), ("21", 'Norton'), ("25", 'Alan');
Copy Clear
Copy Format Clear
<?php // $mysqli is already declared as a working database connection by this sandbox $ids = ["1", "5", "18", "25"]; $names = ['Dave', ' Bill']; $idCount = count($ids); $nameCount = count($names); $sql = 'SELECT name FROM somewhere'; $idPlaceholders = implode(',', array_fill(0, $idCount, '?')); $namePlaceholders = implode(',', array_fill(0, $nameCount, '?')); $stmt = $mysqli->prepare("$sql WHERE id IN ($idPlaceholders) AND name IN ($namePlaceholders)"); $stmt->bind_param(str_repeat('s', $idCount + $nameCount), ...array_merge($ids, $names)); $stmt->execute(); $result = $stmt->get_result(); foreach ($result as $row) { echo "<div>{$row['name']}</div>\n"; }
Show:  
Copy Clear