小编典典

尝试在wpdb中使用IN()时出现问题

mysql

我有这个:

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

当我做一个echo $query;我得到:

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

在probleme我有是$wpdb附加'IN('...')

有人可以帮忙吗,谢谢


阅读 257

收藏
2020-05-17

共1个答案

小编典典

尝试以下代码(已修复):

// 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;
2020-05-17