php - How to use IN dynamically with mysqli prepare statement -
this question has answer here:
- pdo binding values mysql in statement 8 answers
- preparedstatement in clause alternatives? 25 answers
i trying use in mysqli prepare statment
$user_in = "'28','22'"; $stmt = $this->connection->prepare("select `id` `$this->table_name` `user_id` in (?) "); if($stmt){ $stmt->bind_param('s',$user_in); if($stmt->execute()){ $result = $stmt->get_result(); if($result !== false && $result->num_rows >= 1){ $row = $result->fetch_all(mysqli_assoc); $stmt->close(); var_dump($row); } } } echo $this->connection->error; return false;
but approach above not able fetch result sets
placeholders represent single value. if have variable , placeholder-using query:
$var = '1,2,3'; select ... foo in (?)
then query executed sql had literally been
select ... foo in ('1,2,3')
and 3 separate csv values treated single monolithic string.
in
clauses 1 place placeholders useless, since have dynamically build string many placeholders have values, e.g.
$vals = array(1,2,3); $placeholders = '?' . str_repeat(',?', count($vals) - 1); $stmt = $db->prepare("select ... foo in ($placeholders)"); foreach($vals $i => $val) { $stmt->bind($i, $vals[$i]); }
and then
Comments
Post a Comment