PHP - Using PDO with IN clause array

前端 未结 8 1847
失恋的感觉
失恋的感觉 2020-11-21 06:03

I\'m using PDO to execute a statement with an IN clause that uses an array for its values:

$in_array = array(1, 2, 3);
$in_values = implode(\',\'         


        
8条回答
  •  孤城傲影
    2020-11-21 06:49

    Variable substitution in PDO prepared statements doesn't support arrays. It's one for one.

    You can get around that problem by generating the number of placeholders you need based on the length of the array.

    $variables = array ('1', '2', '3');
    $placeholders = str_repeat ('?, ',  count ($variables) - 1) . '?';
    
    $query = $pdo -> prepare ("SELECT * FROM table WHERE column IN($placeholders)");
    if ($query -> execute ($variables)) {
        // ...
    }
    

提交回复
热议问题