mgraph mgraph - 2 years ago 72
SQL Question

Issue when trying to use IN() in wpdb

i have this:

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

when I do an
echo $query;
i get:

SELECT distinct telecopie FROM `comptage_fax` WHERE `ville` IN('\"CHAPELLE VIVIERS \",\"LE MANS \",\"QUEND\"')

the probleme i have is that

can someone help, thanks

Answer Source

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 = "
  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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download