R
R
rogiivs2019-04-08 15:48:55
PHP
rogiivs, 2019-04-08 15:48:55

How to bind an array of strings in an IN SQLite PDO statement via execute?

There is a string obtained from a POST/GET variable of the form (text1,text2,text3,text4,text5). The number of values ​​is not defined, but they are all text. How to bind them in execute ? that is, I can pass it as a string, but then IN will receive one string in theory. How to solve the issue?

$tags= $_POST['tags']; // тут (текст,текст,текст,текст)

$db = new PDO('sqlite: files.sqlite');
$query = $db->prepare("SELECT * FROM files WHERE tags IN (:tags)");
$query->execute(array(':tags' => explode(',',$tags)));

until the thought just came
$tags= $_POST['tags']; // тут (текст,текст,текст,текст)

$db = new PDO('sqlite: files.sqlite');
$query = $db->prepare("SELECT * FROM files WHERE tags IN (:tags)");
$query->execute(array(':tags' => "'" . explode("', '",$tags) . "'"));

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
senior65, 2019-04-08
@rogiivs

I think it was discussed
here

F
FanatPHP, 2019-04-08
@FanatPHP

https://phpdelusions.net/pdo#in
Named placeholders are more of a hassle, so I highly recommend positional ones. Especially if there is only one placeholder in the request.
Well, I don’t understand the point of writing the unfortunate field name 4 times, when one is enough

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question