PDO IN() Array Statement AND a placeholder

前端 未结 5 909
死守一世寂寞
死守一世寂寞 2021-01-04 08:16

I found this code on SO, which is great for using PDO and the IN() statement together.

$values = explode(\',\', $values) ; # 1,4,7

$placeholders = rtrim(str         


        
5条回答
  •  难免孤独
    2021-01-04 08:54

    And an other solution can be (if you like the :param_name = $value way, as me):

    $params = array(
         ':product' =>  $product
    );
    $_in_params = array();
    foreach ( $_in_values as $idx_in => $value_in)
    {
        $_in_params[] = ':param_in_'.$idx_in;
        $params[':param_in_'.$idx_in] = $value_in;
    }
    
    $query .= "SELECT * FROM table WHERE id IN (".join(',',$_in_params).") AND product=:product";
    

    I'm not sure if this is the best and the most optimal solution, but it's a little bit more human readable :) And it can be helpful if you have a big an complicated query and you want to debug it

    (I'm curious if someone have a good argument why NOT to do in this way)

提交回复
热议问题