当关键字包含&符号时,wpdb get_results为空(同时请求在phpMyAdmin SQL中正常工作)

时间:2015-04-10 13:10:04

标签: php wordpress wpdb ampersand

我对wpdb-> get_results有一个非常具体的问题 我已经实现了自定义搜索请求,可以搜索我们所有的自定义类型和自定义结构表。 查询工作正常(因此没有数据库连接问题),直到搜索关键字包含&符号(&)。 真正的谜团是,如果我回应请求本身然后通过phpMyAdmin SQL运行它,查询将返回预期(和期望的)结果。 我尝试使用mysqli,但无济于事。

以下是搜索系统中使用的代码的关键部分:

$request = "SELECT DISTINCT
                    SQL_CALC_FOUND_ROWS
                    p.ID,
                    (SELECT meta_value FROM wp_postmeta WHERE post_id = p.ID AND meta_key = 'latitude') AS latitude,
                    (SELECT meta_value FROM wp_postmeta WHERE post_id = p.ID AND meta_key = 'longitude') AS longitude
                        FROM wp_posts p
                        WHERE p.post_type = 'studia'
                            AND (SELECT meta_value FROM wp_postmeta WHERE post_id = p.ID AND meta_key = 'latitude') > 0
                            AND (SELECT meta_value FROM wp_postmeta WHERE post_id = p.ID AND meta_key = 'longitude') > 0
                            AND ((SELECT meta_value FROM wp_postmeta WHERE post_id = p.ID AND meta_key = 'mesto' LIMIT 1) LIKE '%" . $keyword . "%'
                                 OR p.post_title LIKE '%" . $keyword . "%'
                                 OR (SELECT meta_value FROM wp_postmeta WHERE post_id = p.ID AND meta_key = 'ulice' LIMIT 1) LIKE '%" . $keyword . "%'
                                 OR (SELECT IF(pm.meta_value LIKE 'a:%',(SELECT post_title FROM wp_posts WHERE ID = SUBSTRING_INDEX(SUBSTRING_INDEX(pm.meta_value,'\"',2),'\"',-1)), pm.meta_value ) FROM wp_postmeta pm WHERE pm.post_id = p.ID AND meta_key = 'studio' ) LIKE '%" . $keyword . "%')";
        echo $request;
        $studios = $wpdb->get_results( $request);

以下是回复的示例(关键字: YOGA& ART )请求,再次在phpMyAdmin中返回所需的结果,但不在网站上

SELECT DISTINCT SQL_CALC_FOUND_ROWS p.ID, (SELECT meta_value FROM wp_postmeta WHERE post_id = p.ID AND meta_key = 'latitude') AS latitude, (SELECT meta_value FROM wp_postmeta WHERE post_id = p.ID AND meta_key = 'longitude') AS longitude FROM wp_posts p WHERE p.post_type = 'studia' AND (SELECT meta_value FROM wp_postmeta WHERE post_id = p.ID AND meta_key = 'latitude') > 0 AND (SELECT meta_value FROM wp_postmeta WHERE post_id = p.ID AND meta_key = 'longitude') > 0 AND ((SELECT meta_value FROM wp_postmeta WHERE post_id = p.ID AND meta_key = 'mesto' LIMIT 1) LIKE '%YOGA & ART%' OR p.post_title LIKE '%YOGA & ART%' OR (SELECT meta_value FROM wp_postmeta WHERE post_id = p.ID AND meta_key = 'ulice' LIMIT 1) LIKE '%YOGA & ART%' OR (SELECT IF(pm.meta_value LIKE 'a:%',(SELECT post_title FROM wp_posts WHERE ID = SUBSTRING_INDEX(SUBSTRING_INDEX(pm.meta_value,'"',2),'"',-1)), pm.meta_value ) FROM wp_postmeta pm WHERE pm.post_id = p.ID AND meta_key = 'studio' ) LIKE '%YOGA & ART%')

1 个答案:

答案 0 :(得分:1)

我找到了答案。简单地说,因为我回应请求,显示格式与实际发送的格式不同,结果发现实际发送的是HTML实体&而不是符号&放;.如此简单的替换解决了我的问题。

$keyword = str_replace("&", "&", $keyword);