Access our premium support and let us know your problems, we will help you solve them.

0
No products in the cart.

Forum Replies Created

Viewing 1 post (of 1 total)
  • Author
    Posts
  • in reply to: WordPress prepared statement with IN() condition #10031
    blankgreenzilla
    Participant

    FUNCTION:

    function escape_array($arr){
        global $wpdb;
        $escaped = array();
        foreach($arr as $k => $v){
            if(is_numeric($v))
                $escaped[] = $wpdb->prepare('%d', $v);
            else
                $escaped[] = $wpdb->prepare('%s', $v);
        }
        return implode(',', $escaped);
    }
    

    USAGE:

    $arr = array('foo', 'bar', 1, 2, 'foo"bar', "bar'foo");
    
    $query = "SELECT values
    FROM table
    WHERE column NOT IN (" . escape_array($arr) . ")";
    
    echo $query;
    

    RESULT:

    SELECT values
    FROM table
    WHERE column NOT IN ('foo','bar',1,2,'foo\"bar','bar\'foo')
    

    May or may not be more efficient, however it is reusable.

Viewing 1 post (of 1 total)