0

Here is the sql code:

SELECT s_id FROM questions WHERE s_id NOT IN (:ids)

IDs are 1,2 and there is only 2 line in the table. But that selects the line 2, anyway. Where is the problem that I make??

Yusuf
  • 53
  • 8
  • 1
    Add your sample dataset in http://sqlfiddle.com/ and add in your question – M Khalid Junaid Feb 12 '14 at 09:49
  • You've not shown any PHP, but I'll hazard a guess that you're using PDO, which doesn't have built-in support for binding an array of params for IN. – John Carter Feb 12 '14 at 09:51
  • @Alexander - of course it has. The SQL is good, the problem is, obviously, how he make the parameterized query in PHP. We lack to see the PHP code! – davidkonrad Feb 12 '14 at 09:52
  • Check out http://stackoverflow.com/questions/920353/php-pdo-can-i-bind-an-array-to-an-in-condition?rq=1 for how you could handle this in PDO. – John Carter Feb 12 '14 at 09:52

1 Answers1

1
SELECT s_id FROM questions WHERE s_id NOT IN (:ids)

This has a placeholder for exactly one value, :ids. If you're binding the value like:

$stmt->bindValue(':ids', '1,2');

Well, guess what, you've just bound one value "1,2". This is the same as:

SELECT s_id FROM questions WHERE s_id NOT IN ('1,2')

Which is not the same as

SELECT s_id FROM questions WHERE s_id NOT IN (1, 2)

If you want to bind more than one value, you need more than once placeholder. Placeholders stand in for values, they're not like copy and pasted SQL snippets.

deceze
  • 510,633
  • 85
  • 743
  • 889