WordPress Prepared Statement with Multiple Values Using IN Condition
When using a prepared statement in WordPress with multiple values in the IN() condition, it's crucial to properly feed those values into the statement. The original code attempted to pass a string containing the values, but WordPress interpreted it as a single string with escaped double quotes.
To rectify this, employ the following approach:
$villes = array("paris", "fes", "rabat");
$sql = " SELECT DISTINCT telecopie FROM `comptage_fax` WHERE `ville` IN(" . implode(', ', array_fill(0, count($villes), '%s')) . ") ";
$query = call_user_func_array(array($wpdb, 'prepare'), array_merge(array($sql), $villes)); echo $query;
This modified code ensures that the prepared statement is correctly constructed with multiple values in the IN() condition. By using an array of values and call_user_func_array, you can avoid the issue of escaped double quotes and execute the prepared statement with the desired results.
The above is the detailed content of How to Use WordPress Prepared Statements with Multiple Values in the IN() Condition?. For more information, please follow other related articles on the PHP Chinese website!