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:
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.
Atas ialah kandungan terperinci Bagaimana Menggunakan Penyata Disediakan dengan Syarat IN() untuk Berbilang Nilai dalam WordPress?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!