PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE sys_code ( codeId VARCHAR(100), text VARCHAR(100) ); INSERT INTO sys_code(codeId, text) VALUES (2, '200002'), (22, '200022'), (23, '200023'), (66, '200066'), (128, '200128');
Copy Clear
Copy Format Clear
<?php $ids = [200022,200128]; $value="23"; // creates a string containing ?,?,? $bindClause = implode(',', array_fill(0, count($ids), '?')); //create a string for the bind param just containing the right amount of s $bindString = str_repeat('s', count($ids)); $stmt = $pdo->prepare("select IV.codeId as optionValue, IV.text as optionText , CASE WHEN IV.text in(".$bindClause.") THEN 1 ELSE 0 END as selected from sys_code IV where IV.codeId=:value"); $stmt->bindParam($bindString, ...$ids); $stmt->bindParam(':value', $value, PDO::PARAM_STR); $stmt->execute(); var_export($stmt->fetchAll());
Show:  
Copy Clear