Home > Database > Mysql Tutorial > body text

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

Susan Sarandon
Release: 2024-11-14 14:33:02
Original
576 people have browsed it

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");
Copy after login
  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')) . ")
";
Copy after login
  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));
Copy after login

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!

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