0

Want to select MySQL rows where column value is 11 or 12 or 23. Tried with such code

$query_select_all = 'SELECT * FROM 2_1_journal WHERE CAST(EntryId AS UNSIGNED) IN (?)';
$sql = $db->prepare($query_select_all);
$data1[] = '11, 12, 23';
$sql->execute($data1);

But the code select only rows where column value is 11 (the first value from $data1[] = '11, 12, 23';)

Tried without ? and works as necessary.

Here is statement without ? (positional placeholder)

$query_select_all = 'SELECT * FROM 2_1_journal WHERE CAST(EntryId AS UNSIGNED) IN (11, 12, 23)';
$sql->execute();

The statement works as expected (select rows where EntryId is 11 or EntryId is 12 or EntryId is 23.

Where is error in the first code (with ?)?

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
user2360831
  • 367
  • 4
  • 16

2 Answers2

1

Based on https://stackoverflow.com/a/15991146/285587

$query_select_all = 'SELECT * FROM 2_1_journal WHERE CAST(EntryId AS UNSIGNED) IN';
$data1 = '11, 12, 23';
$data1 = explode(",",$data1);//Convert Comma Separated String into Array
$in  = str_repeat('?,', count($data1) - 1) . '?';
$query_select_all = $query_select_all .'('. $in .')';
$sql = $db->prepare($query_select_all);
$sql->execute($data1);
Community
  • 1
  • 1
user2466952
  • 196
  • 8
-1

I think you change the statement with between instead of IN ....

Umer Best
  • 9
  • 1
  • 8
  • What if `$query_select_all = 'SELECT * FROM 2_1_journal WHERE CAST(EntryId AS UNSIGNED) IN (11, 12,23)';`? I need many `OR` and not `Between`. I will edit my question. – user2360831 Jun 25 '13 at 06:27