我想從SQL注入使用$ wpdb-> prepare()優化SQL;MATCH AGAINST SQL注入預防和wpdb->準備
此查詢不返回任何內容。任何人都可以看到爲什麼或有一個更優雅的選擇。還有必要在MATCH AGAINST語句中轉義搜索條件。
$search_terms = "example search" // input post
// remove single quotes from search terms
$search_terms = str_replace("'","",$search_terms);
$sql = "SELECT
post.ID,
post.post_author,
post.post_date,
post.post_title,
LEFT(post.post_content, 240) As post_content,
post.post_name,
post.post_type,
post.comment_count,
post.comment_status,
MATCH (post_search.post_content,post_search.post_title) AGAINST (%s IN BOOLEAN MODE) AS score
FROM wp_posts post
LEFT JOIN wp_term_relationships term_rel
ON post.ID = term_rel.object_id
LEFT JOIN wp_term_taxonomy term_tax
ON term_rel.term_taxonomy_id = term_tax.term_taxonomy_id
LEFT JOIN wp_terms terms
ON term_tax.term_id = terms.term_id
LEFT JOIN wp_posts_fulltext_search post_search
ON post.ID=post_search.post_id
WHERE MATCH(post_search.post_content,post_search.post_title) AGAINST (%s IN BOOLEAN MODE) &&
post.post_status = 'publish' &&
post.post_type = 'post' &&
(term_tax.description != '' &&
term_tax.description NOT LIKE '%sample%')
GROUP BY post.ID
ORDER BY score DESC
LIMIT 0,20";
$results = $wpdb->get_results($wpdb->prepare($sql, $search_terms));
「As分數」用於返回更相關的結果到頂部。只是爲了重申我更關注SQL注入,MATCH AGAINST和wordpress準備功能。
什麼'$ wpdb-> print_error()'顯示,如果有什麼? – 2011-04-28 14:52:09
$ wpdb-> show_errors();什麼都沒顯示 – madphp 2011-04-28 15:04:27