本文介绍了带有 IN() 条件的 WordPress 准备语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这样的字符串中有三个值:
I have three values in a string like this:
$villes = '"paris","fes","rabat"';
当我将它输入到这样的准备好的语句中时:
When I feed it into a prepared statement like this:
$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\"')
它不是将字符串写成三个单独的值——它只是一个双引号被转义的字符串.
It is not writing the string as three separate values -- it is just one string with the double quotes escaped.
如何在 WordPress 中使用多个值正确实现准备好的语句?
How can I properly implement a prepared statement in WordPress with multiple values?
推荐答案
试试这个代码:
// 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()
这篇关于带有 IN() 条件的 WordPress 准备语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!