Home > Database > Mysql Tutorial > body text

How to Use WordPress Prepared Statements with Multiple Values in the IN() Condition?

Mary-Kate Olsen
Release: 2024-11-12 19:09:02
Original
583 people have browsed it

How to Use WordPress Prepared Statements with Multiple Values in the IN() Condition?

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:

  1. Create an array of values: Convert the comma-separated string of values into an array of individual values.
$villes = array("paris", "fes", "rabat");
Copy after login
  1. Generate the SQL statement: Construct the SQL statement with the appropriate number of placeholders (%s) for the IN() condition.
$sql = "
  SELECT DISTINCT telecopie
  FROM `comptage_fax`
  WHERE `ville` IN(" . implode(', ', array_fill(0, count($villes), '%s')) . ")
";
Copy after login
  1. Use call_user_func_array: Call $wpdb->prepare using call_user_func_array to pass the values of the array as separate arguments. This ensures that each value is treated individually.
$query = call_user_func_array(array($wpdb, 'prepare'), array_merge(array($sql), $villes));

echo $query;
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template