1 回答

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超6個(gè)贊
由于您的數(shù)據(jù)存儲(chǔ)為 PHP 序列化數(shù)組,因此您需要首先提取通用數(shù)據(jù)并循環(huán)遍歷它,或者需要執(zhí)行 LIKE 查詢。
WordPress 有一個(gè)專門的函數(shù)來正確轉(zhuǎn)義 LIKE 參數(shù),稱為wpdb::esc_like
.?運(yùn)行完之后,SQL 應(yīng)該看起來像這樣:
SELECT
? ? user_id
FROM
? ? wp_usermeta
WHERE
? ? meta_key='addition_qualification'
? ? AND
? ? (
? ? ? ? meta_value? LIKE %s
? ? ? ? OR
? ? ? ? meta_value? LIKE %s?
? ? )
一旦你調(diào)用該 SQL 的準(zhǔn)備,它將被轉(zhuǎn)換為:
SELECT
? ? user_id
FROM
? ? wp_usermeta
WHERE
? ? meta_key='addition_qualification'
? ? AND
? ? (
? ? ? ? meta_value? LIKE '%Allgemeine Pharmazie%'
? ? ? ? OR
? ? ? ? meta_value? LIKE '%Geriatrische Pharmazie%'
? ? )
這不是最理想的解決方案,但它可能是在 WordPress 上下文中處理此類數(shù)據(jù)的最佳解決方案。這是創(chuàng)建上述內(nèi)容的代碼:
$zuqual = $this->userInput["Zuquali"];
if (!empty($zuqual)) {
? ? $likeTemplate = ' meta_value? LIKE %s ';
? ??
? ? // This will hold the above string repeated once for each item in our search array
? ? $likes = [];
? ??
? ? // This will hold sanitized values to perform LIKE searches, each surrounded by percent signs
? ? $params = [];
? ? foreach ($zuqual as $item) {
? ? ? ? $likes[] = $likeTemplate;
? ? ? ? $params[] = '%' . $this->wpdb->esc_like($item) . '%';
? ? }
? ??
? ? // If we have more than one search term, this will join with the OR, otherwise it will be left as-is
? ? $likeSql = implode(' OR ', $likes);
? ??
? ? // Create our query, remembering to surround the nested part with parentheses
? ? $sql = "SELECT user_id FROM wp_usermeta WHERE meta_key='addition_qualification' AND (" . $likeSql . ")";
? ??
? ? // Pass our escaped params in
? ? $prepared = $this->wpdb->prepare($sql, $params);
? ? $result = $this->wpdb->get_col($prepared);
}
- 1 回答
- 0 關(guān)注
- 194 瀏覽
添加回答
舉報(bào)