1 回答
TA贡献1877条经验 获得超6个赞
由于您的数据存储为 PHP 序列化数组,因此您需要首先提取通用数据并循环遍历它,或者需要执行 LIKE 查询。
WordPress 有一个专门的函数来正确转义 LIKE 参数,称为wpdb::esc_like
. 运行完之后,SQL 应该看起来像这样:
SELECT
user_id
FROM
wp_usermeta
WHERE
meta_key='addition_qualification'
AND
(
meta_value LIKE %s
OR
meta_value LIKE %s
)
一旦你调用该 SQL 的准备,它将被转换为:
SELECT
user_id
FROM
wp_usermeta
WHERE
meta_key='addition_qualification'
AND
(
meta_value LIKE '%Allgemeine Pharmazie%'
OR
meta_value LIKE '%Geriatrische Pharmazie%'
)
这不是最理想的解决方案,但它可能是在 WordPress 上下文中处理此类数据的最佳解决方案。这是创建上述内容的代码:
$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 关注
- 146 浏览
添加回答
举报