Answer the question
In order to leave comments, you need to log in
What is the correct way to substitute a value into ... IN (?) ... using the execute() method?
Task:
Extract rows with a specific id value from the database, but the nuance is that sometimes you need to extract several rows from the database.
For example, those whose id is equal to 100, 101 or 102.
Constructions ... WHERE id = ? not enough.
My decision:
$sql = 'SELECT * FROM table_name WHERE id IN (?)';
$query = $pdo->prepare($sql);
$query->execute([100, 101, 102]);
$result = $query->fetchAll(PDO::FETCH_ASSOC);
$sql = 'SELECT * FROM table_name WHERE id IN (?)';
$query = $pdo->prepare($sql);
$query->execute(array(100, 101, 102));
$result = $query->fetchAll(PDO::FETCH_ASSOC);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question