首頁 >資料庫 >mysql教程 >如何在 WordPress 中使用帶有 IN() 條件的準備語句來取得多個值?

如何在 WordPress 中使用帶有 IN() 條件的準備語句來取得多個值?

Linda Hamilton
Linda Hamilton原創
2024-11-15 06:43:02846瀏覽

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

Proper WordPress Prepared Statement with IN() Condition for Multiple Values

When using prepared statements in WordPress, handling multiple values within an IN() condition can pose a challenge. This issue arises when the input values, stored in strings with double quotes, are not parsed correctly, resulting in a single string with escaped quotes.

To implement a prepared statement correctly in WordPress for multiple values, use the following approach:

  1. Create an array with the values for the IN() condition.
  2. Generate the SQL statement with the appropriate number of %s placeholders based on the length of the array using array_fill().
  3. Prepare the query using call_user_func_array(), passing the SQL statement and the array values as separate arguments.

Consider the following example:

// Values for IN() condition
$villes = array('paris', 'fes', 'rabat');

// Generate SQL statement
$sql = "SELECT DISTINCT telecopie FROM `comptage_fax` WHERE `ville` IN(" . implode(', ', array_fill(0, count($villes), '%s')) . ")";

// Prepare query
$query = call_user_func_array(array($wpdb, 'prepare'), array_merge(array($sql), $villes));

echo $query;

This code will output the correct SQL statement with three separate values in the IN() condition. Using this technique, you can ensure that prepared statements work as intended with multiple values in WordPress.

以上是如何在 WordPress 中使用帶有 IN() 條件的準備語句來取得多個值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn