Home >Database >Mysql Tutorial >How to Use WordPress Prepared Statements with IN() Conditions?

How to Use WordPress Prepared Statements with IN() Conditions?

Susan Sarandon
Susan SarandonOriginal
2024-11-14 14:33:02651browse

How to Use WordPress Prepared Statements with IN() Conditions?

WordPress Prepared Statements with IN() Conditions

When attempting to use a WordPress prepared statement with multiple values in an IN() condition, you may encounter issues with the values being concatenated into a single string with escaped double quotes.

To resolve this, follow these steps:

  1. Convert the values to an array:
$villes = array("paris", "fes", "rabat");
  1. Generate the SQL statement with the placeholder for the values:
$sql = "
  SELECT DISTINCT telecopie
  FROM `comptage_fax`
  WHERE `ville` IN(" . implode(', ', array_fill(0, count($villes), '%s')) . ")
";
  1. Use call_user_func_array to prepare the statement with the values as separate arguments:
$query = call_user_func_array(array($wpdb, 'prepare'), array_merge(array($sql), $villes));

This method ensures that each value is properly escaped and passed as a separate parameter.

The above is the detailed content of How to Use WordPress Prepared Statements with IN() Conditions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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