0

I have used this method to bind IN() condition , But still my issue is

$in  = str_repeat('?,', count($in_array) - 1) . '?';
$sql = "SELECT * FROM my_table WHERE my_value IN ($in) and other_value='$variable' ";
$stm = $db->prepare($sql);
$stm->execute($in_array);

How to bind the $variable with in this query ?

Can any one help ?

Community
  • 1
  • 1
newbie
  • 29
  • 7

1 Answers1

1

Simple, build an array of placeholders, add an extra placeholder for your extra $variable, then add $variable's value to the array you pass into the execute() call:

$temp = array_fill('?', count($array_in) - 1);
$placeholders = implode(',', $temp);

$array_in[] = $variable; // add your $variable to the values list

$sql = "SELECT ... IN ($placeholders) AND other_value = ?"; // insert placeholders string into query
$stmt = $dbh->prepare($sql);
$stmt->execute($array_in);
Marc B
  • 356,200
  • 43
  • 426
  • 500