php - How to use IN dynamically with mysqli prepare statement -


this question has answer here:

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

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -