PDO IN() Array Statement AND a placeholder

前端 未结 5 868
死守一世寂寞
死守一世寂寞 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)

    0 讨论(0)
  • 2021-01-04 08:55

    Solution

    This should work, if $values is an array:

    $query = "SELECT * FROM table WHERE id IN ($placeholders) AND product=?";
    $stm->execute(array_merge($values, array($product)));
    

    Explanation

    execute() expects one parameter - in this case an array - to be provided. By adding array_merge($values, array($product)) you create one array with $product added at the end, so the query should work correctly.

    See the demo here: http://ideone.com/RcClX

    0 讨论(0)
  • 2021-01-04 09:03

    Placeholders version if you need it

    $values = [1, 4, 7, 8];
    $placeholders =  preg_filter('/^/', ':prefix_', array_keys($values)));
    $query = 'SELECT * FROM table WHERE id IN ( '. implode(', ', $placeholders) . ')';
    
    $stmt = $db->prepare($query);
    
    if (count($values) > 0) {
        foreach ($values as $key => $current_value) {
            $stmt->bindValue($placeholders[$key] , $current_value, PDO::PARAM_STR);
        }
    }
    
    $stmt->execute();
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    0 讨论(0)
  • 2021-01-04 09:14
    $stm->execute($values,$product) ; //error happens when adding product placeholder
    

    The problem here is that execute needs a single array. You can't pass multiple arrays, and worse, you can't nest arrays.

    We already have a perfectly good $values array, so let's reuse it after you create the placeholder string.

    $values = explode(',', $values) ; # 1,4,7
    
    $placeholders = rtrim(str_repeat('?, ', count($values)), ', ') ;
    $query = "SELECT * FROM table WHERE id IN ($placeholders) AND product=?";
    
    // New!
    $values[] = $product;
    
    $stm = $db->prepare($query);
    $stm->execute($values);
    
    0 讨论(0)
  • 2021-01-04 09:17

    You forgot to prepare it ^_^

    $query = "SELECT * FROM table WHERE id IN ($placeholders) AND product=?";
    $stm = $db->prepare($query) ;
    $stm->execute($values,$product) ; //p00f
    

    And aside from that execute() should only have one parameter

    So the above won't work AT ALL!

    See the DOCs

    0 讨论(0)
提交回复
热议问题