Issue when trying to use IN() in wpdb

后端 未结 4 1405
旧时难觅i
旧时难觅i 2020-11-28 13:52

i have this:

$villes = \'\"paris\",\"fes\",\"rabat\"\';
$sql    = \'SELECT distinct telecopie FROM `comptage_fax` WHERE `ville` IN(%s)\';
$query  = $wpdb->         


        
4条回答
  •  情歌与酒
    2020-11-28 14:30

    Try this code (FIXED):

    // Create an array of the values to use in the list
    $villes = array("paris", "fes", "rabat");    
    
    // Generate the SQL statement.
    // The number of %s items is based on the length of the $villes array
    $sql = "
      SELECT DISTINCT telecopie
      FROM `comptage_fax`
      WHERE `ville` IN(".implode(', ', array_fill(0, count($villes), '%s')).")
    ";
    
    // Call $wpdb->prepare passing the values of the array as separate arguments
    $query = call_user_func_array(array($wpdb, 'prepare'), array_merge(array($sql), $villes));
    
    echo $query;
    
    • implode()
    • array_fill()
    • call_user_func_array()
    • array_merge()

提交回复
热议问题