G
G
Georgy Butin2020-06-28 18:40:00
PHP
Georgy Butin, 2020-06-28 18:40:00

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);


Also tried like this:
$sql = 'SELECT * FROM table_name WHERE id IN (?)';
$query = $pdo->prepare($sql);
$query->execute(array(100, 101, 102));
$result = $query->fetchAll(PDO::FETCH_ASSOC);


But none of the options work.
I have been working with php and sql for more than 5 months, but I have never encountered such a problem.

PS: the connection to the db is done correctly

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-06-28
@butingeorgiy

https://stackoverflow.com/a/14767651/285587

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question